PROCESS SCHEDULING
Monalisa Mishra
Assistant Professor, CSE
C.V Raman Global University, Bhubaneswar
Scheduling Algorithms Type
Scheduling Algorithm
Preemptive Non-Preemptive
A scheduler may suspend a A process cannot be suspended
running process anytime until it completes its execution.
When an advertise comes during watching your Girl watching her favourite show in OTT
favourite show platform
First Come First Serve(FCFS) Scheduling Algorithm
Jobs are executed on first come, first serve
basis.
It is a Non-Preemptive scheduling algorithm.
Easy to understand and implement.
Its implementation is based on FIFO queue.
Poor in performance as average wait time is
high.
May suffer from Convoy effect
Proces Arrival Execution Time
s Time
FIFO queue P1 0 24
P2 0 3
P3 0 3
Short Term P4 0 1
READY P1 P2 P3 P4
P P QUEUE
P P P Scheduler Gantt
5 4 3 2 1P2 0 2 2 3 3 Chart
4 7 0 1
Waiting time for P1 = 0; P2 = 24; P3 = 27; P4=30
Average waiting time: (0 + 24 + 27+30)/4 = 20.25
Example of FCFS Scheduling
Consider the following set of processes that arrive
at time 0 with the length of the CPU burst time in
milliseconds:
Process Burst Time (in milliseconds)
P1 24
P2 3
P3 3
4
Example of FCFS Scheduling
Suppose that the processes arrive in the order:
P 1, P 2, P 3. P 24
1
The Gantt Chart for the schedule is: P2 3
P3 3
P1 P2 P3
0 24 27 30
Waiting Time for P1 = 0 milliseconds
Waiting Time for P2 = 24 milliseconds
Waiting Time for P3 = 27 milliseconds
5
Example of FCFS Scheduling
Average Waiting Time = (Total Waiting Time) /
No. of Processes
= (0 + 24 + 27) / 3
= 51 / 3
= 17 milliseconds
6
Example of FCFS Scheduling
Suppose that the processes arrive in the order:
P 2 , P 3, P 1.
The Gantt chart for the schedule is:
P2 P3 P1
0 3 6 30
Waiting Time for P2 = 0 milliseconds
Waiting Time for P3 = 3 milliseconds
Waiting Time for P1 = 6 milliseconds
7
Example of FCFS Scheduling
Average Waiting Time= (Total Waiting Time) /
No. of Processes
= (0 + 3 + 6) / 3
=9/3
= 3 milliseconds
Thus, the average waiting time depends on the
order in which the processes arrive.
8
Shortest Job First (SJF) Scheduling Algorithm
Shortest Job First scheduling works on the
process with the shortest burst time
This is a non-preemptive, pre-emptive
scheduling algorithm.
Best approach to minimize waiting time.
The processer should know in advance
how much time process will take.
2 20
I need full
Min Min
1 hr. A
s s
chapter to
Ma’
Ma’
m
complete Proces Arrival Execution Time
m Ma’m
s Time
P1 0 6
P2 0 8
Short Term P3 0 7
READY
P QUEUE
P P P Scheduler P4 0 3
P4 P1 P3 P2
4 3 2 1P2 0 3 9 1 2
6 4
Waiting time for P4 = 0; P1 = 3; P3 = 9; P2=16
Average waiting time: (0 + 3 + 9+16)/4
=7
Shortest Job First Scheduling
(SJF)
In SJF, the process with the least estimated execution
time is selected from the ready queue for execution.
It associates with each process, the length of its next
CPU burst.
When the CPU is available, it is assigned to the
process that has the smallest next CPU burst.
If two processes have the same length of next CPU
burst, FCFS scheduling is used.
SJF algorithm can be preemptive or non-preemptive.
10
Example of Non-Preemptive SJF
Consider the following set of processes that arrive
at time 0 with the length of the CPU burst time in
milliseconds:
Process Burst Time (in milliseconds)
P1 6
P2 8
P3 7
P4 3
11
Example of Non-Preemptive SJF
P1 6
The Gantt Chart for the schedule is:
P2 8
P4 P1 P3 P2 P3 7
P4 3
0 3 9 16 24
Waiting Time for P4 = 0 milliseconds
Waiting Time for P1 = 3 milliseconds
Waiting Time for P3 = 9 milliseconds
Waiting Time for P2 = 16 milliseconds
12
Example of Non-Preemptive SJF
Average Waiting Time = (Total Waiting
Time) /
No. of Processes
= (0 + 3 + 9 + 16 ) / 4
= 28 / 4
= 7 milliseconds
13
Example of Preemptive SJF
Consider the following set of processes. These
processes arrived in the ready queue at the times
given in the table:
Process Arrival Time Burst Time (in milliseconds)
P1 0 8
P2 1 4
P3 2 9
P4 3 5
14
Example of Preemptive SJF
The Gantt Chart for the schedule is:
P1 P2 P4 P1 P3
0 1 5 10 17 26
Waiting Time for P1 = 10 – 1 – 0 = 9
Waiting Time for P2 = 1 – 1 = 0
P AT BT
Waiting Time for P3 = 17 – 2 = 15 P1 0 8
P2 1 4
Waiting Time for P4 = 5 – 3 = 2 P3 2 9
P4 3 5
15
Example of Preemptive SJF
Average Waiting Time= (Total Waiting Time) /
No. of Processes
= (9 + 0 + 15 + 2) / 4
= 26 / 4
= 6.5 milliseconds
16
Explanation of the Example
Process P1 is started at time 0, as it is the only
process in the queue.
Process P2 arrives at the time 1 and its burst time
is 4 milliseconds.
This burst time is less than the remaining time of
process P1 (7 milliseconds).
So, process P1 is preempted and P2 is scheduled.
17
Explanation of the Example
Process P3 arrives at time 2. Its burst time is 9
which is larger than remaining time of P2 (3
milliseconds).
So, P2 is not preempted.
Process P4 arrives at time 3. Its burst time is 5.
Again it is larger than the remaining time of P2
(2 milliseconds).
So, P2 is not preempted.
18
Explanation of the Example
After the termination of P2, the process with
shortest next CPU burst i.e. P4 is scheduled.
After P4, processes P1 (7 milliseconds) and then
P3 (9 milliseconds) are scheduled.
19
Priority Scheduling Algorithm
Let’s See it
A priority number (integer) is associated
with each process
The CPU is allocated to the process with
the highest priority (smallest integer highest
priority)
It could be of either Preemptive or Non-
preemptive
Note that SJF is a priority scheduling
where priority is the predicted next CPU burst
FIFO queue time
Problem Starvation – low priority processes
may never execute
Solution Aging – as time progresses
increase the priority of the process.
Recruiter
Priority Scheduling
In priority scheduling, a priority is associated with
all processes.
Processes are executed in sequence according
to their priority.
CPU is allocated to the process with highest
priority.
If priority of two or more processes are equal than
FCFS is used to break the tie.
21
Priority Scheduling
Priority scheduling can be preemptive or non-
preemptive.
Preemptive Priority Scheduling:
In this, scheduler allocates the CPU to the new process
if the priority of new process is higher than the priority
of the running process.
Non-Preemptive Priority Scheduling:
The running process is not interrupted even if the new
process has a higher priority.
In this case, the new process will be placed at the head
of the ready queue.
22
Priority Scheduling
Problem:
In certain situations, a low priority process can be
blocked infinitely if high priority processes arrive in
the ready queue frequently.
This situation is known as Starvation.
23
Priority Scheduling
Solution:
Aging is a technique which gradually increases the
priority of processes that are victims of starvation.
For e.g.: Priority of process X is 10.
There are several processes with higher priority in
the ready queue.
Processes with higher priority are inserted into
ready queue frequently.
In this situation, process X will face starvation.
24
Priority Scheduling
(Cont.):
The operating system increases priority of a
process by 1 in every 5 minutes.
Thus, the process X becomes a high priority
process after some time.
And it is selected for execution by the scheduler.
25
Example of Priority Scheduling
Consider the following set of processes that arrive
at time 0 with the length of the CPU burst time in
milliseconds. The priority of these processes is
also given:
Process Burst Time Priority
P1 10 3
P2 1 1
P3 2 4
P4 1 5
P5 5 2
26
Example of Priority Scheduling
The Gantt Chart for the schedule is:
P2 P5 P1 P3 P4
0 1 6 16 18 19
Waiting Time for P2 = 0 P BT Pr
P1 10 3
Waiting Time for P5 = 1 P2 1 1
P3 2 4
Waiting Time for P1 = 6
P4 1 5
Waiting Time for P3 = 16 P5 5 2
Waiting Time for P4 = 18
27
Example of Priority Scheduling
Average Waiting Time = (Total Waiting Time) /
No. of Processes
= (0 + 1 + 6 + 16 + 18) / 5
= 41 / 5
= 8.2 milliseconds
28
Another Example of Priority
Scheduling
Processes P1, P2, P3 are the processes with their
arrival time, burst time and priorities listed in table
below:
Process Arrival Time Burst Time Priority
P1 0 10 3
P2 1 5 2
P3 2 2 1
29
Another Example of Priority
Scheduling
The Gantt Chart for the schedule is:
P1 P2 P3 P2 P1
0 1 2 4 8 17
Waiting Time for P1 = 0 + (8 – 1) = 7
Waiting Time for P2 = 1 + (4 – 2) = 3
P AT BT Pr
Waiting Time for P3 = 2
P1 0 10 3
P2 1 5 2
P3 2 2 1
30
Another Example of Priority
Scheduling
Average Waiting Time = (Total Waiting
Time) /
No. of Processes
= (7 + 3 + 2) / 3
= 12 / 3
= 4 milliseconds
31
Round Robin Scheduling (RR)
In Round Robin scheduling, processes are
dispatched in FIFO but are given a small amount
of CPU time.
This small amount of time is known as Time
Quantum or Time Slice.
A time quantum is generally from 10 to 100
milliseconds.
32
Round Robin Scheduling (RR)
If a process does not complete before its time
slice expires, the CPU is preempted and is given
to the next process in the ready queue.
The preempted process is then placed at the tail
of the ready queue.
If a process is completed before its time slice
expires, the process itself releases the CPU.
The scheduler then proceeds to the next process
in the ready queue.
33
Round Robin Scheduling (RR)
Round Robin scheduling is always preemptive as
no process is allocated the CPU for more than
one time quantum.
If a process’s CPU burst time exceeds one time
quantum then that process is preempted and is
put back at the tail of ready queue.
The performance of Round Robin scheduling
depends on several factors:
Size of Time Quantum
Context Switching Overhead
34
Example of Round Robin
Scheduling
Consider the following set of processes that arrive
at time 0 with the length of the CPU burst time in
milliseconds:
Process Burst Time
P1 10
P2 5
P3 2
Time quantum is of 2 milliseconds.
35
Example of Round Robin
Scheduling
The Gantt Chart for the schedule is:
P1 P2 P3 P1 P2 P1 P2 P1 P1
0 2 4 6 8 10 12 13 15 17
Waiting Time for P1 = 0 + (6 – 2) + (10 – 8) + (13 – 12)
= 4 + 2 + 1 = 7
Waiting Time for P2 = 2 + (8 – 4) + (12 – 10)
P BT
= 2 + 4 + 2 = 8 P1 10
P2 5
Waiting Time for P3 = 4
P3 2
36
Example of Round Robin
Scheduling
Average Waiting Time= (Total Waiting Time) /
No. of Processes
= (7 + 8 + 4) / 3
= 19 / 3
= 6.33 milliseconds
37
Multi-Level Queue Scheduling
(MLQ)
Multi-Level Queue scheduling classifies the
processes according to their types.
For e.g.: a MLQ makes common division between
the interactive processes (foreground) and the
batch processes (background).
These two processes have different response
times, so they have different scheduling
requirements.
Also, interactive processes have higher priority
than the batch processes.
38
Multi-Level Queue Scheduling
(MLQ)
In this scheduling, ready queue is divided into
various queues that are called subqueues.
The processes are assigned to subqueues,
based on some properties like memory size,
priority or process type.
Each subqueue has its own scheduling algorithm.
For e.g.: interactive processes may use round
robin algorithm while batch processes may use
FCFS.
39
Multi-Level Queue Scheduling
(MLQ)
40
Multi-Level Feedback Queue
Scheduling (MFQ)
Multi-Level Feedback Queue scheduling is an
enhancement of MLQ.
In this scheme, processes can move between
different queues.
The various processes are separated in different
queues on the basis of their CPU burst times.
If a process consumes a lot of CPU time, it is placed
into a lower priority queue.
If a process waits too long in a lower priority queue, it
is moved into higher priority queue.
Such an aging prevents starvation.
41
Multi-Level Feedback Queue
Scheduling (MFQ)
42
Multi-Level Feedback Queue
Scheduling (MFQ)
The top priority queue is given smallest CPU time
quantum.
If the quantum expires before the process terminates,
it is then placed at the back of the next lower queue.
Again, if it does not complete, it is put to the last
priority queue.
The processes in this queue runs on FCFS
scheduling.
If a process becomes a victim of starvation, it is
promoted to the next higher priority queue.
43
THANK YOU
44