Sei sulla pagina 1di 5

6/27/2015

OperatingSystemsLectureNotesLecture6CPUScheduling

OperatingSystemsLectureNotes
Lecture6
CPUScheduling
MartinC.Rinard
WhatisCPUscheduling?Determiningwhichprocessesrunwhentherearemultiplerunnable
processes.Whyisitimportant?Becauseitcancanhaveabigeffectonresourceutilizationandthe
overallperformanceofthesystem.
Bytheway,theworldwentthroughalongperiod(late80's,early90's)inwhichthemostpopular
operatingsystems(DOS,Mac)hadNOsophisticatedCPUschedulingalgorithms.Theywere
singlethreadedandranoneprocessatatimeuntiltheuserdirectsthemtorunanotherprocess.
Whywasthistrue?Morerecentsystems(WindowsNT)arebacktohavingsophisticatedCPU
schedulingalgorithms.Whatdrovethechange,andwhatwillhappeninthefuture?
Basicassumptionsbehindmostschedulingalgorithms:
ThereisapoolofrunnableprocessescontendingfortheCPU.
Theprocessesareindependentandcompeteforresources.
ThejobofthescheduleristodistributethescarceresourceoftheCPUtothedifferent
processes``fairly''(accordingtosomedefinitionoffairness)andinawaythatoptimizes
someperformancecriteria.
Ingeneral,theseassumptionsarestartingtobreakdown.Firstofall,CPUsarenotreallythat
scarcealmosteverybodyhasseveral,andprettysoonpeoplewillbeabletoaffordlots.Second,
manyapplicationsarestartingtobestructuredasmultiplecooperatingprocesses.So,aviewofthe
schedulerasmediatingbetweencompetingentitiesmaybepartiallyobsolete.
Howdoprocessesbehave?First,CPU/IOburstcycle.Aprocesswillrunforawhile(theCPU
burst),performsomeIO(theIOburst),thenrunforawhilemore(thenextCPUburst).Howlong
betweenIOoperations?Dependsontheprocess.
IOBoundprocesses:processesthatperformlotsofIOoperations.EachIOoperationis
followedbyashortCPUbursttoprocesstheIO,thenmoreIOhappens.
CPUboundprocesses:processesthatperformlotsofcomputationanddolittleIO.Tendto
haveafewlongCPUbursts.
OneofthethingsaschedulerwilltypicallydoisswitchtheCPUtoanotherprocesswhenone
processdoesIO.Why?TheIOwilltakealongtime,anddon'twanttoleavetheCPUidlewhile
waitfortheIOtofinish.
WhenlookatCPUbursttimesacrossthewholesystem,havetheexponentialorhyperexponential
distributioninFig.5.2.
Whatarepossibleprocessstates?
RunningprocessisrunningonCPU.
Readyreadytorun,butnotactuallyrunningontheCPU.
WaitingwaitingforsomeeventlikeIOtohappen.
Whendoschedulingdecisionstakeplace?WhendoesCPUchoosewhichprocesstorun?Area
varietyofpossibilities:
Whenprocessswitchesfromrunningtowaiting.CouldbebecauseofIOrequest,because
waitforchildtoterminate,orwaitforsynchronizationoperation(likelockacquisition)to
complete.
Whenprocessswitchesfromrunningtoreadyoncompletionofinterrupthandler,for
example.Commonexampleofinterrupthandlertimerinterruptininteractivesystems.If
http://people.csail.mit.edu/rinard/osnotes/h6.html

1/5

6/27/2015

OperatingSystemsLectureNotesLecture6CPUScheduling

