OS_4 lecture

  • Upload
    dineep

  • View
    215

  • Download
    0

Embed Size (px)

Citation preview

  • 8/11/2019 OS_4 lecture

    1/13

    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#.# 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

  • 8/11/2019 OS_4 lecture

    2/13

    (

    (

    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:uest#

    8# s'$tches from runn$n state to read! state 2e"p$r! of a t$me sl$ce#

    ;# s'$tches from 'a$t$n to read! state 2complet$on of an I)*#

    4# term$nates#

    Schedul$n under cond$t$on 2. or 24 $s sa$d to %e nonpreempt$-e# In nonpreempt$-eschedul$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 28 or 2;#

    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)*

  • 8/11/2019 OS_4 lecture

    3/13

    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

  • 8/11/2019 OS_4 lecture

    4/13

    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 2msecs

    P. 84

    P8 ;

    P; ;

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

    < 84 8 ; . 84

    A-erae 'a$t$n t$me B 2< ; > . ) 4 B 8 ) 4 B msecs#

    A-erae turnaround t$me B 2; > . 84 ) 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< .4 8. ) 4 B 4. ) 4 B .

  • 8/11/2019 OS_4 lecture

    7/13

    Process Arr$-al t$me 2msecs 0urst t$me 2msecs

    P. <

    P8 . 4

    P; 8 >

    P4 ; ?

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

    < . ? .< . 8

    A-erae 'a$t$n t$me B 22.< / . < 2. / 8 2.? / ; ) 4 B 8 ) 4 B #? msecs#

    If nonpreempt$-e SF3 schedul$n $s used, the result $s as follo's(

    < .8 . 8

    A-erae 'a$t$n t$me B 22< 2 / . 2.8 / ; 2. / 8 ) 4 B ;. ) 4 B #? 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 P1P4 P3

    P1 P4P2 P3

  • 8/11/2019 OS_4 lecture

    8/13

    Process Pr$or$t! 0urst t$me 2msecs

    P. ; .

    A-erae 'a$t$n t$me B 2 < . . . ) ? B 4. ) ? B #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 users

    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 P1P5 P3 P4

  • 8/11/2019 OS_4 lecture

    9/13

    4#4#4 R*UND@R*0IN 2RR 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 .

  • 8/11/2019 OS_4 lecture

    10/13

    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

  • 8/11/2019 OS_4 lecture

    11/13

    3or e"ample, ueue 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#8#

    H$hest pr$or$t!

    Lo'est Pr$or$t!

    ueues 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! 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# 6ULTILE9EL 3EED0AC UEUE 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

  • 8/11/2019 OS_4 lecture

    12/13

    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#;#

    H$hest pr$or$t!

    ueue