M2
M2
PROCESS MANAGEMENT
A program is a set of instructions which is stored on a disk. A process is a program loaded into
memory for execution. Thus a program can be termed as passive entity and a process as an active
entity.
In case of C program, a program is written and stored in .c extension. When this .c extension file is
compiled, it is converted into machine language .o executable file. These are all programs, when this
.o file is loaded into memory for execution it is called as process.
Fig 2.1 shows a process in memory. When a process is created, these are the fields present in a
process. A text section contains the program code to be executed, also contains program counter
which will give next instruction to be executed along with a set of registers. The data section
contains the global variables declared for the program. The stack section consists of function
parameters, return addresses, local variables etc. The heap section is a memory which is allocated
dynamically during the process runtime.
Waiting: The process is waiting for some event to occur (such as I/O completion or
reception of a signal)
Terminated: The process has finished its execution.
Several processes may be in ready or waiting state but only 1 process will be in running state per
processor.
Process Control Block:
The process control block is a data structure in operating system kernel containing information
needed to manage a particular process. It is also called as task control block.
Process State: The state may be new, ready, running, waiting and so on.
Program Counter: Indicates the address of next instruction to be executed for this process.
Registers: The registers vary in number and type depending on the computer architecture.
They include accumulators, index registers, stack pointers and general purpose registers.
When an interrupt occurs, the program counter and state information of these registers
must be saved to allow a process to be continued correctly afterwards as shown in Fig 2.4
CPU scheduling information: Includes process priority, pointers to scheduling queues and
any other scheduling parameters.
Operating Systems
Memory Management information: The information may include the value of base and limit
registers, the page tables or segment tables.
Accounting information: This information includes the amount of CPU and real time used,
time limits, account numbers, job or process numbers and so on.
I/O status Information: Information includes list of I/O devices allocated to the process, a
list of open files and so on.
PCB is a repository for information that may vary from process to process.
A process is a program that performs single thread of execution or multiple threads of execution.
On a system that allows thread, a PCB is expanded to include information for each thread.
Process Scheduling:
The objective of multiprogramming is to have some process running at all to time to maximize the
CPU utilization. The objective of time sharing is to switch the CPU among processes so frequently
that users can interact with each program while it is running. To meet these objectives, the process
scheduler selects an available process for program execution on the CPU.
Scheduling Queues:
As process enters the system, they are put into the job queue which consists of all processes in the
system. The processes that are in main memory and are ready and waiting to execute are kept on a
list called the ready queue.
As shown in Fig 2.5, this queue is generally stored as a linked list. A ready queue header contains
pointers to the first and final PCB’s in the list. Each PCB includes a pointer field that points to the
next PCB in the ready queue. The system also includes other queues. When a process is allocated
the CPU, it executes for a while and eventually quits, is interrupted or waits for the occurrence of a
particular event such as the completion of an I/O request. Suppose the process makes an I/O
request to a shared device, like disk.
Operating Systems
Process could issue I/O request and then be placed in an I/O queue.
Process could create sub-process and wait for it to finish its execution.
Process could be removed forcibly from the CPU, due to interrupt and be put back in ready
queue.
Operating Systems
In the first 2 cases, the process switches from waiting state to ready state and is then put back in
ready queue. A process continues this cycle until it terminates, once it terminates it is removed
from all queues and PCB and its resources are deallocated.
Schedulers:
A process migrates from one queue to another during its execution. For scheduling purpose the
operating system must select the processes from these queues. This selection process is carried out
by appropriate scheduler.
Often more processes are submitted than can be executed immediately. These processes are
spooled to a mass storage device where they are kept for later execution. 2 types of scheduler are
used.
The long term scheduler selects process from this pool and loads them into memory for
execution.
The short term scheduler selects from among the processes that are ready to execute and
allocates CPU to one of them.
The frequency of selection between these 2 schedulers vary. The short term scheduler must
select new process frequently i.e it selects once I every 100 milliseconds. The long term
scheduler selects new processes less frequently minutes may separate the creation of new
process and the next.
The processes can be divided into I/O bound process which spends more time in doing I/O
than it spends doing computations and CPU bound process generates I/O requests less
frequently but does CPU more computations. Long term scheduler must select the processes
carefully to have good mix.
Some operating systems introduces intermediate level of scheduling such as in time sharing
systems.
of new process scheduled to run. Context switch time is pure overhead as it does no useful work
while switching.
Operations on Processes:
A process may create several processes via create process system call during the course of
execution. The creating process is called parent process and the created process is called child
process. Each of these child processes inturn creates several processes which forms a tree like
structure. Each of these processes are identified by using process identifier (pid).
Whenever a process is created it needs several resources (CPU time, memory, files, I/O devices) for
its execution. When a process creates subprocess, the subprocess may be able to obtain the
resources from the operating system directly or it may be constrained to subset of resources of
parent process.
When a process creates a new process, 2 possibilities exists
The child process is a duplicate of parent process (same program and data)
The child process has a new program loaded into it.
In UNIX, each process has a unique id i.e pid, a new process is created by using fork ( ) system call.
The new process consists of same address space as of parent process. This makes it easier for
parent process to communicate with child. When a fork ( ) call is made, it returns 2 values i.e zero to
child process and process id (PID) of child process to parent process.
Exec( ) system call is used after fork ( ) call, exec ( ) call is used to load new program into the
process. The exec ( ) system call loads a binary file into memory and starts its execution. The parent
can then create more children or if it has nothing to do while a child runs, it can issue a wait ( )
system call to move itself out of ready queue until termination of child.
A parent may terminate the execution of one of its child process for various reasons like:
The child has exceeded the usage of resources that it has been allocated.
The task assigned to child is no longer required.
The parent is exiting and the operating system does not allow child to continue without the
parent.
In some of the systems when a parent terminates, along with it the child process will also be
terminated. This phenomenon is referred to as cascading termination, normally initiated by the
operating system.
Interprocess Communication:
Process executing concurrently in the operating system may be independent processes or
cooperating processes.
A process is independent if it does not affect or get affected by any other process executing in the
system. Any process that does not share the data with any process is independent.
A process is cooperating if it gets affected or if it affects any other process executing in the system.
Any process that shares the data with other processes is a cooperating process.
There are several reasons for providing an environment that allows process cooperation:
Information Sharing: Several users may be interested in same information, we must provide
an environment to allow concurrent access.
Computation Speedup: If we want a particular task to run faster, break it into subtasks each
of which will be executing in parallel with others.
Modularity: To construct a system in a modular fashion dividing the system functions into
separate processes or threads.
Convenience: Individual user may work on many tasks at same time.
Cooperating processes require an interprocess communication mechanism that will allow them to
exchange data and information. There are 2 fundamental models of interprocess communication:
1. Shared Memory
2. Message passing
Operating Systems
while the consumer is consuming another item. They must be synchronized, so the consumer does
not try to consume the item which is not yet produced.
So, 2 types of buffer can be used.
The unbounded buffer does not have limit on the size of buffer. Here consumer may have to
wait for new items but the producer can always produce new items.
The bounded buffer assumes a fixed buffer size. Here the consumer must wait if the buffer is
empty and producer must wait if buffer is full.
Message Passing Systems:
The cooperating processes communicate with each other by passing message with the help of
operating system. The message passing system is mainly used in distributed environment where
communicating processes reside in different computers connected by network.
Message passing system uses the 2 operations: send (message) and receive (message). To use these
operations a communication link must exist between the processes. The link can be implemented in
variety of ways. Several methods for logically implementing a link and send/ receive operations are
Create a mailbox
Send and receive messages through mailbox
Delete a mailbox
The process that creates mailbox is default owner. However the ownership and privilege may be
passed to other processes through appropriate system calls.
Synchronization:
Message passing may be either synchronous or asynchronous
Blocking Send: The sending process is blocked until the message is received by receiving
process or mailbox
Non-Blocking Send: Sender sends the message and resumes other operation
Blocking Receive: Receiver blocks until a message is received.
Non-Blocking Receive: Receiver retrieves either a valid message or null
Buffering:
Messages exchanged by communicating processes reside in temporary queue. Such queues can be
implemented in 3 ways
Zero capacity: Queue has maximum length of zero. The link cannot have any message
waiting in it. The sender must block until recipient receives the message. It is referred as
message system with no buffering.
Bounded capacity: The queue has finite length n; atmost n messages resides in it. If link is
not full, message is stored in queue and sender can continue other operations without
waiting. If link is full, the sender must block until space is available in queue.
Unbounded capacity: Queue’s length is infinite; any number of messages can wait in it. The
sender never blocks.
The other 2 cases are referred as systems with automatic buffering.
Multithreaded Programming
In a client server model, when a server receives a request, it creates a process to provide service.
This was the technique that was used before. Process creation is time consuming and also resource
insensitive. To overcome this overhead, threads were created for the same process. It is generally
more efficient to use one process that contains multiple threads than creating more processes.
Operating Systems
Till now we have seen the programs which were run on only single threads execution. In modern
desktop PC’s many software packages are multithreaded.
Threads consists of thread ID, register sets, stack but it shares data section and code section with
other threads belonging to same process as shown in Fig 2.10.
Economy: Allocating the memory and resources for process creation is costly. Because threads
share resources of the process to which they belong, it is more economical to create and context
switch threads.
Scalability: Multiprocessor architecture can be used efficiently by using multithreaded programs
where threads may be running in parallel on different processors.
Nowadays many cores are mounted on the chip. Each core will have a separate processor
embedded in it. In a single core system, the execution of an application having 4 threads, the
threads are interleaved over time as shown in Fig 2.13
Dividing Activities: Applications must be examined to find the areas that can be divided
into separate, concurrent tasks and thus can run in parallel on individual cores.
Balance: While identifying the task which run parallel, care must be taken each task
performs equal work on cores. Giving a core for task which doesn’t provide more value to
process may not be worth.
Data Splitting: Just like applications, the data accessed and manipulated by the tasks must
be divided to run on separate cores.
Data Dependency: The data accessed by the tasks must be examined for dependencies
between 2 or more tasks. In instances where one task depends data used by another task,
programmers must ensure that the execution of tasks is synchronized to accommodate the
data dependency.
Testing and Debugging: Testing and debugging is more difficult for concurrent programs
than single threaded applications.
Multithreading Models:
Operating Systems
Threads can be divided into user threads and kernel threads. User threads are supported above the
kernel and are managed without kernel support whereas the kernel threads are supported and
managed directly by the operating system.
But there exist a relationship between user threads and kernel threads on following models
Many to One model:
In this model, many user threads are mapped to a single kernel thread as shown in Fig 2.15. Thread
management is done by thread library in user space, so it is efficient but the entire process will
block if a thread makes a blocking system call. Here a user can create as many user threads he
wishes, but only 1 kernel thread will be created at same time. Also since only one thread can access
the kernel at a time, multiple threads are unable to run in parallel on multiprocessors. EG: In
Solaris, green threads uses this model, GNU portable Threads.
application or a particular machine. Here developer can create as many user threads as he wishes
and the corresponding kernel threads can run in parallel on a multiprocessor. This will remove the
overheads found in many to one and one to one models. EG: Solaris prior to 9, Windows NT/2000
with ThreadFiber package
Thread Cancellation is a task of terminating a thread before it has completed. A thread that is to
be cancelled is often referred to as target thread. Cancellation of target thread can occur in 2
scenarios:
process creation, it does consume some time to create. And once a thread finishes its job, it will be
deleted.
Also if for every concurrent request a thread is created then the system would be full of threads
exhausting system resources such as CPU time or memory. For these purposes a thread pool is
created.
During process start up, a number of threads are created and stored into a pool, where they sit and
wait for work. When a server receives a request, it awakens a thread from this pool if available and
passes it the request for service. Once a thread completes the service it returns back to the pool and
awaits more work. If the pool contains no available thread, the server waits until one becomes free.
Advantages:
Usually slightly faster to service a request with an existing thread than create a new thread
Allows the number of threads in the application(s) to be bound to the size of the pool
Scheduler Activations
The systems implementing many to many models and two level model place an intermediate data
structure between user and kernel threads. That structure is known as lightweight process as
shown in Fig 2.19. To the user thread library, the LWP acts as virtual processor on which the
application can schedule a user thread to run. Each LWP is attached to kernel thread and it is the
kernel thread that operating system schedules to run on physical processors. If kernel thread
blocks, LWP blocks and up the chain user thread attached to LWP is also blocked.
Kernel provides application with set of LWP’s and the application can schedule the user
threads on the available LWP.
Kernel must inform application about certain events called as upcall.
Upcalls are handled by thread library with an upcall handler which is run on LWP.
One event that triggers an upcall is when an application thread is about to block. Here
Kernel makes an upcall to application informing about the specific thread which is going to
block
Kernel allocates new virtual processor (LWP)
Operating Systems
Upcall handler runs on new LWP which saves the state of blocking thread and relinquishes
the old LWP on which the block occurred.
A new thread is scheduled by upcall handler on new LWP
When an expected event occurs in old LWP, this information is sent by kernel to application
about the old LWP being eligible to run again.
After marking the unblocked thread as eligible to run, the application schedules an eligible
thread to run on an available virtual processor (LWP).
Process Scheduling:
In single processor system, only process runs at a single point of time. Other processes must wait
until the CPU is free and can be rescheduled.
In case of multiprogramming, CPU will be busy running one process or other having high utilization.
A process is executed until it waits for some I/O operations to complete. In normal computers, CPU
sits idle during this time. In multiprogramming, several processes are kept in memory, ready for
execution, the CPU is allocated for such processes when previous process waiting for I/O to
complete.
Whenever the CPU becomes idle, the Operating system must select a process from the ready queue
for execution, this selection is carried out by a short term scheduler. The process thus selected is
allocated with the CPU for its execution.
Ready queue may not necessarily be First Come First serve, priority based or FIFO.
The scheduling algorithms can be divided into preemptive scheduling and non-preemptive
scheduling.Scheduling of process is done in the following situations
1. When a process moves from running state to waiting state (wait for I/O)
2. When a process moves running state to ready state ( in case of interrupts)
3. When a process moves from waiting state to ready state (once I/O is finished)
4. When a process terminates.
The 1st and 4th situation uses a non-preemptive scheduling, others use preemptive scheduling.
Under non-preemptive scheduling the CPU once allocated to process will be returned back only if
process is finished or if process moves to waiting state due to I/O operation. This is used in
Windows 3.x
Preemptive scheduling was 1st used in Windows 95 and later on continued in next Windows
operating system. Preemptive scheduling incurs cost associated with access to shared data.
Preemption also affects the design of operating system kernel.
A dispatcher is module which gives control of the CPU to the process selected by the short term
scheduler. The function involves
Switching Context
Switching to user mode
Jumping to proper location in user program to restart that program.
The time taken for a dispatcher to stop one process and start another running is known as dispatch
latency.
Operating Systems
Many criteria have been suggested for comparing the CPU scheduling algorithms. Those criteria are
CPU utilization: The CPU should be busy all the time by executing one or the other process.
Conceptually the utilization ranges from 0 to 100 %. In real time it ranges from 40% to
90%.
Throughput: Measure of work is the number of processes that are completes in a unit time
will give the throughput. For long process, 1 process/hour. For short transactions 10
processes/ second
Turnaround Time: It is the time taken by a process for execution. Turnaround time is the
sum of periods spent waiting to get into memory, waiting in ready queue, executing on CPU
and doing I/O.
Waiting Time: It is the time taken by process while waiting in a ready queue.
Response Time: It is time taken by a process in interactive system to respond to a request.
Response time is the time it takes to start responding not the time it takes to output the
response.
It is desirable to have high CPU utilization and throughput and minimize the turnaround time,
response time and waiting time.
Scheduling Algorithm:
The problem of deciding which process is to be allotted with the CPU is overcome by using
scheduling algorithm. The following are the various CPU scheduling algorithms
First Come First Served Scheduling
It is the simplest CPU scheduling algorithm. In this scheme, the CPU is first allocated to the process
which requested the CPU first. The FCFS policy uses FIFO queue. When a process requests for CPU,
its PCB is linked onto the tail of the FIFO queue. When the CPU is free, The CPU is allotted to the
process which is present in the head of the queue. The average waiting time for FCFS algorithm is
too long.
Example: Consider the following processes which arrive at time 0, with the length of CPU burst
given in milliseconds
P1 P2 P3
0 24 27 30
The waiting time of process P1 is 0 milliseconds, process P2 is 2 milliseconds and process P3 is 27
milliseconds. Thus average waiting time is (0+24+27)/3=17 milliseconds.
Likewise, if processes arrive in P2, P3, P1 order, then the results can be shown using Gantt chart as
P2 P3 P1
Operating Systems
0 3 6 30
The average waiting time here is (0+3+6)/3 = 3 milliseconds. Thus average waiting time varies
substantially and is not minimal.
The FCFS also leads to convoy effect in case we have a 1 CPU bound process and many I/O bound
processes. The FCFS scheduling algorithm is non-preemptive. Once the CPU has been allocated to a
process, that process will keep CPU until either during termination of itself or by requesting I/O.
FCFS cannot be used efficiently in time sharing systems as each user needs some time for its
execution.
Shortest Job First Scheduling
This algorithm associates with each process the length of the process’s next CPU burst. When the
CPU is available, it is assigned to the process which is having small CPU burst. If the next 2
processes have the same CPU burst, then FCFS algorithm is used to break the tie.
Example: Consider the following set of processes, with the length of CPU burst given in milliseconds
P4 P1 P3 P2
0 3 9 16 24
The waiting time for Process P4 is 0 milliseconds, P1 is 3 milliseconds, P3 is 9 milliseconds and P2
is 16 milliseconds.
The average waiting time is (0+3+9+16)/4=7 milliseconds.
The SJF scheduling algorithm is provably optimal. Moving a short process before a long one
decreases the waiting time of short process and increases the waiting time of long process. The
above example showed the non-preemptive SJF scheduling algorithm.
The SJF scheduling algorithm can be preemptive as well. A preemptive SJF algorithm will
preemptive the currently executing process if the newly arrived process has less CPU burst time.
Example: Consider the following 4 processes, with a length of CPU burst given in milliseconds
P1 P2 P4 P1 P3
0 1 5 10 17 26
Operating Systems
Process P started at time 0, At time 1, P2 arrives, now the remaining time for P1 and P2 are
calculated, P1 has 7 milliseconds remaining and P2 has 4 milliseconds, so the short job P2 is started.
At time 3, P3 arrives, now the remaining time is calculated where, P1=7 ms, P2=3 ms, P3= 9ms. So
P2 is continued. At time 4, P4 also arrives. The remaining time at this point is for P1=7ms, P2=1ms,
P3=9ms and P4=5ms. P2 is continued and it finishes its work at time t=5. Later the shortest of them
are checked and accordingly the processes are executed.
Priority Scheduling
A priority is associated with each process and the CPU is allocated to the process with highest
priority. Equal priority process are scheduled according to the FCFS order. A priority p is allotted to
each process, according to the value of p the processes are executed.
Priorities are indicated by some fixed range of numbers like 0 to 7 or 0 to 4095. According to the
systems used, 0 is either allocated as highest priority or lowest priority.
Example: Consider the following processes assumed to have arrived at time 0, in order P1, P2,
P3,….P5 with the length of CPU burst given in milliseconds
P2 P5 P1 P3 P4
0 1 6 16 18 19
The average waiting time is 8.2 milliseconds.
Priority scheduling can be either preemptive or non-preemptive. When a new process arrives to the
ready queue, its priority is checked with the currently executing process. A preemptive priority
scheduling algorithm will preempt the CPU if the priority of the newly arrived process is higher
than the currently running process. A non-preemptive process will simply put the new process at
the head of the queue.
A major problem with priority scheduling algorithm is indefinite blocking or starvation. A process
that is ready to run but waiting for the CPU can be termed as blocked. This algorithm can leave
some low priority process to be blocked indefinitely.
A solution to above said problem is aging. Aging is a technique of gradually increasing the priority
of processes that wait in the system for a long time. For example, if priorities range from 127 to 0
where 0 being highest priority, the priority of waiting process is increased by 1 in every 15 minutes.
Eventually a process with priority as 127 will become process with highest priority 0 in 32 hours.
Round Robin Scheduling
One of the oldest, simplest, fairest and most widely used algorithm is round robin (RR).
Operating Systems
In the round robin scheduling, processes are dispatched in a FIFO manner but are given a
limited amount of CPU time called a time-slice or a quantum.
If a process does not complete before its CPU-time expires, the CPU is preempted and given
to the next process waiting in a queue. The preempted process is then placed at the back of
the ready list.
Round Robin Scheduling is preemptive (at the end of time-slice) therefore it is effective in
timesharing environments in which the system needs to guarantee reasonable response
times for interactive users.
The only interesting issue with round robin scheme is the length of the quantum. Setting the
quantum too short causes too many context switches and lower the CPU efficiency. On the
other hand, setting the quantum too long may cause poor response time and approximates
FCFS.
In any event, the average waiting time under round robin scheduling is often quite long.
1. Each process gets a small unit of CPU time (time quantum), usually 10-100 milliseconds. After
this time has elapsed, the process is preempted and added to the end of the ready queue.
2. If there are n processes in the ready queue and the time quantum is q, then each process gets
1/n of the CPU time in chunks of at most q time units at once. No process waits more than (n-1)
q time units.
3. Performance ->q large. FIFO ->q small. q must be large with respect to context switch,
otherwise overhead is too high. Example: Process Burst Time
Example: Consider the following processes which arrive at time 0, with the length of CPU burst
given in milliseconds
P1 P2 P3 P1 P1 P1 P1 P1
0 4 7 10 14 18 22 26 30
Multilevel Queue Scheduling
A multilevel queue scheduling algorithm partitions the ready queue in several separate queues, for
instance,
In a multilevel queue scheduling, processes are permanently assigned to one queues. The processes
are permanently assigned to one another, based on some property of the process, such as Memory
size, Process priority, Process type. Algorithm choose the process from the occupied queue that has
the highest priority, and run that process either Preemptive or Non-preemptively. Each queue has
its own scheduling algorithm or policy.
Operating Systems
A new job enters queue Q0 which is served FCFS. When it gains CPU, job receives 8
milliseconds. If it does not finish in 8 milliseconds, job is moved to queue Q1.
At Q1 job is again served FCFS and receives 16 additional milliseconds. If it still does not
complete, it is preempted and moved to queue Q2.