schedulerswitchesprocessesinthiscase,ithaspreemptedtherunningprocess.Another
commoncaseinterrupthandleristheIOcompletionhandler.
Whenprocessswitchesfromwaitingtoreadystate(oncompletionofIOoracquisitionofa
lock,forexample).
Whenaprocessterminates.
Howtoevaluateschedulingalgorithm?Therearemanypossiblecriteria:
CPUUtilization:KeepCPUutilizationashighaspossible.(Whatisutilization,bythe
way?).
Throughput:numberofprocessescompletedperunittime.
TurnaroundTime:meantimefromsubmissiontocompletionofprocess.
WaitingTime:Amountoftimespentreadytorunbutnotrunning.
ResponseTime:Timebetweensubmissionofrequestsandfirstresponsetotherequest.
SchedulerEfficiency:Theschedulerdoesn'tperformanyusefulwork,soanytimeittakesis
pureoverhead.So,needtomaketheschedulerveryefficient.
Bigdifference:BatchandInteractivesystems.Inbatchsystems,typicallywantgoodthroughputor
turnaroundtime.Ininteractivesystems,bothofthesearestillusuallyimportant(afterall,want
somecomputationtohappen),butresponsetimeisusuallyaprimaryconsideration.And,forsome
systems,throughputorturnaroundtimeisnotreallyrelevantsomeprocessesconceptuallyrun
forever.
Differencebetweenlongandshorttermscheduling.Longtermschedulerisgivenasetof
processesanddecideswhichonesshouldstarttorun.Oncetheystartrunning,theymaysuspend
becauseofIOorbecauseofpreemption.Shorttermschedulerdecideswhichoftheavailablejobs
thatlongtermschedulerhasdecidedarerunnabletoactuallyrun.
Let'sstartlookingatseveralvanillaschedulingalgorithms.
FirstCome,FirstServed.Onereadyqueue,OSrunstheprocessatheadofqueue,newprocesses
comeinattheendofthequeue.AprocessdoesnotgiveupCPUuntiliteitherterminatesor
performsIO.
ConsiderperformanceofFCFSalgorithmforthreecomputeboundprocesses.Whatifhave4
processesP1(takes24seconds),P2(takes3seconds)andP3(takes3seconds).Ifarriveinorder
P1,P2,P3,whatis
WaitingTime?(24+27)/3=17
TurnaroundTime?(24+27+30)=27.
Throughput?30/3=10.
WhataboutifprocessescomeinorderP2,P3,P1?Whatis
WaitingTime?(3+3)/2=6
TurnaroundTime?(3+6+30)=13.
Throughput?30/3=10.
ShortestJobFirst(SJF)caneliminatesomeofthevarianceinWaitingandTurnaroundtime.In
fact,itisoptimalwithrespecttoaveragewaitingtime.Bigproblem:howdoesschedulerfigureout
howlongwillittaketheprocesstorun?
Forlongtermschedulerrunningonabatchsystem,userwillgiveanestimate.Usuallyprettygood
ifitistooshort,systemwillcanceljobbeforeitfinishes.Iftoolong,systemwillholdoffon
runningtheprocess.So,usersgiveprettygoodestimatesofoverallrunningtime.
Forshorttermscheduler,mustusethepasttopredictthefuture.Standardway:useatimedecayed
exponentiallyweightedaverageofpreviousCPUburstsforeachprocess.LetTnbethemeasured
bursttimeofthenthburst,snbethepredictedsizeofnextCPUburst.Then,chooseaweighting
factorw,where0<=w<=1andcomputesn+1=wTn+(1w)sn.s0isdefinedassomedefault
constantorsystemaverage.
wtellshowtoweightthepastrelativetofuture.Ifchoosew=.5,lastobservationhasasmuch
weightasentirerestofthehistory.Ifchoosew=1,onlylastobservationhasanyweight.Doa
http://people.csail.mit.edu/rinard/osnotes/h6.html

2/5

6/27/2015

OperatingSystemsLectureNotesLecture6CPUScheduling

