OS Unit 1 - 2nd - BCA
OS Unit 1 - 2nd - BCA
Unit-1 INTRODUCTION
Operating System
Definition:
An operating system act as an intermediary between the user of a computer and
computer hardware. The purpose of an operating system is to provide an environment
in which a user can execute programs in a convenient and efficient manner. An
operating system is software that manages the computer hardware.
(Or)
LINUX Symbian
1
Operating System
2
Operating System
keyboard, keeping track of files and directories on the disk, sending output to the
display screen and controlling a peripheral devices.
Control Program
Controls execution of user programs and operation of I/O devices.
Kernel
At the core of the system view is the kernel, which:
3
Operating System
A command interpreter is a program which reads the instructions given by the user. It
then translates these instructions into the context of the operating system followed by
the execution. Command interpreter is also known as shell.
Loader:- A loader is a routine that loads an object program and prepares it for
execution. There are various loading schemes: absolute, relocating and direct linking.
In general, the loader must load, relocate, and link the object program
4
Operating System
1. Batch System
Some computer systems only did one thing at a time. They had a list of the computer
system may be dedicated to a single program until its completion, or they may be
dynamically reassigned among a collection of active programs in different stages of
execution.
Batch operating system is one where programs and data are collected together in a
batch before processing starts. A job is predefined sequence of commands, programs
and data that are combined in to a single unit called job.
Batch processing is a technique in which Operating System collects one programs and
data together in a batch before processing starts. Operating system does the following
activities related to batch processing.
Advantages
5
Operating System
Batch processing takes much of the work of the operator to the computer.
Increased performance as a new job get started as soon as the previous
job finished without any manual intervention.
Disadvantages
2. Multi-programming OS
When two or more programs are residing in memory at the same time, then sharing
the processor is referred to the multiprogramming. Multiprogramming assumes a
single shared processor. Multiprogramming increases CPU utilization by organizing
jobs so that the CPU always has one to execute.
The operating system keeps several jobs in memory at a time. This set of jobs is a
subset of the jobs kept in the job pool. The operating system picks and begins to
execute one of the job in the memory.
Having several programs in memory at the same time requires some form of memory
management. Multiprogramming operating system monitors the state of all active
programs and system resources. This ensures that the CPU is never idle unless there
are no jobs.
6
Operating System
Advantages
Disadvantages
In time sharing system, the CPU will be allocated to each user for a small, fixed
period of time (Generally the timeslots are defined by the OS.) and it is switched from
one job to another either when there a natural break or when the allocated timeslot is
7
Operating System
over. Time shared systems are much more complex than Multi-Programming
Operating System. However multiprogramming and time sharing are the central
themes of modern OS.
8
Operating System
RTOS is used when rigid time requirements have been placed on the operation of a
processor or flow of data. This system is often used as a control device in dedicated
applications. RTOS has well defined, fixed time constraints. Processing must be done
within the defined constraints otherwise the system fails.
Hard real-time systems: They are highly stringent systems which guarantees that
critical tasks be completed on-time. Here, the time constraint dictate the facilities
available in the system.
Soft real-time systems: These are less stringent. Here, critical tasks get priority over
other tasks
and retain that priority till they complete. Thus, the prime objective of this system is to
provide quick event response.
Advantages:
Disadvantages:
Hard real-time systems are highly stringent and fails if they are unable to respond
within the defined time constraint.
Soft real-time systems are less restrictive and do not support deadline scheduling.
5. Distributed System
9
Operating System
1. Process Management
2. Main Memory Management
3. File Management
4. Secondary Storage Management
5. I/O System Management
6. Networking
7. Protection System
8. Command Interpreter System
1. Process Management
The operating system manages many kinds of activities ranging from user programs to
system programs like printer spooler, name servers, file server etc. Each of these
activities is encapsulated in a process. A process includes the complete execution
context (code, data, PC, registers, OS resources in use etc.).
It is important to note that a process is not a program. A process is only ONE instant
of a program in execution. There are many processes can be running the same
program.
The five major activities of an operating system in regard to process management are
2. Main-Memory Management
10
Operating System
Keep track of which part of memory are currently being used and by whom.
Decide which process is loaded into memory when memory space becomes
available.
Allocate and deallocate memory space as needed.
3. File Management
A file is a collected of related information defined by its creator. Computer can store
files on the disk (secondary storage), which provide long term storage. Some
examples of storage media are magnetic tape, magnetic disk and optical disk. Each of
these media has its own properties like speed, capacity, data transfer rate and access
methods.
File systems normally organized into directories to ease their use. These directories
may contain files and other directions.
The five main major activities of an operating system in regard to file management are
I/O subsystem hides the peculiarities of specific hardware devices from the user. Only
the device driver knows the peculiarities of the specific device to which it is assigned.
5. Secondary-Storage Management
Generally speaking, systems have several levels of storage, including primary storage,
secondary storage and cache storage. Instructions and data must be placed in primary
storage or cache to be referenced by a running program. Because main memory is too
small to accommodate all data and programs, and its data are lost when power is lost,
the computer system must provide secondary storage to back up main memory.
Secondary storage consists of tapes, disks, and other media designed to hold
information that will eventually be accessed in primary storage (primary, secondary,
cache) is ordinarily divided into bytes or words consisting of a fixed number of bytes.
Each location in storage has an address; the set of all addresses available to a program
is called an address space.
11
Operating System
6. Networking
7. Protection System
If computer systems has multiple users and allows the concurrent execution of
multiple processes, then the various processes must be protected from one another's
activities. Protection refers to mechanism for controlling the access of programs,
processes, or users to the resources defined by computer systems.
A command interpreter is an interface of the operating system with the user. The user
gives commands with are executed by operating system (usually by turning them into
system calls). The main function of a command interpreter is to get and execute the
next user specified command.
1. If we want to change the way the command interpreter looks, i.e., I want to
change the interface of command interpreter, I am able to do that if the
command interpreter is separate from the kernel. I cannot change the code of
the kernel so I cannot modify the interface.
12
Operating System
1. Program execution
2. I/O operation
3. File system manipulation
4. Communications
5. Error detection
6. Resource Allocation
7. Accounting
8. Protection
1. Program execution: Operating system loads a program into memory and executes
the program. The program must be able to end its execution, either normally or
abnormally.
2. I/O Operation: I/O means any file or any specific I/O device. Program may require
any I/O device while running. So operating system must provide the required I/O.
3. File system manipulation: Program needs to read a file or write a file. The
operating system gives the permission to the program for operation on file.
4. Communication: Data transfer between two processes is required for some time.
The both processes are on the one computer or on different computer but connected
through computer network. Communication may be implemented by two methods:
a. Shared memory
b. Message passing.
5. Error detection: error may occur in CPU, in I/O devices or in the memory
hardware. The operating system constantly needs to be aware of possible errors. It
should take the appropriate action to ensure correct and consistent computing.
6. Resource Allocation: If there is more than one user or jobs running at the same
time, then resources must be allocated to each of them. Operating system manages
different types of resources require special allocation code, i.e. main memory, CPU
cycles and file storage.
13
Operating System
There are some resources which require only general request and release code. For
allocating CPU, CPU scheduling algorithms are used for better utilization of CPU.
CPU scheduling algorithms are used for better utilization of CPU. CPU scheduling
routines consider the speed of the CPU, number of available registers and other
required factors.
7. Accounting: Logs of each user must be kept. It is also necessary to keep record of
which user how much and what kinds of computer resources. This log is used for
accounting purposes. The accounting data may be used for statistics or for the billing.
It also used to improve system efficiency.
System Calls
The system call provides an interface to the operating system services. Application
developers often do not have direct access to the system calls, but can access them
through an application programming interface (API). The functions that are included
in the API invoke the actual system calls. By using the API, certain benefits can be
gained:
14
Operating System
Portability: as long a system supports an API, any program using that API can
compile and run.
Ease of Use: using the API can be significantly easier then using the actual system
call.
2. File Management: Some common system calls are create, delete, read, write,
reposition, or close. Also, there is a need to determine the file attributes – get and set
file attribute. Many times the OS provides an API to make these system calls.
15
Operating System
Process Management
Concept of Process
A process is sequential program in execution. A process defines the fundamental unit
of computation for the computer.
Structure of a process
A process is a program in execution. A process is more than the program code, which
is sometimes known as the text section.
It also includes the current activity, as represented by the value of the program counter
and the contents of the processor's registers.
A process generally also includes the process stack, which contains temporary data
(such as function parameters, return addresses, and local variables), and a data
section, which contains global variables.
A process may also include a heap, which is memory that is dynamically allocated
during process run time.
16
Operating System
Process State
When process executes, it changes state. Process state is defined as the current activity
of the process.
Process state contains five states. Each process is in one of the states.
1. New
2. Ready
3. Running
4. Waiting
5. Terminated (exist)
1. New: A process that just been created.
2. Ready: Ready processes are waiting to have the processor allocated to them
by the operating system so that they can run.
3. Running: The process that is currently being executed. A running process
possesses all the resources needed for its execution, including the processor.
4. Waiting: A process that cannot execute until some event occurs such as the
completion of an I/O operation. The running process may become suspended by
invoking an I/O module.
5. Terminated: A process that has been released from the pool of executable
17
Operating System
Whenever processes changes state, the operating system reacts by placing the process
PCB in the list that corresponds to its new state. Only one process can be running on
any processor at any instant and many processes may be ready and waiting state.
Each process contains the process control block (PCB). PCB is the data structure used
by the operating system. Operating system groups all information that needs about
particular process.
18
Operating System
1. Pointer: Pointer points to another process control block. Pointer is used for
maintaining the scheduling list.
2. Process State: Process state may be new, ready, running, waiting and so on.
4. Event information: For a process in the blocked state this field contains
information concerning the event for which the process is waiting.
5. CPU register: It indicates general purpose register, stack pointers, index registers
and accumulator’s etc. number of register and type of register totally depends upon the
computer architecture.
7. Accounting Information: This information includes the amount of CPU and real
time used, time limits, job or process numbers, account numbers etc. Process control
block also includes the information about CPU scheduling, I/O resource management,
file management information, priority and so on. The PCB simply serves as the
repository for any information that may vary from process to process.
The process scheduling is the activity of the process manager that handles the removal
of the running process from the CPU and the selection of another process on the basis
of a particular strategy.
The scheduling mechanism is the part of the process manager that handles the removal
of the running process from the CPU and the selection of another process on the basis
of particular strategy.
19
Operating System
Scheduling Queues
When the process enters into the system, they are put into a job queue. This queue
consists of all processes in the system. The operating system also has other queues.
The processes that are residing in main memory and are ready and waiting to execute
are kept on a list called the ready queue
Device queue is a queue for which a list of processes waiting for a particular I/O
device. Each device has its own device queue.
Fig. shows the queuing diagram of process scheduling. In the fig , queue is
represented by rectangular box.
Queues are of two types: ready queue and set of device queues. A newly arrived
process is put in the ready queue. Processes are waiting in ready queue for allocating
the CPU. Once the CPU is assigned to the process, then process will execute. While
executing the process, one of the several events could occur.
1. The process could issue an I/O request and then place in an I/O queue.
2. The process could create new sub process and waits for its termination.
20
Operating System
3. The process could be removed forcibly from the CPU, as a result of interrupt
and put back in the ready queue.
Schedules
It is also called job scheduler. Long term scheduler determines which programs are
admitted to the system for processing. Job scheduler selects processes from the queue
and loads them into memory for execution. Process loads into the memory for CPU
scheduler. The primary objective of the job scheduler is to provide a balanced mix of
jobs, such as I/O bound and processor bound.
Timesharing operating systems have no long term scheduler. When process changes
the state from new to ready, then there is a long term scheduler.
21
Operating System
Medium term scheduling is part of the swapping function. It removes the processes
from the memory. It reduces the degree of multiprogramming. The medium term
scheduler is in charge of handling the swapped out-processes.
CPU Scheduling
CPU scheduling is a process which allows one process to use the CPU while the
execution of another process is on hold (in waiting state) due to unavailability of any
resource like I/O etc, thereby making full use of CPU. The aim of CPU scheduling is
to make the system efficient, fast and fair.
CPU Scheduler
Whenever the CPU becomes idle, the operating system must select one of the
processes in the ready queue to be executed. The selection process is carried out by
the short-term scheduler (or CPU scheduler). The scheduler selects a process from the
processes in memory that are ready to execute and allocates the CPU to that process.
CPU-scheduling decisions may take place under the following four circumstances:
1. When a process switches from the running state to the waiting state (for example, as
the result of an I/O request or an invocation of wait for the termination of one of the
child processes)
22
Operating System
2. When a process switches from the running state to the ready state (example, when
an interrupt occurs)
3. When a process switches from the waiting state to the ready state (for example, at
completion of I/O)
For situations 1 and 4, there is no choice in terms of scheduling. A new process (if one
exists in the ready queue) must be selected for execution. There is a choice, however,
for situations 2 and 3.
When scheduling takes place only under circumstances 1 and 4, we say that the
scheduling scheme is non pre-emptive or cooperative; otherwise, it is pre-emptive.
Under non pre-emptive scheduling, once the CPU has been allocated to a process, the
process keeps the CPU until it releases the CPU either by terminating or by switching
to the waiting state.
1. Pre-emptive Scheduling
In preemptive scheduling, the tasks are assigned based on their priorities. For
example, a low-priority task is running. After some time, a high-priority task comes
into the system for execution, and then since the new task is of high priority, the
operating system will keep the low priority task on hold and allocate the CPU to the
high priority task. As the high-priority task will finish its execution, the CPU will be
reallocated to the low-priority task. This is known as preemptive scheduling.
Preemptive scheduling also occurs when a process switches from the running state to
the ready state and switches from the waiting state to the ready state.
2. Non-Preemptive Scheduling
In non-preemptive scheduling, once the CPU has been allocated to a process, the
process keeps the CPU until it releases the CPU either by terminating or by switching
to the waiting state. New processes are executed only after the current process has
completed its execution. The process holds the resources of the CPU (CPU time) till
its state changes to terminated or is pushed to the process waiting state. If a process is
currently being executed by the CPU, it is not interrupted till it is completed.
23
Operating System
Once the process has completed its execution, the processer picks the next process
from the ready queue (the queue in which all processes that are ready for execution are
stored).
Scheduling Criteria
Different CPU scheduling algorithms have different properties, and the choice of a
particular algorithm may favour one class of processes over another. In choosing
which algorithm to use in a particular situation, we must consider the properties of the
various algorithms.
Many criteria have been suggested for comparing CPU scheduling algorithms. Which
characteristics are used for comparison can make a substantial difference in which
algorithm is judged to be best. The criteria include the following:
1. CPU utilization.
We want to keep the CPU as busy as possible. Conceptually, CPU utilization can
range from 0 to 100 percent. In a real system, it should range from 40 percent (for a
lightly loaded system) to 90 percent (for a heavily used system).
2. Throughput.
If the CPU is busy executing processes, then work is being done. One measure of
work is the number of processes that are completed per time unit, called throughput.
For long processes, this rate may be one process per hour; for short transactions, it
may be 10 processes per second.
3. Turnaround time.
From the point of view of a particular process, the important criterion is how long it
takes to execute that process. The interval from the time of submission of a process to
the time of completion is the turnaround time. Turnaround time is the sum of the
periods spent waiting to get into memory, waiting in the ready queue, executing on the
CPU, and doing I/O.
4. Waiting time.
24
Operating System
The CPU scheduling algorithm does not affect the amount of time during which a
process executes or does I/O; it affects only the amount of time that a process spends
waiting in the ready queue. Waiting time is the sum of the periods spent waiting in the
ready queue.
5. Response time.
In an interactive system, turnaround time may not be the best criterion. Often, a
process can produce some output fairly early and can continue computing new results
while previous results are being output to the user. Thus, another measure is the time
from the submission of a request until the first response is produced. This measure,
called response time, is the time it takes to start responding, not the time it takes to
output the response. The turnaround time is generally limited by the speed of the
output device.
Scheduling Algorithms
A multi-processor is a system that has more than one processor but shares the same
memory, bus, and input/output devices. The bus connects the processor to the RAM,
to the I/O devices, and to all the other components of the computer.
The system is a tightly coupled system. This type of system works even if a processor
goes down. The rest of the system keeps working. In multi-processor scheduling, more
than one processors(CPUs) share the load to handle the execution of processes
smoothly.
25
Operating System
Load balancing is a problem since more than one processors are present.
Processes executing simultaneously may require access to shared data.
Cache affinity should be considered in scheduling.
Thread Scheduling
The Lightweight process is threads that act as an interface for the User-Level Threads
to access the physical CPU resources.
The number of the lightweight processes depends on the type of application, for an I\O
bound application the number of LWP depends on the user level threads, and for CPU
bound application each thread is connected to a separate kernel-level thread.
26
Operating System
Contention scope – Control scope defines the extent to which contention takes
place.
Contention refers to the competition among the ULTs to access the KLTs.
Contention scope can be further classified into Process Contention Scope (PCS)
and System Contention Scope (SCS).
Process Contention Scope: Process Contention Scope is when the contention takes
place in the same process.
System contention scope (SCS): System Contention Scope refers to the contention
that takes place among all the threads in the system.
Allocation domain – The allocation domain is a set of multiple (or single)
resources for which a thread is competing.
It is cheaper.
It helps reduce system calls and achieve better performance.
If the SCS thread is a part of more than one allocation domain, the system will
have to handle multiple interfaces.
PCS thread can share one or multiple available LWPs, while every SCS thread
needs a separate LWP. Therefore, for every system call, a separate KLT will be
create
Real-Time Scheduling
Systems that carry real-time tasks are known as real-time systems. These tasks can be
divided into two categories:
The scheduler aims to reduce the response time for each process.
27
Operating System
In a simple real-time system, there might be no need for a scheduler, one task can
simply call the next. But for more complex real-time systems, that have a large but
fixed number of tasks that do not function in pipeline function, one may need static
scheduling.
And for real-time systems where the workload keeps changing, one may need
dynamic scheduling.
Best efforts
Work shedding
To decide which process to execute first from the ready queue these methods may be
used:
SJF
Static priority
Slack time.
Context Switch
When the scheduler switches the CPU from executing one process to executing
another, the context switcher saves the content of all processor registers for the
process being removed from the CPU in its process being removed from the CPU in
its process descriptor. The context of a process is represented in the process control
block of a process.
28
Operating System
One process does not directly switch to another within the system. Context
switching makes it easier for the operating system to use the CPU’s resources to
carry out its tasks and store its context while switching between multiple
processes.
Context switching enables all processes to share a single CPU to finish their
execution and store the status of the system’s tasks. The execution of the process
begins at the same place where there is a conflict when the process is reloaded
into the system.
29
Operating System
Operations On Processes
1) Process Creation
A process can create several new processes through create process system call during
the process execution. The process which creates a another process is called as parent
process and the new process is a child process.
Every new process creates another process forming a tree-like structure. It can be
identified with a unique process identifier that usually represents it as Pid which is
typically an integer number. Every process needs some resources like CPU time,
memory, file, I/O devices to accomplish. Whenever a process creates a sub process,
and may be each sub process is able to obtain its resources directly from the operating
system or from the resources of the parent process. The parent process needs to
partition its resources among all its children or it may be able to share some resources
to several children.
Restricting a child process to a subset of the parent’s resources prevents any process
from overloading the system by creating too many sub-processes. A process is going
to obtain its resources whenever it is created.
Whenever a process creates a new process, there are two possibilities in terms of
execution, which are as follows –
There are two more possibilities in terms of address space of the new process, which
are as follows-
2) Process Termination
A process terminates when it finishes executing its last statement and request the
operating system to delete it by issuing the “exit system call”. And the process may
return data, output to its parent
30
Operating System
process when it exits using fork system call. And when a process exits all the
resources of that process including physical, virtual memory, open files, and
input/output buffers are de – allocated by the operating system.
The reasons that the process may terminate the execution of one of its children are as
follows −
The child exceeds its usage of resources that it has been allocated.
The task that is assigned to the child is no longer required.
The parent is exiting and the operating system does not allow a child to continue if
its parent terminates.
1) Independent Process
2) Cooperating Process
An Independent process is one that doesn’t get affected by the other processes running
in the system. It doesn’t even affect the processing of any other process in the system.
This is because the independent process doesn’t share any data with the other process.
The cooperating process is one that shares data with the other processes in the system.
Thus, it can get affected by the other processes of the system. And even it can affect
the working of other processes. So, among these two processes, the cooperating
process requires inter-process communication.
31
Operating System
There are two methods or two models that operating systems can implement to
achieve IPC. Some operating systems implement both of these models.
Shared Memory
Message Passing
Consider two cooperating processes P1 and P2. Both the processes P1 and P2 have
their different address spaces. Now let us assume, P1 wants to share some data with
P2. So, P1 and P2 will have to perform the following steps:
Step 1 − Process P1 has some data to share with process P2. First P1 takes initiative
and establishes a shared memory region in its own address space and stores the data or
information to be shared in its shared memory region.
Step 2 − Now, P2 requires the information stored in the shared segment of P1. So,
process P2 needs to attach itself to the shared address space of P1. Now, P2 can read
out the data from there.
32
Operating System
Step 3 − The two processes can exchange information by reading and writing data in
the shared segment of the process.
Send
Receive
Message sent by processes can be either fixed or variable size. In order to send and
receive message from each other, the processes must have a communication link
existing between them. The link can be implemented in many ways.
1. Direct or Indirect
2. Synchronous and Asynchronous Communication
3. Automatic and Explicit Buffering
For processes which want to communicate there must be a way in order to refer to
each other. They can be either direct or indirect communication.
33
Operating System
1. Direct Communication:
In this method each processes which wants to communicate must explicitly name the
sender or recipient of the communication.
In this method the send and receive primitives are defined as follows
Addressing in this scheme is symmetric, that is both receiver and sender processes
have to name each other to communicate.
In asymmetric type, only the sender names the recipient, but recipient need not name
the sender so,
Where the variable id is set to the process name with which communication takes
place.
2. Indirect Communication:
In this scheme, the sending and receiving of message is done using a mail box (called
as ports). A mail box abstractly can be viewed as an object, where the processes place
their message and removes the messages from it. Each mail box will have a unique
identification and a process can communicate with some other process, through
number of different mailbox. Communication between two processes is possible, only
if they have a shared mailbox. The send, receive primitives are defined as,
34
Operating System
Communication happens using send() and receive(). There are many options for these
two primitives.
The number of messages that can be present is determined by a link and is considered
as queue of messages to the link. This queue can be implemented using the following
3 methods
This has maximum length zero, so a link cannot maintain any message waiting in it.
Thus sender must wait until the recipient receives the message. And also the two
processes communicating must be synchronized for a message transfer to happen.
This queue type is also called as message system with no buffering.
This type will have a finite length say ‘n’. So at the most ‘n’ messages can be stored in
it. The new messages sent can be placed in queue, if it is not full. And sender can
continue execution. When the link is full, the sender must wait, until the queue space
becomes free. This queue provides automatic buffering.
35
Operating System
This has a infinite length and hence any number of messages can wait in it. So, the
sender will not be delayed anytime. This queue type also provides automatic
buffering.
36