CHAPTER 4

CPU SCHEDULING
The CPU scheduler selects a process from amon the read! processes to e"ecute on the
CPU# CPU schedul$n $s the %as$s for mult$prorammed operat$n s!stems# CPU ut$l$&at$on
$ncreases %! s'$tch$n the CPU amon read! processes $nstead of 'a$t$n for each process to
term$nate %efore e"ecut$n the ne"t#
The $dea of mult$proramm$n could %e descr$%ed as follo's( A process $s e"ecuted %! the
CPU unt$l $t completes or oes for an I)*# In s$mple s!stems '$th no mult$proramm$n the CPU $s
$dle t$ll the process completes the I)* and restarts e"ecut$on# +$th mult$proramm$n, man! read!
processes are ma$nta$ned $n memor!# So 'hen CPU %ecomes $dle as $n the case a%o-e, the
operat$n s!stem s'$tches to e"ecute another process each t$me a current process oes $nto a
'a$t for I)*#
4#. CPU / I)* 0URST C1CLE
Process e"ecut$on cons$sts of alternate CPU e"ecut$on and I)* 'a$t# A c!cle of these t'o
e-ents repeats t$ll the process completes e"ecut$on 23$ure 4#.4# Process e"ecut$on %e$ns '$th a
CPU %urst follo'ed %! an I)* %urst and then another CPU %urst and so on# E-entuall! a CPU %urst
'$ll term$nate the e"ecut$on# An I)* %ound 5o% '$ll ha-e short CPU %ursts and a CPU %ound 5o% '$ll
ha-e lon CPU %ursts#
21
(
(
Load memor!
Add to memor! CPU %urst
Read from f$le
I)* %urst
Load memor!
6a7e $ncrement CPU %urst
+r$te $nto f$le
I)* %urst
Load memor!
Add to memor! CPU %urst
Read from f$le
I)* %urst
(
(
3$ure 4#.( CPU and I)* %ursts
4#8 PREE6PTI9E ) N*NPREE6PTI9E SCHEDULING
CPU scheduler has to ta7e a dec$s$on 'hen a process(
.# s'$tches from runn$n state to 'a$t$n 2an I)* re:uest4#
8# s'$tches from runn$n state to read! state 2e"p$r! of a t$me sl$ce4#
;# s'$tches from 'a$t$n to read! state 2complet$on of an I)*4#
4# term$nates#
Schedul$n under cond$t$on 2.4 or 244 $s sa$d to %e nonpreempt$-e# In nonpreempt$-e
schedul$n, a process once allotted the CPU 7eeps e"ecut$n unt$l the CPU $s released e$ther %! a
s'$tch to a 'a$t$n state or %! term$nat$on# Preempt$-e schedul$n occurs under cond$t$on 284 or 2;4#
In preempt$-e schedul$n, an e"ecut$n process $s stopped e"ecut$n and returned to the read!
:ueue to ma7e the CPU a-a$la%le for another read! process#
22
+a$t for I)*
+a$t for I)*
+a$t for I)*
4#; SCHEDULING CRITERIA
6an! alor$thms e"$st for CPU schedul$n# 9ar$ous cr$ter$a ha-e %een suested for
compar$n these CPU schedul$n alor$thms# Common cr$ter$a $nclude(
.# CPU ut$l$&at$on( Th$s ma! rane from <= to .<<= $deall!# In real s!stems $t ranes
from 4<= for a l$htl! loaded s!stems to ><= for hea-$l! loaded s!stems#
8# Throuhput( Num%er of processes completed per t$me un$t $s throuhput# 3or lon
processes ma! %e of the order of one process per hour 'here as $n case of short
processes, throuhput ma! %e .< or .8 processes per second#
;# Turnaround t$me( The $nter-al of t$me %et'een su%m$ss$on and complet$on of a
process $s called turnaround t$me# It $ncludes e"ecut$on t$me and 'a$t$n t$me#
4# +a$t$n t$me( Sum of all the t$mes spent %! a process at d$fferent $nstances 'a$t$n
$n the read! :ueue $s called 'a$t$n t$me#
?# Response t$me( In an $nteract$-e process the user $s us$n some output enerated
'h$le the process cont$nues to enerate ne' results# Instead of us$n the
turnaround t$me that $-es the d$fference %et'een t$me of su%m$ss$on and t$me of
complet$on, response t$me $s somet$mes used# Response t$me $s thus the
d$fference %et'een t$me of su%m$ss$on and the t$me the f$rst response occurs#
Des$ra%le features $nclude ma"$mum CPU ut$l$&at$on, throuhput and m$n$mum turnaround
t$me, 'a$t$n t$me and response t$me#
4#4 SCHEDULING ALG*RITH6S
Schedul$n alor$thms d$ffer $n the manner $n 'h$ch the CPU selects a process $n the read!
:ueue for e"ecut$on#
4#4#. 3IRST C*6E 3IRST SER9E 23C3S4 SCHEDULING ALG*RITH6
Th$s $s one of the -er! %rute force alor$thms# A process that re:uests for the CPU f$rst $s
allocated the CPU f$rst# Hence the name f$rst come f$rst ser-e# The 3C3S alor$thm $s $mplemented
%! us$n a f$rst@$n@f$rst@out 23I3*4 :ueue structure for the read! :ueue# Th$s :ueue has a head and
23
a ta$l# +hen a process 5o$ns the read! :ueue $ts PC0 $s l$n7ed to the ta$l of the 3I3* :ueue# +hen
the CPU $s $dle, the process at the head of the 3I3* :ueue $s allocated the CPU and deleted from
the :ueue#
E-en thouh the alor$thm $s s$mple, the a-erae 'a$t$n $s often :u$te lon and -ar$es
su%stant$all! $f the CPU %urst t$mes -ar! reatl! as seen $n the follo'$n e"ample#
Cons$der a set of three processes P., P8 and P; arr$-$n at t$me $nstant < and ha-$n CPU
%urst t$mes as sho'n %elo'(
Process 0urst t$me 2msecs4
P. 84
P8 ;
P; ;
The Gantt chart %elo' sho's the result#
< 84 8A ;<
A-erae 'a$t$n t$me and a-erae turnaround t$me are calculated as follo's(
The 'a$t$n t$me for process P. B < msecs
P8 B 84 msecs
P; B 8A msecs
A-erae 'a$t$n t$me B 2< C 84 C 8A4 ) ; B ?. ) ; B .A msecs#
P. completes at the end of 84 msecs, P8 at the end of 8A msecs and P; at the end of ;<
msecs# A-erae turnaround t$me B 284 C 8A C ;<4 ) ; B D. ) ; B 8A msecs#
If the processes arr$-e $n the order P8, P; and P;, then the result '$ll %e as follo's(
< ; E ;<
A-erae 'a$t$n t$me B 2< C ; C E4 ) ; B > ) ; B ; msecs#
24
P1 P2 P3
P1 P2 P3
A-erae turnaround t$me B 2; C E C ;<4 ) ; B ;> ) ; B .; msecs#
Thus $f processes '$th smaller CPU %urst t$mes arr$-e earl$er, then a-erae 'a$t$n and
a-erae turnaround t$mes are lesser#
The alor$thm also suffers from 'hat $s 7no'n as a con-o! effect# Cons$der the follo'$n
scenar$o# Let there %e a m$" of one CPU %ound process and man! I)* %ound processes $n the
read! :ueue#
The CPU %ound process ets the CPU and e"ecutes 2lon I)* %urst4#
In the mean'h$le, I)* %ound processes f$n$sh I)* and 'a$t for CPU thus lea-$n the I)*
de-$ces $dle#
The CPU %ound process releases the CPU as $t oes for an I)*#
I)* %ound processes ha-e short CPU %ursts and the! e"ecute and o for I)* :u$c7l!# The
CPU $s $dle t$ll the CPU %ound process f$n$shes the I)* and ets hold of the CPU#
The a%o-e c!cle repeats# Th$s $s called the con-o! effect# Here small processes 'a$t for
one %$ process to release the CPU#
S$nce the alor$thm $s nonpreempt$-e $n nature, $t $s not su$ted for t$me shar$n s!stems#
4#4#8 SH*RTEST F*0 3IRST 2SF34 SCHEDULING ALG*RITH6
Another approach to CPU schedul$n $s the shortest 5o% f$rst alor$thm# In th$s alor$thm,
the lenth of the CPU %urst $s cons$dered# +hen the CPU $s a-a$la%le, $t $s ass$ned to the process
that has the smallest ne"t CPU %urst# Hence the name shortest 5o% f$rst# In case there $s a t$e,
3C3S schedul$n $s used to %rea7 the t$e# As an e"ample, cons$der the follo'$n set of processes
P., P8, P;, P4 and the$r CPU %urst t$mes(
Process 0urst t$me 2msecs4
P. E
P8 D
P; A
P4 ;
Us$n SF3 alor$thm, the processes 'ould %e scheduled as sho'n %elo'#
25
< ; > .E 84
A-erae 'a$t$n t$me B 2< C ; C > C .E4 ) 4 B 8D ) 4 B A msecs#
A-erae turnaround t$me B 2; C > C .E C 844 ) 4 B ?8 ) 4 B .; msecs#
If the a%o-e processes 'ere scheduled us$n 3C3S alor$thm, then
A-erae 'a$t$n t$me B 2< C E C .4 C 8.4 ) 4 B 4. ) 4 B .<#8? msecs#
A-erae turnaround t$me B 2E C .4 C 8. C 844 ) 4 B E? ) 4 B .E#8? msecs#
The SF3 alor$thm produces the most opt$mal schedul$n scheme# 3or a $-en set of
processes, the alor$thm $-es the m$n$mum a-erae 'a$t$n and turnaround t$mes# Th$s $s
%ecause, shorter processes are scheduled earl$er than loner ones and hence 'a$t$n t$me for
shorter processes decreases more than $t $ncreases the 'a$t$n t$me of lon processes#
The ma$n d$sad-antae '$th the SF3 alor$thm l$es $n 7no'$n the lenth of the ne"t CPU
%urst# In case of lon@term or 5o% schedul$n $n a %atch s!stem, the t$me re:u$red to complete a 5o%
as $-en %! the user can %e used to schedule# SF3 alor$thm $s therefore appl$ca%le $n lon@term
schedul$n#
The alor$thm cannot %e $mplemented for CPU schedul$n as there $s no 'a! to accuratel!
7no' $n ad-ance the lenth of the ne"t CPU %urst# *nl! an appro"$mat$on of the lenth can %e
used to $mplement the alor$thm#
0ut the SF3 schedul$n alor$thm $s pro-a%l! opt$mal and thus ser-es as a %enchmar7 to
compare other CPU schedul$n alor$thms#
SF3 alor$thm could %e e$ther preempt$-e or nonpreempt$-e# If a ne' process 5o$ns the
read! :ueue '$th a shorter ne"t CPU %urst then 'hat $s rema$n$n of the current e"ecut$n
process, then the CPU $s allocated to the ne' process# In case of nonpreempt$-e schedul$n, the
current e"ecut$n process $s not preempted and the ne' process ets the ne"t chance, $t %e$n the
process '$th the shortest ne"t CPU %urst#
G$-en %elo' are the arr$-al and %urst t$mes of four processes P., P8, P; and P4#
26
P4 P1 P2 P3
Process Arr$-al t$me 2msecs4 0urst t$me 2msecs4
P. < D
P8 . 4
P; 8 >
P4 ; ?
If SF3 preempt$-e schedul$n $s used, the follo'$n Gantt chart sho's the result#
< . ? .< .A 8E
A-erae 'a$t$n t$me B 22.< / .4 C < C 2.A / 84 C 2.? / ;44 ) 4 B 8E ) 4 B E#? msecs#
If nonpreempt$-e SF3 schedul$n $s used, the result $s as follo's(
< D .8 .A 8E
A-erae 'a$t$n t$me B 22< C 2D / .4 C 2.8 / ;4 C 2.A / 844 ) 4 B ;. ) 4 B A#A? msecs#
4#4#; PRI*RIT1 SCHEDULING
Each process can %e assoc$ated '$th a pr$or$t!# CPU $s allocated to the process ha-$n the
h$hest pr$or$t!# Hence the name pr$or$t!# E:ual pr$or$t! processes are scheduled accord$n to
3C3S alor$thm#
The SF3 alor$thm $s a part$cular case of the eneral pr$or$t! alor$thm# In th$s case pr$or$t!
$s the $n-erse of the ne"t CPU %urst t$me# Larer the ne"t CPU %urst, lo'er $s the pr$or$t! and -$ce
-ersa# In the follo'$n e"ample, 'e '$ll assume lo'er num%ers to represent h$her pr$or$t!#
27
P1 P2 P1 P4 P3
P1 P4 P2 P3
Process Pr$or$t! 0urst t$me 2msecs4
P. ; .<
P8 . .
P; ; 8
P4 4 .
P? 8 ?
Us$n pr$or$t! schedul$n, the processes are scheduled as sho'n %elo'(
< . E .E .D .>
A-erae 'a$t$n t$me B 2E C < C .E C .D C .4 ) ? B 4. ) ? B D#8 msecs#
Pr$or$t$es can %e def$ned e$ther $nternall! or e"ternall!# Internal def$n$t$on of pr$or$t! $s %ased
on some measura%le factors l$7e memor! re:u$rements, num%er of open f$les, and so on# E"ternal
pr$or$t$es are def$ned %! cr$ter$a such as $mportance of the user depend$n on the userGs
department and other $nfluenc$n factors#
Pr$or$t! %ased alor$thms can %e e$ther preempt$-e or nonpreempt$-e# In case of
preempt$-e schedul$n, $f a ne' process 5o$ns the read! :ueue '$th a pr$or$t! h$her than the
process that $s e"ecut$n, then the current process $s preempted and CPU allocated to the ne'
process# 0ut $n case of nonpreempt$-e alor$thm, the ne' process ha-$n h$hest pr$or$t! from
amon the read! processes, $s allocated the CPU onl! after the current process $-es up the CPU#
Star-at$on or $ndef$n$te %loc7$n $s one of the ma5or d$sad-antaes of pr$or$t! schedul$n#
E-er! process $s assoc$ated '$th a pr$or$t!# In a hea-$l! loaded s!stem, lo' pr$or$t! processes $n the
read! :ueue are star-ed or ne-er et a chance to e"ecute# Th$s $s %ecause there $s al'a!s a
h$her pr$or$t! process ahead of them $n the read! :ueue#
A solut$on to star-at$on $s a$n# A$n $s a concept 'here the pr$or$t! of a process 'a$t$n
$n the read! :ueue $s $ncreased raduall!# E-entuall! e-en the lo'est pr$or$t! process aes to
atta$n the h$hest pr$or$t! at 'h$ch t$me $t ets a chance to e"ecute on the CPU#
28
P2 P1 P5 P3 P4
4#4#4 R*UND@R*0IN 2RR4 SCHEDULING ALG*RITH6
The round@ro%$n CPU schedul$n alor$thm $s %as$call! a preempt$-e schedul$n alor$thm
des$ned for t$me@shar$n s!stems# *ne un$t of t$me $s called a t$me sl$ce# Durat$on of a t$me sl$ce
ma! rane %et'een .< msecs# and a%out .<< msecs# The CPU scheduler allocates to each
process $n the read! :ueue one t$me sl$ce at a t$me $n a round@ro%$n fash$on# Hence the name
round@ro%$n#
The read! :ueue $n th$s case $s a 3I3* :ueue '$th ne' processes 5o$n$n the ta$l of the
:ueue# The CPU scheduler p$c7s processes from the head of the :ueue for allocat$n the CPU#
The f$rst process at the head of the :ueue ets to e"ecute on the CPU at the start of the current
t$me sl$ce and $s deleted from the read! :ueue# The process allocated the CPU ma! ha-e the
current CPU %urst e$ther e:ual to the t$me sl$ce or smaller than the t$me sl$ce or reater than the
t$me sl$ce# In the f$rst t'o cases, the current process '$ll release the CPU on $ts o'n and there %!
the ne"t process $n the read! :ueue '$ll %e allocated the CPU for the ne"t t$me sl$ce# In the th$rd
case, the current process $s preempted, stops e"ecut$n, oes %ac7 and 5o$ns the read! :ueue at
the ta$l there %! ma7$n 'a! for the ne"t process#
Cons$der the same e"ample e"pla$ned under 3C3S alor$thm#
Process 0urst t$me 2msecs4
P. 84
P8 ;
P; ;
Let the durat$on of a t$me sl$ce %e 4 msecs, 'h$ch $s to sa! CPU s'$tches %et'een
processes e-er! 4 msecs $n a round@ro%$n fash$on# The Gantt chart %elo' sho's the schedul$n of
processes#
< 4 A .< .4 .D 88 8E ;<
A-erae 'a$t$n t$me B 24 C A C 2.< / 444 ) ; B .A) ; B ?#EE msecs#
29
P1 P2 P3 P1 P1 P1 P1 P1
If there are ? processes $n the read! :ueue that $s n B ?, and one t$me sl$ce $s def$ned to %e
8< msecs that $s : B 8<, then each process '$ll et 8< msecs or one t$me sl$ce e-er! .<< msecs#
Each process '$ll ne-er 'a$t for more than 2n / .4 " : t$me un$ts#
The performance of the RR alor$thm $s -er! much dependent on the lenth of the t$me
sl$ce# If the durat$on of the t$me sl$ce $s $ndef$n$tel! lare then the RR alor$thm $s the same as
3C3S alor$thm# If the t$me sl$ce $s too small, then the performance of the alor$thm deter$orates
%ecause of the effect of fre:uent conte"t s'$tch$n# 0elo' $s sho'n a compar$son of t$me sl$ces of
-ar!$n durat$on and the conte"t s'$tches the! enerate on onl! one process of .< t$me un$ts#
T$me Conte"t
Process t$me B .< Sl$ce S'$tch
< .< .8 <

< E .< E .
< . 8 ; 4 ? E A D > .< . >
The a%o-e e"ample sho's that the t$me sl$ce should %e lare '$th respect to the conte"t
s'$tch t$me else $f RR schedul$n $s used the CPU '$ll spend more t$me $n conte"t s'$tch$n#
4#4#? 6ULTILE9EL HUEUE SCHEDULING
Processes can %e class$f$ed $nto roups# 3or e"ample, $nteract$-e processes, s!stem
processes, %atch processes, student processes and so on# Processes %elon$n to a roup ha-e a
spec$f$ed pr$or$t!# Th$s alor$thm part$t$ons the read! :ueue $nto as man! separate :ueues as there
are roups# Hence the name mult$le-el :ueue# 0ased on certa$n propert$es $s process $s ass$ned
to one of the read! :ueues# Each :ueue can ha-e $ts o'n schedul$n alor$thm l$7e 3C3S or RR#
30
3or e"ample, Hueue for $nteract$-e processes could %e scheduled us$n RR alor$thm 'here
:ueue for %atch processes ma! use 3C3S alor$thm# An $llustrat$on of mult$le-el :ueues $s sho'n
%elo' 23$ure 4#84#
H$hest pr$or$t!
Lo'est Pr$or$t!
Hueues themsel-es ha-e pr$or$t$es# Each :ueue has a%solute pr$or$t! o-er lo' pr$or$t!
:ueues, that $s a process $n a :ueue '$th lo'er pr$or$t! '$ll not %e e"ecuted unt$l all processes $n a
:ueue '$th h$her pr$or$t! ha-e f$n$shed e"ecut$n# If a process $n a lo'er pr$or$t! :ueue $s
e"ecut$n 2h$her pr$or$t! :ueues are empt!4 and a process 5o$ns a h$her pr$or$t! :ueue, then the
e"ecut$n process $s preempted to ma7e 'a! for a process $n the h$her pr$or$t! :ueue#
Th$s pr$or$t! on the :ueues themsel-es ma! lead to star-at$on# To o-ercome th$s pro%lem,
t$me sl$ces ma! %e ass$ned to :ueues 'hen each :ueue ets some amount of CPU t$me# The
durat$on of the t$me sl$ces ma! %e d$fferent for :ueues depend$n on the pr$or$t! of the :ueues#
4#4#E 6ULTILE9EL 3EED0ACI HUEUE SCHEDULING
In the pre-$ous mult$le-el :ueue schedul$n alor$thm, processes one ass$ned to :ueues
do not mo-e or chane :ueues# 6ult$le-el feed%ac7 :ueues allo' a process to mo-e %et'een
:ueues# The $dea $s to separate out processes '$th d$fferent CPU %urst lenths# All processes
could $n$t$all! 5o$n the h$hest pr$or$t! :ueue# Processes re:u$r$n loner CPU %ursts are pushed to
lo'er pr$or$t! :ueues# I)* %ound and $nteract$-e processes rema$n $n h$her pr$or$t! :ueues# A$n
31
SYSTEM PROCESSES
INTERACTIVE PROCESSES
BATCH PROCESSES
STUDENT PROCESSES
could %e cons$dered to mo-e processes from lo'er pr$or$t! :ueues to h$her pr$or$t! to a-o$d
star-at$on# An $llustrat$on of mult$le-el feed%ac7 :ueues $s sho'n %elo' 23$ure 4#;4#
H$hest pr$or$t!
Hueue <
Hueue .
Hueue 8
Lo'est pr$or$t!
A process enter$n the read! :ueue 5o$ns :ueue <# RR schedul$n alor$thm '$th : B D $s
used to schedule processes $n :ueue <# If the CPU %urst of a process e"ceeds D msecs#, then the
process preempted, deleted from :ueue < and 5o$ns the ta$l of :ueue .# +hen :ueue < %ecomes
empt!, then processes $n :ueue . '$ll %e scheduled# Here also RR schedul$n alor$thm $s used to
schedule processes %ut : B .E# Th$s '$ll $-e processes a loner t$me '$th the CPU# If a process
has a CPU %urst st$ll loner, then $t 5o$ns :ueue ; on %e$n preempted# Hence h$hest pr$or$t!
processes 2processes ha-$n small CPU %ursts, that $s I)* %ound processes4 rema$n $n :ueue .
and lo'est pr$or$t! processes 2those ha-$n lon CPU %ursts4 '$ll e-entuall! s$n7 do'n# The lo'est
pr$or$t! :ueue could %e scheduled us$n 3C3S alor$thm to allo' processes to complete e"ecut$on#
6ult$le-el feed%ac7 scheduler '$ll ha-e to cons$der parameters such as num%er of :ueues,
schedul$n alor$thm for each :ueue, cr$ter$a for uprad$n a process to a h$her pr$or$t! :ueue,
cr$ter$a for do'nrad$n a process to a lo'er pr$or$t! :ueue and also the :ueue to 'h$ch a process
$n$t$all! enters#
32
RR sch!"#$%& '( ) 8*
RR sch!"#$%& '( ) 16*
+C+S sch!"#$%&
4#? SU66AR1
In th$s chapter 'e ha-e d$scussed CPU schedul$n# The lon@term scheduler pro-$des a
proper m$" of CPU@I)* %ound 5o%s for e"ecut$on# The short@term scheduler has to schedule these
processes for e"ecut$on# Schedul$n can e$ther %e preempt$-e or nonpreempt$-e# If preempt$-e,
then an e"ecut$n process can %e stopped and returned to read! state to ma7e the CPU a-a$la%le
for another read! process# 0ut $f nonpreempt$-e schedul$n $s used then a process once allotted
the CPU 7eeps e"ecut$n unt$l e$ther the process oes $nto 'a$t state %ecause of an I)* or $t has
completed e"ecut$on# D$fferent schedul$n alor$thms 'ere stud$ed#
33