quickexample.
Preemptivevs.NonpreemptiveSJFscheduler.Preemptiveschedulerrerunsschedulingdecision
whenprocessbecomesready.Ifthenewprocesshaspriorityoverrunningprocess,theCPU
preemptstherunningprocessandexecutesthenewprocess.Nonpreemptivescheduleronlydoes
schedulingdecisionwhenrunningprocessvoluntarilygivesupCPU.Ineffect,itallowsevery
runningprocesstofinishitsCPUburst.
Consider4processesP1(bursttime8),P2(bursttime4),P3(bursttime9)P4(bursttime5)that
arriveonetimeunitapartinorderP1,P2,P3,P4.Assumethatafterbursthappens,processisnot
reenabledforalongtime(atleast100,forexample).WhatdoesapreemptiveSJFschedulerdo?
Whataboutanonpreemptivescheduler?
PriorityScheduling.Eachprocessisgivenapriority,thenCPUexecutesprocesswithhighest
priority.Ifmultipleprocesseswithsamepriorityarerunnable,usesomeothercriteriatypically
FCFS.SJFisanexampleofaprioritybasedschedulingalgorithm.Withtheexponentialdecay
algorithmabove,theprioritiesofagivenprocesschangeovertime.
Assumewehave5processesP1(bursttime10,priority3),P2(bursttime1,priority1),P3(burst
time2,priority3),P4(bursttime1,priority4),P5(bursttime5,priority2).Lowernumbers
representhigherpriorities.Whatwouldastandardpriorityschedulerdo?
Bigproblemwithpriorityschedulingalgorithms:starvationorblockingoflowpriorityprocesses.
Canuseagingtopreventthismakethepriorityofaprocessgoupthelongeritstaysrunnablebut
isn'trun.
Whataboutinteractivesystems?CannotjustletanyprocessrunontheCPUuntilitgivesitup
mustgiveresponsetousersinareasonabletime.So,useanalgorithmcalledroundrobin
scheduling.SimilartoFCFSbutwithpreemption.Haveatimequantumortimeslice.Letthefirst
processinthequeuerununtilitexpiresitsquantum(i.e.runsforaslongasthetimequantum),
thenrunthenextprocessinthequeue.
Implementingroundrobinrequirestimerinterrupts.Whenscheduleaprocess,setthetimertogo
offafterthetimequantumamountoftimeexpires.IfprocessdoesIObeforetimergoesoff,no
problemjustrunnextprocess.Butifprocessexpiresitsquantum,doacontextswitch.Savethe
stateoftherunningprocessandrunthenextprocess.
HowwelldoesRRwork?Well,itgivesgoodresponsetime,butcangivebadwaitingtime.
Considerthewaitingtimesunderroundrobinfor3processesP1(bursttime24),P2(bursttime3),
andP3(bursttime4)withtimequantum4.Whathappens,andwhatisaveragewaitingtime?What
givesbestwaitingtime?
Whathappenswithreallyareallysmallquantum?Itlookslikeyou'vegotaCPUthatis1/nas
powerfulastherealCPU,wherenisthenumberofprocesses.Problemwithasmallquantum
contextswitchoverhead.
Whatabouthavingareallysmallquantumsupportedinhardware?Then,youhavesomething
calledmultithreading.GivetheCPUabunchofregistersandheavilypipelinetheexecution.Feed
theprocessesintothepipeonebyone.TreatmemoryaccesslikeIOsuspendthethreaduntilthe
datacomesbackfromthememory.Inthemeantime,executeotherthreads.Usecomputationto
hidethelatencyofaccessingmemory.
Whataboutareallybigquantum?ItturnsintoFCFS.Ruleofthumbwant80percentofCPU
burststobeshorterthantimequantum.
MultilevelQueueSchedulinglikeRR,excepthavemultiplequeues.Typically,classifyprocesses
intoseparatecategoriesandgiveaqueuetoeachcategory.So,mighthavesystem,interactiveand
batchprocesses,withtheprioritiesinthatorder.CouldalsoallocateapercentageoftheCPUto
eachqueue.
MultilevelFeedbackQueueSchedulingLikemultilevelscheduling,exceptprocessescanmove
betweenqueuesastheirprioritychanges.CanbeusedtogiveIOboundandinteractiveprocesses
CPUpriorityoverCPUboundprocesses.Canalsopreventstarvationbyincreasingthepriorityof
http://people.csail.mit.edu/rinard/osnotes/h6.html

3/5

6/27/2015

OperatingSystemsLectureNotesLecture6CPUScheduling

