You are on page 1of 13

21

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$pro rammed 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$pro ramm$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$pro ramm$n the CPU $s $dle t$ll the process completes the I)* and restarts e"ecut$on# +$th mult$pro ramm$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#

22

( ( Load memor! Add to memor! Read from f$le +a$t for I)* Load memor! 6a7e $ncrement +r$te $nto f$le +a$t for I)* Load memor! Add to memor! Read from f$le +a$t for I)* ( ( 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( .# 8# ;# 4# s'$tches from runn$n state to 'a$t$n 2an I)* re:uest4# 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# term$nates# I)* %urst CPU %urst I)* %urst CPU %urst I)* %urst CPU %urst

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#

23

4#;

SCHEDULING CRITERIA 6an! al or$thms e"$st for CPU schedul$n # 9ar$ous cr$ter$a ha-e %een su ested for

compar$n these CPU schedul$n al or$thms# Common cr$ter$a $nclude( .# 8# CPU ut$l$&at$on( Th$s ma! ran e from <= to .<<= $deall!# In real s!stems $t ran es from 4<= for a l$ htl! loaded s!stems to ><= for hea-$l! loaded s!stems# Throu hput( Num%er of processes completed per t$me un$t $s throu hput# 3or lon processes ma! %e of the order of one process per hour 'here as $n case of short processes, throu hput ma! %e .< or .8 processes per second# ;# 4# ?# 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# +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, throu hput and m$n$mum turnaround t$me, 'a$t$n t$me and response t$me# 4#4 SCHEDULING ALG*RITH6S Schedul$n al or$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 al or$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 al or$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

24

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 thou h the al or$thm $s s$mple, the a-era e '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 P. P8 P; 0urst t$me 2msecs4 84 ; ;

The Gantt chart %elo' sho's the result#

P1 <

P2 84 8A

P3 ;<

A-era e 'a$t$n t$me and a-era e 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-era e '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-era e 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(

P2 <

P3 ; E

P1 ;<

A-era e 'a$t$n t$me B 2< C ; C E4 ) ; B > ) ; B ; msecs#

25

A-era e 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-era e 'a$t$n and a-era e turnaround t$mes are lesser# The al or$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 al or$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 al or$thm# In th$s al or$thm, the len th 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 P. P8 P; P4 0urst t$me 2msecs4 E D A ;

Us$n SF3 al or$thm, the processes 'ould %e scheduled as sho'n %elo'#

26

P4 < ;

P1 >

P3 .E

P2 84

A-era e 'a$t$n t$me B 2< C ; C > C .E4 ) 4 B 8D ) 4 B A msecs# A-era e 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 al or$thm, then A-era e 'a$t$n t$me B 2< C E C .4 C 8.4 ) 4 B 4. ) 4 B .<#8? msecs# A-era e turnaround t$me B 2E C .4 C 8. C 844 ) 4 B E? ) 4 B .E#8? msecs# The SF3 al or$thm produces the most opt$mal schedul$n scheme# 3or a $-en set of processes, the al or$thm $-es the m$n$mum a-era e 'a$t$n and turnaround t$mes# Th$s $s %ecause, shorter processes are scheduled earl$er than lon er 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-anta e '$th the SF3 al or$thm l$es $n 7no'$n the len th 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 al or$thm $s therefore appl$ca%le $n lon @term schedul$n # The al or$thm cannot %e $mplemented for CPU schedul$n as there $s no 'a! to accuratel! 7no' $n ad-ance the len th of the ne"t CPU %urst# *nl! an appro"$mat$on of the len th can %e used to $mplement the al or$thm# 0ut the SF3 schedul$n al or$thm $s pro-a%l! opt$mal and thus ser-es as a %enchmar7 to compare other CPU schedul$n al or$thms# SF3 al or$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#

27

Process P. P8 P; P4

Arr$-al t$me 2msecs4 < . 8 ;

0urst t$me 2msecs4 D 4 > ?

If SF3 preempt$-e schedul$n $s used, the follo'$n Gantt chart sho's the result#

P1 < .

P2 ?

P4 .<

P1 .A

P3 8E

A-era e '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(

P1 < D

P2 .8

P4 .A

P3 8E

A-era e '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 al or$thm# The SF3 al or$thm $s a part$cular case of the eneral pr$or$t! al or$thm# In th$s case pr$or$t! $s the $n-erse of the ne"t CPU %urst t$me# Lar er 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!#

28

Process P. P8 P; P4 P?

Pr$or$t! ; . ; 4 8

0urst t$me 2msecs4 .< . 8 . ?

Us$n pr$or$t! schedul$n , the processes are scheduled as sho'n %elo'(

P2 < .

P5 E

P1

P3 .E

P4 .D .>

A-era e '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 al or$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 al or$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-anta es 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 a es to atta$n the h$ hest pr$or$t! at 'h$ch t$me $t ets a chance to e"ecute on the CPU#

29

4#4#4

R*UND@R*0IN 2RR4 SCHEDULING ALG*RITH6 The round@ro%$n CPU schedul$n al or$thm $s %as$call! a preempt$-e schedul$n al or$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! ran e %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 al or$thm# Process P. P8 P; 0urst t$me 2msecs4 84 ; ;

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#

P1 <

P2 4

P3 A

P1 .<

P1 .4

P1 .D

P1 88

P1 8E ;<

A-era e 'a$t$n t$me B 24 C A C 2.< / 444 ) ; B .A) ; B ?#EE msecs#

30

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 al or$thm $s -er! much dependent on the len th of the t$me sl$ce# If the durat$on of the t$me sl$ce $s $ndef$n$tel! lar e then the RR al or$thm $s the same as 3C3S al or$thm# If the t$me sl$ce $s too small, then the performance of the al or$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 Process t$me B .< Sl$ce Conte"t S'$tch

<

.<

.8

<

<

.<

<

>

.<

>

The a%o-e e"ample sho's that the t$me sl$ce should %e lar e '$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 al or$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 al or$thm l$7e 3C3S or RR#

31

3or e"ample, Hueue for $nteract$-e processes could %e scheduled us$n RR al or$thm 'here :ueue for %atch processes ma! use 3C3S al or$thm# An $llustrat$on of mult$le-el :ueues $s sho'n %elo' 23$ ure 4#84# H$ hest pr$or$t!
SYSTEM PROCESSES

INTERACTIVE PROCESSES

BATCH PROCESSES

STUDENT PROCESSES

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 al or$thm, processes one ass$ ned to :ueues do not mo-e or chan e :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 len ths# All processes could $n$t$all! 5o$n the h$ hest pr$or$t! :ueue# Processes re:u$r$n lon er 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

32

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 < RR sch !"#$%& '( ) 8*

RR sch !"#$%& '( ) 16* Hueue .

+C+S sch !"#$%& Hueue 8 Lo'est pr$or$t! A process enter$n the read! :ueue 5o$ns :ueue <# RR schedul$n al or$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 al or$thm $s used to schedule processes %ut : B .E# Th$s '$ll $-e processes a lon er t$me '$th the CPU# If a process has a CPU %urst st$ll lon er, 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 al or$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 al or$thm for each :ueue, cr$ter$a for up rad$n a process to a h$ her pr$or$t! :ueue, cr$ter$a for do'n rad$n a process to a lo'er pr$or$t! :ueue and also the :ueue to 'h$ch a process $n$t$all! enters#

33

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 al or$thms 'ere stud$ed# 4#E EJERCISE .# 8# ;# +hat do !ou understand %! CPU@I)* %urstK D$fferent$ate %et'een preempt$-e and nonpreempt$-e schedul$n # Cons$der the 'or7 load sho'n %elo'( Process P. P8 P; P4 P? 0urst t$me 2msecs4 .< 8> <; <A .8

All the processors arr$-e at t$me < $n the order $-en# Compute a-era e turn around t$me and a-era e 'a$t$n t$me us$n 3C3S, SF3 and RR 2H B ?msecs4 schedul$n al or$thms# Illustrate us$n Gantt charts# 4#A ACTI9IT1 3$nd out the schedul$n al or$thms $mplemented for the operat$n s!stems noted#

You might also like