Scheduling Algorithms
Scheduling Algorithms
knowledge
TECHNICAL PUBLICA TIONs an up-thrust for
Introduction to Operating Systems 2-26 Process Management
When new process enters into the system, its process control block is
linked to the
end of the ready queue and it is removed from the front of
the queue.
When a process waits or blocks, it is removed
from the queue and it queues up
again inFCFS queue when it gets ready.
FCFS is non-preemptive CPU scheduling algorithm. It
Out method (FIFO). is also called First In First
FCFS is simple to implement because it
for most of the batch
uses a FIFO queue. This algorithm is
operating systems. fine
FCFS is not useful in scheduling
interactive processes because it cannot
short response time.This algorithm guarantee
short ones. performs much better for long processes than
Turnaround time is unpredictable with the FCFS
of the FCFS is often quite algorithm. Average waiting time
long.
Real life analogy is buying
tickets.
Example 2.6.1 Consider the following
set of process that
arrives at time 0, with the length of the Process Burst time
CPU burst
given in milliseconds
PË 14
Calculate the average waiting time.
P2 2
P3 4
Solution:
Gantt chart :
P1
P2 P3
14 16
20
TECHNICAL PUBLICATIONS® - an
up-thrust for knowledge
2- 27 Process Management
Introduction to Operating Systems
P;
14
P2
16
P3
6
P
P
P3
waiting time
Sum of P, P and Pa
Average waiting time 3
6+0+2
= 2.66
3
waiting
Convey effect bound processes will be
I/0
device utilization, all effect.
To reduce I/O ones. This is called as convey
processor bound
excessively long for
extent of its overall effect on system
monopolizes the system,
the is
" If one process scheduling policy and whether the process
performance depends on the
I/0bound.
processor bound or
Disadvantages
1. Waiting time depends on arrival order
2. Convoy effect : short process stuck waiting for long process
3 Also called head of the line blocking
Example 2.6.2 Consider the folloving set of process
Process Burst time
that arrive at time 0, with the length of CPU
burst given in nilliseconds. Calculate the average P 4
following order : P3 3
a. P, P, P3, Py Ps PA 3
Provide the Gantt chart for the Same.
P5 5
Solution :
Gantt chart :
P P2 P3 PA P5
4 1 14 17 22
P2 4 0 = 4
P3 11 0= 11
PA 14 0 = 14
P's 17 0 = 17
0+4+11+14 +17 46
Average waiting time 5
= 9.2
5
0 + 4 = 4
P
4 +7 = 11
Pzws
11 +3 = 14
P3
14 + 3 = 17
P4
17 + 5 = 22
Ps
4+11+14+17+22 68 = 13.60
Average turnaround time 5 5
P3 6
P4 4
Solution :
Gantt chart:
P2 Pa P4 P3
2 11 17
Waiting time :
P 0-0 = 0
P3 11 -0 = 11
2-0= 2
6+0+11+2 19
Average waiting time = 4 4
= 4.75
P; 6 +5 = 11
P2 0 +2 =2
11 + 6 = 17
P3
2 + 4 = 6
PA
11+2+17+6 36
Average turnaround time =
= 9
4 4
TECHNICAL
PUBLICATIONs an up-thrust for knowledge
Introduction to Operating Systems 2-31 Process Management
The SJF scheduling algorithm is optimal only when all of the processes are
available at the same time and the CPUestimates are available.
sJF scheduling algorithm may be preemptive or non-preemptive.
2.6.3 Priority Scheduling
algorithm. It
Priority CPU scheduling algorithm is preemptive and non-preemptive
system. Priority can be
batch
is one of the most common scheduling algorithm in
process.
assigned by a system admin using characteristics of the
first. If the priority
In this scheduling algorithm, CPUselect higher priority process
applied for solving the
scheduling algorithm is
of two process is same then FCFS
problem.
quickly its request for a CPU will
be
process determines how
The priority of a
granted if other processes make competing requests.
scheduling.
process is assigned a priority number for the purpose of CPU
Each
non negative integer number.
The priority number is normally a
priority scheduling algorithm will simply put the new process at
Non preemptive
the head of the ready queue.
algorithm will preempt the CPU if the priority of
Preemptive priority scheduling priority of the currently running
process is higher than the
the newly arrived
process.
arrive at time 0, with the length
of
following set of process that
Example 2.6.4 Consider the average wvaiting time and average
milliseconds. Calculate the
CPU burst given in slice = 2)
Provide the Gantt chart for the sane. (Time
turnaround time.
Burst time Priority
Process
3
P;
4
2
P
6 1
P3
4
P4
P3 P4 P P2
6 10 15 17
Waiting time :
Process Waiting time
10 --0 = 10
P,
15 - 0 = 15
P2
P3 0 -0 = 0
P4 6-0 =6
10+15+0+6 31
Average waiting time 4 4
= 7.75
P 10 +5 = 15
P2 15 + 2 = 17
P, 0 + 6 =6
P4 6 + 4 = 10
Example 2.6.5 Consider the following set of process that arrive at tine 0, with the length
of
CPU burst given in milliseconds. Calculate the average waiting time and average
turnaround time. Provide the Gantt chart for the same. (Time slice = 2)
Process Burst time
5
P1
P2
6
P3
4
P4
Solution :
Gantt chart :
PA P P3
P P3 PA P P3
P1 17
12 14 15
10
0 2
Waiting time :
Waiting time
Process
0-0 + 8- 2+ 14
- 10 = 10
PË
2-0 =2
P2
+ 15 - 12 = 11
4 -0 + 10 - 6
Pz
6-0 + 12 - 8 = 10
P4
10+2+11+10 33 = 8.25
time = 4
Average waiting 4
time
time + Burst
Turnaround time = Waiting
Turnaround time :
Turnaround time
Process
10 + 5 = 15
P
2 +2 = 4
P
11 + 6 = 17
P'3
10 + 4 = 14
P4
15+4 +17+14 50
time = = 12.5
Average turnaround 4 4
3 Response time may be high. Provides good response time for short
processes.
order at time 0.
Processes are arrived in P, P, Pa Pa, P, non-preemptive
execution using FIFO, SJF,
to show
(i) Drawfour Gantt charts (Quantum = 1) scheduling.
Robin scheduling algorith1m.
priority and Round time for each
time and turnaround Marks 15
Also calculate waiting AU : May-18,
(ii)
Solution :
chart :
FIFO : Gantt
P4 P5
P2 P3
19
P1 13 14
11
10
Turnaroud time for FIFO
FIFO
Waiting time for
Process 0+ 10 = 10
10 + 1 = 11
P1 10
11 + 2 = 13
P2 11
P3 13 + 1 = 14
13
14 +.5 = 19
14
Ps
Sum of all processes waiting time
Average waiting time 5
0 + 10 + 11 + 13 + 14
= 9.6
5
2) SJF
P2 PA P3 Ps P,
0 1 2 9 19
3) Non-Preemptive Priority
Gantt chart : (1 is highest
priority and 4 is lowest priority)
P2 P5 P4
1
P3 P4
16 18 19
Process
Waiting time for
Non-Preemptive Priority Turnaroud time for
P 6
Non-Preemptive Priority
6 + 10 = 16
P
0 + 1 = 1
P3 16
16 + 2 = 18
PA 18
18 + 1 = 19
P5
1+5 = 6
TECHNICAL PUBLICATIONS - an up-thrust for knowledge
Introduction to Operating Systems 2-37 Process Management
6+0 + 16 + 18 + 1
= 8.2
5
16 + 1 + 18 + 19 + 6 = 13.2
5
P, P1 P4 P4
P5 P1 P5 P. Ps
Pa P5 P1 P3 Ps P
P. P P3 19
15 16 17 18
11 12 13 14
7 10
3 4 5 6
1 2
P'5
Sum of allprocesses waiting
time
Average waiting
time =
9 + 1+ 3 + 3 + 9
5
turnaround time
Sum of all processes
turnaround time =
Average
19 + 2 +5 + 4 + 14 = 8.8
5
P 10 3
P 1 1 1
P 2 3 2
P 4 1
P, P P4
0
P3 Ps
10 11 12 14 19
ii) SJF
P1 P2 P4
0 Pa P5
10 11 12 14 19
ii) SRTF (Gantt chart)
P P2 PA P3
1 2
P5 P1
3 5
10
19
iv) Preemptive priority
P, P2 Ps P1
0 1 2 7 P3 P4
16 18 19
v) Non-preemptive priority
P, P2 Ps P3 P4
10 11
16 18 19
Waiting time :
Round Robin
FCFS SJF SRFT Preemptive Non-preemptiv
Process
Priority e Priority
0-0+8-2+12-1
0-0+7 0-0 =0
0-0 = 0 0-0 = 0 0-0 +10 - 1 0 + 15- 14 = 9
= 9 -1=6
2-1 = 1
1-1 = 0 10 - 1=9
10-1 = 9 10 -1 =9 1-1= 0
P 4-2 = 2
16-2 14 16 - 2 = 14
12 -2 = 10 12-2 =10 3 -2 = 1
P 3-1= 2
18-1 17 18 - 1 = 17
2 - 1 = 1
P 11 -1= 10 11-1= 10
11 -2 =9 6-2+10 8
5-2 = 3 2-2 = 0 = 8
14-2 = 12 14-2 = 12 + 14-12
Turnaround time
Round
Preemptive Non-preemptive
SRTF Robin
FCFS SJF Priority Priority
10 + 0 = 10 10 +9 = 19
10 +9 = 19 10+6 = 16
10 +0 10
10 + 0= 10 1+1= 2
1 +9 = 10
1 +0 =1 1+0 =1
1+9 = 10 1+9 = 10
2 + 14 =16 2+2 = 4
2 +1= 3 2+ 14 = 16
2+ 10 = 12 2+ 10 = 12
1+ 17 = 18 1+2= 3
1+1= 2 1+17 =18
1+ 10 = 11 1+ 10=11
P 5+9 = 14 5+8 = 13
5 +3 = 8 5+0 =5
5+ 12 = 17 5+ 12 = 17
turnaround time
Average waiting time and
Average Turnaround time
Averege Waiting time
Advantages :
1. Very. short processes get very good service.
in most cases
The penalty ratios are small; this algorithm works extremely well
highest throughput of all scheduling
3 This algorithm provably gives the correct.
if the estimates are exactly
algorithms
Scheduling
Queue
2.6.8 Multilevel
not separate scheduling algorithm but it works in conjunction
Multilevel queue is
scheduling algorithms. The processes can be grouped according
with several CPU
to common characteristics.
different
queue is based on the priority based system with
One type of multilevel
level.
queues for each priority Interactive and batch.
types of processes or jobs :
System consists of two longer. Fig. 2.6.1 shows multilevel
and batch jobs are
Interactive jobs are shorter
queue scheduling.
Ihe scheduler now has to decide which queue to run. The methods are as
follows :
I. Higher priority queues can be processed until they are empty before the lowex
priority queues are executed.
2. Each queue can be given a certain amount of the CPU. Maybe, the interactive
queue could be assigned 75 % of the CPU, with the batch queue being given
25 %.
" It should also be noted that there can be many other queues. Many systems will
have a system queue. This queue will contain processes that are important to keep
the system running. For this reason these processes normally have the highest
priority.
It should be also need to specify which queue a process will be put to when it
arrives to the system and,/or when it starts a new CPUburst.
Advantages :
1. It can be preemptive or non-preemptive.
2. Short CPUbound processes are executed first.
3. Simple to implement.
Disadvantage :
1 Queues require
monitoring, which is a costly activity.
2.6.9 Multilevel Feedback Queue Scheduling
Multilevel Feedback Queue (MLFQ) allows moving the
processes between the
queues. MLFQ has a number of distinct queues; each assigned a
level. At any different priority
given time, a process that is ready to run is on a
MLFQ uses priorities to decide which process should single queue.
run at a given time. A
process with higher priority is selected for execution.
Consider processes with different CPU burst
much of the CPUit will be moved to a lower
characteristics. If a process uses too
priority queue. This will leave I/0
bound and interactive processes in the higher
priority queue.
Fig. 2.6.2 shows multilevel feedback queue
scheduling. (See Fig. 2.6.2 on next
page.)
MLFQ also uses concept of aging to prevent starvation.
The processes with lower
priority will move towards the higher priority queue in
aging
MLFQ scheduling lies in how the scheduler sets priorities. concept. The key to
Rather than giving a
fixed priority to each process, MLFQ varies the priority of a process
observed behavior.
based on its
Queue
Queue
Queue
(CPU} Exit
Queue
CPU) + Exit
OO0--0
scheduling
feedback queue
Fig. 2.6.2 Multilevel
Q0 is the lowest priority queue
Q1 and 02).
Assume we have
three queues (Q0, enters at the highest priority
process
highest priority queue. The reduces the process's
priority
and Q2 is the the scheduler is
single time-slice of 10 ms, for a time slice, the job
(Q2). After a After running at
Q1
the job is on Q1. it remains.
by one, and
thus
in the system (Q0), where
lowered to the lowest priority implementing
MLFQ:
finally scheduler for
parameters are defined by
Following
"
1. Number of queue required. of
queue. priority
algorithm for each increase and/or
decrease
for finding to
Scheduling
2. is required
3. Method
shortest-job-first
processes.
non preemptive versions of
FCFS, preemptive
and chart for the four
Explain the with Gantt
Example 2.6.8 2) schedulign
algorithms
(time slice = waitingtimp
and roudn robin average turn
around and
their
processes given. Compare
Burst tÉme
Arrival time
Process
10
P1
6
P2
2 12
P3
3 15
P4
P3 P4
P1 P2
16 28
10
P3 P4
P1 P2
28 43
10 16
P1 P4 P2 P3 P1| P4 P3 P1 P3 P4 P3 P4 P4 P4 P4
P1 P2 P3 P1 P4 P2 P3
10 12 14 16 18 20 22 24 26 28 30 32 34 36 38 40 4243
2 4 6 8
Waiting time
FCFS Preemptive SJF Nonpreemptive RR
Process
SJF
6 20
P1
0 13
P2
14 14 14 22
P3
P4 25 25 25 25
6+0+14 +15 35
Preemptive SJF = = 8.75
4 4
0+9+14 +15 48
Nonpreemptive SJF = = 12
4 4
20 +13 + 22+ 25 80
RR = = 20
4 4
Turnaround time
16 10 30
10
P1
15 19
15
P2 34
26
26
26
P3 40 40
40
40
P4
16+6+26+40 88 = 22
Preemptive SJF = 4
4
10+15+26+40 91 = 22.75
Nonpreemptive SJF= 4
4
30+19+34+ 40 123 = 30.75
RR = 4 4
following process
using
time for the
is the average turnaround
Example 2.6.9 What
non-prrmptive c) Preemptive SJF.
a) FCFS b) SJF Burst Time
Arrival Time
Process
0.0
P1
4
0.4
P2 Dec.-17, Marks 9
AU:
a) FCFS P3
P2
P1 13
8
12
0.0
b) SJF non-preemptive
P3 P2
|P1
8 13
0.0
c) Preemptive SJF
P1 P2 P3 P2 |P1
13
1.0 2 5.6
0.0 0.4
Turnaround Time
Process
Process Waiting Time
FCFS SJF Preemptive
FCFS SJF Preemptive Non-preem SJF
Non-pree SJF ptive
mptive
8 13.2
5.2 P1
P1
11.6 12.6 5
8.6 1 P2
P2 7.6
P3 12
P3 11 7
7.2 9.53 6.4
Average
turnaround
time
University Questions
1. Consider the following set of processes, with the length of the CPU-burst time in given ms :
P1 0.00
P2 4 1.001
P3 2.001
P4 5 3.001
P5 4.001
Draw four Gantt charts illustrating the execution of these processes using FCFS, SJE, priority and
RR (quantum = 2) scheduling. Also calculate waiting timne and turnaround time for each
scheduling algorith1s. AU:May-17, Marks 13
2. Explain the differences in the degree to which the following scheduling algorithms discriminate in
favor of short process. :
i) RR i) Multilevel feedback queues. AU : May-18, Marks 13
Consider the following set of processes, with the length of the CPU burst given in milliseconds :
Process Burst time Priority
10 3
P2 1 1
P3 2 3
PA 4
Ps 5 2
The process are assumed to have arrived in the order P, P, P3,, Pa, % all at time 0.
i) Draw Gantt chart that illustrate the execution of these processes using the scheduling algorithmns
FCFS (Smaller priority number implies higher priority) and RR (quantum = 1). [10]
ii) What is the waiting time of each process for each of the scheduling algorithms ? [5]
AU: May-18,22, Marks 15
4. Explain - multi level queue and multi level feedback queue scheduling with suitable examples.
AU: May-19, Marks 05
TECHNICAL PUBLICATIONS
an up-thrust for knowledge