processesthathavebeenidleforalongtime.
Asimpleexampleofamultilevelfeedbackqueueschedulingalgorithm.Have3queues,numbered
0,1,2withcorrespondingpriority.So,forexample,executeataskinqueue2onlywhenqueues0
and1areempty.
Aprocessgoesintoqueue0whenitbecomesready.Whenrunaprocessfromqueue0,giveita
quantumof8ms.Ifitexpiresitsquantum,movetoqueue1.Whenexecuteaprocessfromqueue
1,giveitaquantumof16.Ifitexpiresitsquantum,movetoqueue2.Inqueue2,runaRR
schedulerwithalargequantumifinaninteractivesystemoranFCFSschedulerifinabatch
system.Ofcourse,preemptqueue2processeswhenanewprocessbecomesready.
Anotherexampleofamultilevelfeedbackqueueschedulingalgorithm:theUnixscheduler.We
willgooverasimplifiedversionthatdoesnotincludekernelpriorities.Thepointofthealgorithm
istofairlyallocatetheCPUbetweenprocesses,withprocessesthathavenotrecentlyusedalotof
CPUresourcesgivenpriorityoverprocessesthathave.
Processesaregivenabasepriorityof60,withlowernumbersrepresentinghigherpriorities.The
systemclockgeneratesaninterruptbetween50and100timesasecond,sowewillassumeavalue
of60clockinterruptspersecond.TheclockinterrupthandlerincrementsaCPUusagefieldinthe
PCBoftheinterruptedprocesseverytimeitruns.
Thesystemalwaysrunsthehighestpriorityprocess.Ifthereisatie,itrunstheprocessthathas
beenreadylongest.Everysecond,itrecalculatesthepriorityandCPUusagefieldforeveryprocess
accordingtothefollowingformulas.
CPUusagefield=CPUusagefield/2
Priority=CPUusagefield/2+basepriority
So,whenaprocessdoesnotusemuchCPUrecently,itspriorityrises.TheprioritiesofIObound
processesandinteractiveprocessesthereforetendtobehighandtheprioritiesofCPUbound
processestendtobelow(whichiswhatyouwant).
Unixalsoallowsuserstoprovidea``nice''valueforeachprocess.Nicevaluesmodifythepriority
calculationasfollows:
Priority=CPUusagefield/2+basepriority+nicevalue
So,youcanreducethepriorityofyourprocesstobe``nice''tootherprocesses(whichmayinclude
yourown).
Ingeneral,multilevelfeedbackqueueschedulersarecomplexpiecesofsoftwarethatmustbe
tunedtomeetrequirements.
Anomaliesandsystemeffectsassociatedwithschedulers.
Priorityinteractswithsynchronizationtocreateareallynastyeffectcalledpriorityinversion.A
priorityinversionhappenswhenalowprioritythreadacquiresalock,thenahighprioritythread
triestoacquirethelockandblocks.Anymiddleprioritythreadswillpreventthelowpriority
threadfromrunningandunlockingthelock.Ineffect,themiddleprioritythreadsblockthehigh
prioritythread.
Howtopreventpriorityinversions?Usepriorityinheritance.Anytimeathreadholdsalockthat
otherthreadsarewaitingon,givethethreadthepriorityofthehighestprioritythreadwaitingto
getthelock.Problemisthatpriorityinheritancemakestheschedulingalgorithmlessefficientand
increasestheoverhead.
Preemptioncaninteractwithsynchronizationinamultiprocessorcontexttocreateanothernasty
effecttheconvoyeffect.Onethreadacquiresthelock,thensuspends.Otherthreadscomealong,
andneedtoacquirethelocktoperformtheiroperations.Everybodysuspendsuntilthelockthat
hasthethreadwakesup.Atthispointthethreadsaresynchronized,andwillconvoytheirway
throughthelock,serializingthecomputation.So,drivesdowntheprocessorutilization.
IfhavenonblockingsynchronizationviaoperationslikeLL/SC,don'tgetconvoyeffectscaused
bysuspendingathreadcompetingforaccesstoaresource.Whynot?Becausethreadsdon'thold
resourcesandpreventotherthreadsfromaccessingthem.
http://people.csail.mit.edu/rinard/osnotes/h6.html

4/5

6/27/2015

OperatingSystemsLectureNotesLecture6CPUScheduling

SimilareffectwhenschedulingCPUandIOboundprocesses.ConsideraFCFSalgorithmwith
severalIOboundandoneCPUboundprocess.AlloftheIOboundprocessesexecutetheirbursts
quicklyandqueueupforaccesstotheIOdevice.TheCPUboundprocessthenexecutesforalong
time.DuringthistimealloftheIOboundprocesseshavetheirIOrequestssatisfiedandmoveback
intotherunqueue.Buttheydon'truntheCPUboundprocessisrunninginsteadsotheIO
deviceidles.Finally,theCPUboundprocessgetsofftheCPU,andalloftheIOboundprocesses
runforashorttimethenqueueupagainfortheIOdevices.ResultispoorutilizationofIOdevice
itisbusyforatimewhileitprocessestheIOrequests,thenidlewhiletheIOboundprocesseswait
intherunqueuesfortheirshortCPUbursts.InthiscaseaneasysolutionistogiveIObound
processespriorityoverCPUboundprocesses.
Ingeneral,aconvoyeffecthappenswhenasetofprocessesneedtousearesourceforashorttime,
andoneprocessholdstheresourceforalongtime,blockingalloftheotherprocesses.Causespoor
utilizationoftheotherresourcesinthesystem.
Permissionisgrantedtocopyanddistributethismaterialforeducationalpurposesonly,providedthatthefollowingcredit
lineisincluded:"OperatingSystemsLectureNotes,Copyright1997MartinC.Rinard."Permissionisgrantedtoalterand
distributethismaterialprovidedthatthefollowingcreditlineisincluded:"AdaptedfromOperatingSystemsLectureNotes,
Copyright1997MartinC.Rinard."

MartinRinard,rinard@lcs.mit.edu,www.cag.lcs.mit.edu/~rinard
8/22/1998

http://people.csail.mit.edu/rinard/osnotes/h6.html

5/5

Potrebbero piacerti anche