Module 3
Module 3
Process Scheduling
Dr. Pradeep K V
Associate Professor (Sr.)
School of Computer Science and Engineering
VIT - Chennai
It is the process of removing the running task from the processor and
selecting another task for processing. It schedules a process into different
states like ready, waiting, and running.
It’s a crucial component of a multiprogramming OS. It makes use of a
variety of scheduling queues (Job Queue, Waiting Queue, Device
Queue...).
The scheduler’s purpose is to implement the virtual machine so that each
process appears to be running on its own computer to the user.
Scheduling Categories
Non-preemptive : The resource can’t be taken from a process until the
process completes execution. The switching of resources occurs when the
running process terminates and moves to a waiting state.
Preemptive : The OS allocates the resources to a process for a fixed
amount of time. During resource allocation, the process switches from
running state to ready state or from waiting state to ready state.
There are multiple states a process has to go through during execution. The
OS maintains a separate queue for each state along with the process control
blocks (PCB) of all processes. The PCB moves to a new state queue, after
being unlinked from its current queue, when the state of a process changes.
The dispatcher is done after the scheduler. It gives control of the CPU to the
process selected by the short-term scheduler. After selecting the process, the
dispatcher gives CPU to it.
Definition..!
It is a process that 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. And
decisions may take place under the following four circumstances:
1 When a process switches from the running state to the waiting state(for
I/O request or invocation of wait for the termination of one of the child
processes).
2 When a process switches from the running state to the ready state (for
example, when an interrupt occurs).
3 When a process switches from the waiting state to the ready state(for
example, completion of I/O).
4 When a process terminates.
In circumstances 1 and 4 [Non-Preemptive], 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 in circumstances 2 and 3 [Preemptive].
Dr. Pradeep K V Operating Systems 10/ 77
Preemptive and Non-Preemptive
PREEMPTIVE NON-PREEMPTIVE
Parameter
SCHEDULING SCHEDULING
CPU’s are allocated to a process,
CPU’s are allocated to a the process holds it till it
Basic
process for a limited time. completes its burst time or
switches to waiting state.
Process can be
Interrupt No Interrupt
interrupted in between.
If a process having high If a process with a long
priority frequently arrives burst time is running CPU,
Starvation in the ready queue, then later coming process
a low priority process with less CPU burst time
may starve. may starve.
Overhead Yes No
Flexibility flexible rigid
Cost cost associated no cost associated
CPU
High Low.
Utilization
Waiting
Less High
Time
Response
Less High
Time
Decisions are made by the Decisions are made by the
Decision scheduler and are based process itself and the OS
making on priority and time slice just follows the process’s
allocation instructions
Process OS has greater control over The OS has less control over
control the scheduling of processes the scheduling of processes
Round Robin and
Examples FCFS
Shortest Remaining Time First.
CPU Utilization : To make out the best use of the CPU and not to waste
any CPU cycle, the CPU would be working most of the time(Ideally 100%
of the time). Considering a real system, CPU usage should range from
40% (lightly loaded) to 90% (heavily loaded.)
Throughput : It is the total number of processes completed per unit of
time or rather says the total amount of work done in a unit of time. This
may range from 10/second to 1/hour depending on the specific processes.
Turnaround Time : It is the amount of time taken to execute a particular
process, i.e. The interval from the time of submission of the process to the
time of completion of the process(Wall clock time).
Waiting Time : The sum of the periods spent waiting in the ready queue
amount of time a process has been waiting in the ready queue to acquire
get control on the CPU.
Load Average : It is the average number of processes residing in the
ready queue waiting for their turn to get into the CPU.
Response Time : Amount of time it takes from when a request was
submitted until the first response is produced. Remember, it is the time
till the first response and not the completion of process execution(final
response).
Dr. Pradeep K V Operating Systems 15/ 77
CPU Scheduling Algorithms - FCFS I
In the "First come first serve" scheduling algorithm, as the name suggests, the
process which arrives first, gets executed first, or we can say that the process
which requests the CPU first, gets the CPU allocated first. (FIFO - QUEUE).
Characteristics of FCFS
It supports non-preemptive and preemptive CPU scheduling algorithms.
Tasks are always executed on a First-come, First-serve concept. FCFS is
easy to implement and use. [Using Queue Data Structure]
It is not much efficient in performance, and the wait time is quite high.
Advangtage of FCFS
Easy to implement in FCFS Order
DisAdvangtage of FCFS
FCFS suffers from Convoy effect.
The average waiting time is much higher than the other algorithms.
It is very simple and easy to implement, but not much efficient.
Consider the below example, where the processes are arrived in the order:
P1 , P2 , P3
Process Burst Time
P1 24
P2 3
P3 3
A Gantt chart is a commonly used graphical depiction of a Process schedule
Consider the below example, where the processes are arrived in the order:
P1 , P2 , P3
Process Burst Time
P2 3
P3 3
P1 24
A Gantt chart is a commonly used graphical depiction of a Process schedule
It works on the process with the shortest burst time or duration first. And
minimizes waiting time and used in Batch Systems.
It is both Preemptive and Non-Preemptive.
To successfully implement it, the burst time/duration time of the
processes should be known to the processor in advance, which is
practically not feasible all the time.
It is optimal if all the jobs/processes are available at the same time.
(either Arrival time is 0 for all, or Arrival time is same for all)
Advantages of SJF:
It reduces the average waiting time thus, it is better than the first come
first serve scheduling algorithm.
It is generally used for long term scheduling
Disadvantages of SJF:
One of the demerit SJF has is starvation.
Many times it becomes complicated to predict the length of the upcoming
CPU request
Non Preemptive SJF : Consider the below processes available in the ready
queue for execution, with arrival time as 0 for all and given burst times.
Process Burst Time
P1 21
P2 3
P3 6
P4 2
Preemptive SJF : Here, jobs are put into ready queue as they arrive, but as
a process with short burst time arrives, the existing process is preempted or
removed from execution, and the shorter job is executed first.
Process Burst Time Arrival Time
P1 8 0
P2 4 1
P3 9 2
P4 5 3
It works based on the priority of a process [ i.e, "The larger the burst
time the lower is the priority of that process."], and Preemptive and
Non-Preemptive.
A priority number (integer) is associated with each process
Characteristics of Priority Scheduling:
Schedules tasks based on priority.
When the higher priority work arrives while a task with less priority is
executed, the higher priority work takes the place of the less priority one
and the latter is suspended until the execution is complete.
Lower is the number assigned, higher is the priority level of a process.
Advantages of Priority Scheduling:
The average waiting time is less than FCFS and Less complex
Disadvantage of Priority Scheduling:
Problem "Starvation" : low priority processes may never execute
Solution "Aging" : as time progresses increase the priority of the process
Consider the below table fo processes with their respective CPU burst times
and the priorities.
Process Burst Time Priority Time
P1 21 2
P2 3 1
P3 6 4
P4 2 3
Consider the below table fo processes with their respective CPU burst times
and the priorities.
Process Burst Time Priority Time
P1 10 3
P2 1 1
P3 2 4
P4 1 5
P5 5 2
Important Terms
Completion Time : is the time, any process completes its execution.
Turn Around Time : Completion Time – Arrival Time
Waiting Time : Turn Around Time – Burst Time
Note: If arrival time is not given, then consider it as 0. (For any Problem)
Advantages
A fair amount of CPU is allocated to each job.
Because it doesn’t depend on the burst time, it can truly be implemented
in the system.
It is not affected by the convoy effect or the starvation problem as
occurred in First Come First Serve CPU Scheduling Algorithm.
Disadvantages
Low Operating System slicing times will result in decreased CPU output.
Round Robin CPU Scheduling approach takes longer to swap contexts.
Time quantum has a significant impact on its performance.
The procedures cannot have priorities established.
Consider the below Example-2 , where the time quantum of the system is 4
units.
Process ID Arrival Time Burst Time
1 0 5
2 1 6
3 2 3
4 3 1
5 4 5
6 6 4
Generate the Gantt Chart and verify with the below table table.
Process Arrival Burst Completion Turn Around Waiting
ID Time Time Time Time Time
1 0 5 17 17 12
2 1 6 23 22 16
3 2 3 11 9 6
4 3 1 12 9 8
5 4 5 24 20 15
6 6 4 21 15 11
Apply the Round Robin CPU Scheduling Algorithm to the Example-3 given
below, to determine the Average Waiting Time and Average Turn Arround
Time for the Time Slice (Quantum): 5 ms, 4 ms, and 2 ms, respectively.
Additionally, create a Gantt chart for the same.
Apply the Round Robin CPU Scheduling Algorithm to the Example-4 & 5
given below, to determine the Average Waiting Time and Average Turn
Arround Time for the Time Slice (Quantum): 5 ms, 4 ms, and 2 ms,
respectively. Additionally, create a Gantt chart for the same.
Dead Locks
Dr. Pradeep K V
Associate Professor (Sr.)
School of Computer Science and Engineering
VIT - Chennai
Every process needs some resources to complete its execution. However, the
resource is granted in a sequential order.
The process requests for some resource.
OS grant the resource if it is available otherwise let the process waits.
The process uses it and release on the completion.
A Deadlock is a situation where each of the process waits for a resource which
is being assigned to some another process. In this situation, none of the
process gets executed since the resource it needs, is held by some other process
which is also waiting for some other resource to be released.
Facts
If Graph(G) contains no cycles −→ No Deadlock
If Graph(G) contains a cycle :
if only one instance per resource type, then deadlock
if several instances per resource type, possibility of deadlock
Deadlock Ignorance: It is the most widely used(OS) approach among all the
mechanism. Here, the OS assumes that deadlock never occurs. It simply
ignores deadlock.
It is best suited for a single end user system, where User uses the system only
for browsing and all other normal stuff.
In these types of systems, the user has to simply restart the computer in the
case of deadlock. Windows and Linux are mainly using this approach.
However, if one of the table’s legs is broken, the table will undoubtedly fall. In
the case of deadlock, we can avoid it if we can breach one of the four
required conditions and do not allow them to occur simultaneously.
Hold and Wait : !(Hold and wait) = !hold or !wait (the negation of hold
and wait is that either one doesn’t hold or they don’t wait)
Circular Wait : occurs when one or more processes wait in a circular order
for the resources they require. And can be solved by assigning a priority
number to each resource.
A resource with a lower priority value cannot be requested by the process.
It ensures that no process can demand a resource that is already in use by
another. As a result, no cycle will form.
The OS checks whether the system is in safe state or in unsafe state at every
step which the operating system performs. The process continues until the
system is in safe state. Suppose, if the system moves to unsafe state, then the
OS has to backtrack one step and reallocate resource.
or
The OS reviews each allocation so that the allocation doesn’t cause the
deadlock in the system.
A state of the system is called safe if the system can allocate all the resources
requested by all the processes without entering into deadlock.
If the system cannot fulfill the request of all processes then the state of the
system is called unsafe.
The key of Deadlock avoidance approach is when the request is made for
resources then the request must only be approved in the case if the resulting
state is also a safe state.
Max[n,m] : If Max [i,j] = ’k’, then process Pi may request at most ’k’
instances of resource type Rj
Safety Algorithm
1 Let Work and Finish be vectors of length ’m’ and ’n’, respectively.
- Work = Available;
- Finish[i] = false, where ’i’ = 1,2,3,....n;
3 Compute
- Work = Work + Allocationi
- Finish[i] = true
Goto Step 2.
4 If Finish [i] == true for all i, then the system is in a Safe state
Let Requesti , is a request vector for process Pi . If Requesti [j] = ’k’, then
process Pi wants ’k’ instances of resource type Rj .
1 If Requesti ≤ Needi go to step 2. Otherwise, raise error condition, since
process has exceeded its maximum claim
2 If Requesti ≤ Available, go to step 3. Otherwise Pi must wait, since
resources are not available
3 Pretend to allocate requested resources to Pi by modifying the state as
follows:
Available = Available - Requesti ;
Allocationi = Allocationi + Requesti ;
Needi = Needi - Requesti ;
If safe =⇒ the resources are allocated to Pi
If unsafe =⇒ Pi must wait, and the old resource-allocation state is
restored
Need
Process
ABC
P1 743
P2 122
P3 600
P4 011
P5 431
Applying the Banker’s Algorithm [Safety Algorithm]: to find the safe state
sequence
Let us consider the following snapshot for understanding the banker’s algorithm:
Let us consider the following snapshot for understanding the banker’s algorithm:
Allocation Max
Process
Matrix Matrix
X Y Z X Y Z
P0 0 0 1 8 4 3
P1 3 2 0 6 2 0
P2 2 1 1 3 3 3
There are 3 units of type X, 2 units of type Y and 2 units of type Z still
available. The system is currently in a safe state. Consider the following
independent requests for additional resources in the current state:
Req1: P0 requests 0 units of X, 0 units of Y and 2 units of Z
Req2: P1 requests 2 units of X, 0 units of Y and 0 units of Z
Can the requests be granted immediately?
In order to avoid deadlocks, the operating system examines the system for any
deadlocks on a regular basis. The OS will recover from the deadlock using
recovery mechanisms after it has been discovered.
The OS’s primary responsibility is to detect deadlocks. With the help of the
resource allocation graph, the OS can detect deadlocks.
1 Let Work and Finish be vectors of length ’m’ and ’n’, respectively.
- Work = Available;
- if Requesti = 0, then Finish[i] = true;
otherwise, Finish[i]= false,
where ’i’ = 1,2,3,....n;
3 Compute
- Work = Work + Allocationi
- Finish[i] = true
Goto Step 2.
4 If Finish [i] == false for all i, then the system is in a Deadlock state
1 In this, Work = [0, 0, 0] & Finish = [false, false, false, false, false]
2 i=0 is selected as both Finish[0] = false and [0, 0, 0]<=[0, 0, 0].
3 Work =[0, 0, 0]+[0, 1, 0] =>[0, 1, 0] & Finish = [true, false, false, false,
false].
4 i=2 is selected as both Finish[2] = false and [0, 0, 0]<=[0, 1, 0].
5 Work =[0, 1, 0]+[3, 0, 3] =>[3, 1, 3] & Finish = [true, false, true, false,
false].
6 i=1 is selected as both Finish[1] = false and [2, 0, 2]<=[3, 1, 3].
7 Work =[3, 1, 3]+[2, 0, 0] =>[5, 1, 3] & Finish = [true, true, true, false,
false].
Dr. Pradeep K V Operating Systems 75/ 77
Deadlock Detection and Recovery IV
8 i=3 is selected as both Finish[3] = false and [1, 0, 0]<=[5, 1, 3]. Work
=[5, 1, 3]+[2, 1, 1] =>[7, 2, 4] & Finish = [true, true, true, true, false].
9 i=4 is selected as both Finish[4] = false and [0, 0, 2]<=[7, 2, 4].
10 Work =[7, 2, 4]+[0, 0, 2] =>[7, 2, 6] & Finish = [true, true, true, true,
true].
11 Since Finish is a vector of all true it means there is no deadlock in this
example.
How to Detect Recover ?