Operating Systems_ CPU Scheduling
-
Upload
nitin-nilesh -
Category
Documents
-
view
23 -
download
3
description
Transcript of Operating Systems_ CPU Scheduling
-
2/19/2015 OperatingSystems:CPUScheduling
http://www.cs.uic.edu/~jbell/CourseNotes/OperatingSystems/5_CPU_Scheduling.html 1/20
CPUSchedulingReferences:
1. AbrahamSilberschatz,GregGagne,andPeterBaerGalvin,"OperatingSystemConcepts,EighthEdition",Chapter5
5.1BasicConcepts
AlmostallprogramshavesomealternatingcycleofCPUnumbercrunchingandwaitingforI/Oofsomekind.(EvenasimplefetchfrommemorytakesalongtimerelativetoCPUspeeds.)Inasimplesystemrunningasingleprocess,thetimespentwaitingforI/Oiswasted,andthoseCPUcyclesarelostforever.AschedulingsystemallowsoneprocesstousetheCPUwhileanotheriswaitingforI/O,therebymakingfulluseofotherwiselostCPUcycles.Thechallengeistomaketheoverallsystemas"efficient"and"fair"aspossible,subjecttovaryingandoftendynamicconditions,andwhere"efficient"and"fair"aresomewhatsubjectiveterms,oftensubjecttoshiftingprioritypolicies.
5.1.1CPUI/OBurstCycle
Almostallprocessesalternatebetweentwostatesinacontinuingcycle,asshowninFigure5.1below:ACPUburstofperformingcalculations,andAnI/Oburst,waitingfordatatransferinoroutofthesystem.
CPUburstsvaryfromprocesstoprocess,andfromprogramtoprogram,butanextensivestudyshows
-
2/19/2015 OperatingSystems:CPUScheduling
http://www.cs.uic.edu/~jbell/CourseNotes/OperatingSystems/5_CPU_Scheduling.html 2/20
frequencypatternssimilartothatshowninFigure5.2:
5.1.2CPUScheduler
WhenevertheCPUbecomesidle,itisthejoboftheCPUScheduler(a.k.a.theshorttermscheduler)toselectanotherprocessfromthereadyqueuetorunnext.ThestoragestructureforthereadyqueueandthealgorithmusedtoselectthenextprocessarenotnecessarilyaFIFOqueue.Thereareseveralalternativestochoosefrom,aswellasnumerousadjustableparametersforeachalgorithm,whichisthebasicsubjectofthisentirechapter.
5.1.3.PreemptiveScheduling
CPUschedulingdecisionstakeplaceunderoneoffourconditions:1. Whenaprocessswitchesfromtherunningstatetothewaitingstate,suchasforanI/Orequestor
invocationofthewait()systemcall.2. Whenaprocessswitchesfromtherunningstatetothereadystate,forexampleinresponsetoan
interrupt.3. Whenaprocessswitchesfromthewaitingstatetothereadystate,sayatcompletionofI/Oorareturn
fromwait().4. Whenaprocessterminates.
Forconditions1and4thereisnochoiceAnewprocessmustbeselected.Forconditions2and3thereisachoiceToeithercontinuerunningthecurrentprocess,orselectadifferentone.Ifschedulingtakesplaceonlyunderconditions1and4,thesystemissaidtobenonpreemptive,orcooperative.Undertheseconditions,onceaprocessstartsrunningitkeepsrunning,untiliteithervoluntarilyblocksoruntilitfinishes.Otherwisethesystemissaidtobepreemptive.WindowsusednonpreemptiveschedulinguptoWindows3.x,andstartedusingpreemptiveschedulingwithWin95.MacsusednonpreemptivepriortoOSX,andpreemptivesincethen.Notethatpreemptiveschedulingisonlypossibleonhardwarethatsupportsatimerinterrupt.Notethatpreemptiveschedulingcancauseproblemswhentwoprocessessharedata,becauseoneprocessmaygetinterruptedinthemiddleofupdatingshareddatastructures.Chapter6willexaminethisissueingreaterdetail.Preemptioncanalsobeaproblemifthekernelisbusyimplementingasystemcall(e.g.updatingcriticalkerneldatastructures)whenthepreemptionoccurs.MostmodernUNIXesdealwiththisproblembymaking
-
2/19/2015 OperatingSystems:CPUScheduling
http://www.cs.uic.edu/~jbell/CourseNotes/OperatingSystems/5_CPU_Scheduling.html 3/20
theprocesswaituntilthesystemcallhaseithercompletedorblockedbeforeallowingthepreemptionUnfortunatelythissolutionisproblematicforrealtimesystems,asrealtimeresponsecannolongerbeguaranteed.Somecriticalsectionsofcodeprotectthemselvesfromconcurrencyproblemsbydisablinginterruptsbeforeenteringthecriticalsectionandreenablinginterruptsonexitingthesection.Needlesstosay,thisshouldonlybedoneinraresituations,andonlyonveryshortpiecesofcodethatwillfinishquickly,(usuallyjustafewmachineinstructions.)
5.1.4Dispatcher
ThedispatcheristhemodulethatgivescontroloftheCPUtotheprocessselectedbythescheduler.Thisfunctioninvolves:
Switchingcontext.Switchingtousermode.Jumpingtotheproperlocationinthenewlyloadedprogram.
Thedispatcherneedstobeasfastaspossible,asitisrunoneverycontextswitch.Thetimeconsumedbythedispatcherisknownasdispatchlatency.
5.2SchedulingCriteria
Thereareseveraldifferentcriteriatoconsiderwhentryingtoselectthe"best"schedulingalgorithmforaparticularsituationandenvironment,including:
CPUutilizationIdeallytheCPUwouldbebusy100%ofthetime,soastowaste0CPUcycles.OnarealsystemCPUusageshouldrangefrom40%(lightlyloaded)to90%(heavilyloaded.)ThroughputNumberofprocessescompletedperunittime.Mayrangefrom10/secondto1/hourdependingonthespecificprocesses.TurnaroundtimeTimerequiredforaparticularprocesstocomplete,fromsubmissiontimetocompletion.(Wallclocktime.)WaitingtimeHowmuchtimeprocessesspendinthereadyqueuewaitingtheirturntogetontheCPU.
(LoadaverageTheaveragenumberofprocessessittinginthereadyqueuewaitingtheirturntogetintotheCPU.Reportedin1minute,5minute,and15minuteaveragesby"uptime"and"who".)
ResponsetimeThetimetakeninaninteractiveprogramfromtheissuanceofacommandtothecommenceofaresponsetothatcommand.
Ingeneralonewantstooptimizetheaveragevalueofacriteria(MaximizeCPUutilizationandthroughput,andminimizealltheothers.)Howeversometimesonewantstodosomethingdifferent,suchastominimizethemaximumresponsetime.Sometimesitismostdesirabletominimizethevarianceofacriteriathantheactualvalue.I.e.usersaremoreacceptingofaconsistentpredictablesystemthananinconsistentone,evenifitisalittlebitslower.
5.3SchedulingAlgorithms
Thefollowingsubsectionswillexplainseveralcommonschedulingstrategies,lookingatonlyasingleCPUbursteachforasmallnumberofprocesses.ObviouslyrealsystemshavetodealwithalotmoresimultaneousprocessesexecutingtheirCPUI/Oburstcycles.
5.3.1FirstComeFirstServeScheduling,FCFS
FCFSisverysimpleJustaFIFOqueue,likecustomerswaitinginlineatthebankorthepostofficeoratacopyingmachine.Unfortunately,however,FCFScanyieldsomeverylongaveragewaittimes,particularlyifthefirstprocesstogettheretakesalongtime.Forexample,considerthefollowingthreeprocesses:
Process BurstTime
P1 24
P2 3
P3 3
InthefirstGanttchartbelow,processP1arrivesfirst.Theaveragewaitingtimeforthethreeprocessesis(0+24+27)/3=17.0ms.InthesecondGanttchartbelow,thesamethreeprocesseshaveanaveragewaittimeof(0+3+6)/3=3.0ms.Thetotalruntimeforthethreeburstsisthesame,butinthesecondcasetwoofthethreefinishmuchquicker,andtheotherprocessisonlydelayedbyashortamount.
-
2/19/2015 OperatingSystems:CPUScheduling
http://www.cs.uic.edu/~jbell/CourseNotes/OperatingSystems/5_CPU_Scheduling.html 4/20
FCFScanalsoblockthesysteminabusydynamicsysteminanotherway,knownastheconvoyeffect.WhenoneCPUintensiveprocessblockstheCPU,anumberofI/Ointensiveprocessescangetbackedupbehindit,leavingtheI/Odevicesidle.WhentheCPUhogfinallyrelinquishestheCPU,thentheI/OprocessespassthroughtheCPUquickly,leavingtheCPUidlewhileeveryonequeuesupforI/O,andthenthecyclerepeatsitselfwhentheCPUintensiveprocessgetsbacktothereadyqueue.
5.3.2ShortestJobFirstScheduling,SJF
TheideabehindtheSJFalgorithmistopickthequickestfastestlittlejobthatneedstobedone,getitoutofthewayfirst,andthenpickthenextsmallestfastestjobtodonext.(TechnicallythisalgorithmpicksaprocessbasedonthenextshortestCPUburst,nottheoverallprocesstime.)Forexample,theGanttchartbelowisbaseduponthefollowingCPUbursttimes,(andtheassumptionthatalljobsarriveatthesametime.)
Process BurstTime
P1 6
P2 8
P3 7
P4 3
Inthecaseabovetheaveragewaittimeis(0+3+9+16)/4=7.0ms,(asopposedto10.25msforFCFSforthesameprocesses.)SJFcanbeproventobethefastestschedulingalgorithm,butitsuffersfromoneimportantproblem:HowdoyouknowhowlongthenextCPUburstisgoingtobe?
Forlongtermbatchjobsthiscanbedonebaseduponthelimitsthatuserssetfortheirjobswhentheysubmitthem,whichencouragesthemtosetlowlimits,butriskstheirhavingtoresubmitthejobiftheysetthelimittoolow.HoweverthatdoesnotworkforshorttermCPUschedulingonaninteractivesystem.Anotheroptionwouldbetostatisticallymeasuretheruntimecharacteristicsofjobs,particularlyifthesametasksarerunrepeatedlyandpredictably.Butonceagainthatreallyisn'taviableoptionforshorttermCPUschedulingintherealworld.Amorepracticalapproachistopredictthelengthofthenextburst,basedonsomehistoricalmeasurementofrecentbursttimesforthisprocess.Onesimple,fast,andrelativelyaccuratemethodistheexponentialaverage,whichcanbedefinedasfollows.(Thebookusestauandtfortheirvariables,butthosearehardtodistinguishfromoneanotheranddon'tworkwellinHTML.)
estimate[i+1]=alpha*burst[i]+(1.0alpha)*estimate[i]
Inthisschemethepreviousestimatecontainsthehistoryofallprevioustimes,andalphaservesasaweightingfactorfortherelativeimportanceofrecentdataversuspasthistory.Ifalphais1.0,thenpasthistoryisignored,andweassumethenextburstwillbethesamelengthasthelastburst.Ifalphais0.0,thenallmeasuredbursttimesareignored,andwejustassumeaconstantbursttime.Mostcommonlyalphaissetat0.5,asillustratedinFigure5.3:
-
2/19/2015 OperatingSystems:CPUScheduling
http://www.cs.uic.edu/~jbell/CourseNotes/OperatingSystems/5_CPU_Scheduling.html 5/20
SJFcanbeeitherpreemptiveornonpreemptive.PreemptionoccurswhenanewprocessarrivesinthereadyqueuethathasapredictedbursttimeshorterthanthetimeremainingintheprocesswhoseburstiscurrentlyontheCPU.PreemptiveSJFissometimesreferredtoasshortestremainingtimefirstscheduling.Forexample,thefollowingGanttchartisbaseduponthefollowingdata:
Process ArrivalTime BurstTime
P1 0 8
P2 1 4
P3 2 9
p4 3 5
Theaveragewaittimeinthiscaseis((53)+(101)+(172))/4=26/4=6.5ms.(Asopposedto7.75msfornonpreemptiveSJFor8.75forFCFS.)
5.3.3PriorityScheduling
PriorityschedulingisamoregeneralcaseofSJF,inwhicheachjobisassignedapriorityandthejobwiththehighestprioritygetsscheduledfirst.(SJFusestheinverseofthenextexpectedbursttimeasitspriorityThesmallertheexpectedburst,thehigherthepriority.)Notethatinpractice,prioritiesareimplementedusingintegerswithinafixedrange,butthereisnoagreeduponconventionastowhether"high"prioritiesuselargenumbersorsmallnumbers.Thisbookuseslownumberforhighpriorities,with0beingthehighestpossiblepriority.Forexample,thefollowingGanttchartisbasedupontheseprocessbursttimesandpriorities,andyieldsan
-
2/19/2015 OperatingSystems:CPUScheduling
http://www.cs.uic.edu/~jbell/CourseNotes/OperatingSystems/5_CPU_Scheduling.html 6/20
averagewaitingtimeof8.2ms:
Process BurstTime Priority
P1 10 3
P2 1 1
P3 2 4
P4 1 5
P5 5 2
Prioritiescanbeassignedeitherinternallyorexternally.InternalprioritiesareassignedbytheOSusingcriteriasuchasaveragebursttime,ratioofCPUtoI/Oactivity,systemresourceuse,andotherfactorsavailabletothekernel.Externalprioritiesareassignedbyusers,basedontheimportanceofthejob,feespaid,politics,etc.Priorityschedulingcanbeeitherpreemptiveornonpreemptive.Priorityschedulingcansufferfromamajorproblemknownasindefiniteblocking,orstarvation,inwhichalowprioritytaskcanwaitforeverbecausetherearealwayssomeotherjobsaroundthathavehigherpriority.
Ifthisproblemisallowedtooccur,thenprocesseswilleitherruneventuallywhenthesystemloadlightens(atsay2:00a.m.),orwilleventuallygetlostwhenthesystemisshutdownorcrashes.(Therearerumorsofjobsthathavebeenstuckforyears.)Onecommonsolutiontothisproblemisaging,inwhichprioritiesofjobsincreasethelongertheywait.Underthisschemealowpriorityjobwilleventuallygetitspriorityraisedhighenoughthatitgetsrun.
5.3.4RoundRobinScheduling
RoundrobinschedulingissimilartoFCFSscheduling,exceptthatCPUburstsareassignedwithlimitscalledtimequantum.WhenaprocessisgiventheCPU,atimerissetforwhatevervaluehasbeensetforatimequantum.
Iftheprocessfinishesitsburstbeforethetimequantumtimerexpires,thenitisswappedoutoftheCPUjustlikethenormalFCFSalgorithm.Ifthetimergoesofffirst,thentheprocessisswappedoutoftheCPUandmovedtothebackendofthereadyqueue.
Thereadyqueueismaintainedasacircularqueue,sowhenallprocesseshavehadaturn,thentheschedulergivesthefirstprocessanotherturn,andsoon.RRschedulingcangivetheeffectofallprocessorssharingtheCPUequally,althoughtheaveragewaittimecanbelongerthanwithotherschedulingalgorithms.Inthefollowingexampletheaveragewaittimeis5.66ms.
Process BurstTime
P1 24
P2 3
P3 3
TheperformanceofRRissensitivetothetimequantumselected.Ifthequantumislargeenough,thenRRreducestotheFCFSalgorithmIfitisverysmall,theneachprocessgets1/nthoftheprocessortimeandsharetheCPUequally.BUT,arealsysteminvokesoverheadforeverycontextswitch,andthesmallerthetimequantumthemorecontextswitchesthereare.(SeeFigure5.4below.)Mostmodernsystemsusetimequantumbetween10and
-
2/19/2015 OperatingSystems:CPUScheduling
http://www.cs.uic.edu/~jbell/CourseNotes/OperatingSystems/5_CPU_Scheduling.html 7/20
100milliseconds,andcontextswitchtimesontheorderof10microseconds,sotheoverheadissmallrelativetothetimequantum.
Turnaroundtimealsovarieswithquantumtime,inanonapparentmanner.Consider,forexampletheprocessesshowninFigure5.5:
Ingeneral,turnaroundtimeisminimizedifmostprocessesfinishtheirnextcpuburstwithinonetimequantum.Forexample,withthreeprocessesof10msburstseach,theaverageturnaroundtimefor1msquantumis29,andfor10msquantumitreducesto20.However,ifitismadetoolarge,thenRRjustdegeneratestoFCFS.Aruleofthumbisthat80%ofCPUburstsshouldbesmallerthanthetimequantum.
5.3.5MultilevelQueueScheduling
-
2/19/2015 OperatingSystems:CPUScheduling
http://www.cs.uic.edu/~jbell/CourseNotes/OperatingSystems/5_CPU_Scheduling.html 8/20
Whenprocessescanbereadilycategorized,thenmultipleseparatequeuescanbeestablished,eachimplementingwhateverschedulingalgorithmismostappropriateforthattypeofjob,and/orwithdifferentparametricadjustments.Schedulingmustalsobedonebetweenqueues,thatisschedulingonequeuetogettimerelativetootherqueues.Twocommonoptionsarestrictpriority(nojobinalowerpriorityqueuerunsuntilallhigherpriorityqueuesareempty)androundrobin(eachqueuegetsatimesliceinturn,possiblyofdifferentsizes.)NotethatunderthisalgorithmjobscannotswitchfromqueuetoqueueOncetheyareassignedaqueue,thatistheirqueueuntiltheyfinish.
5.3.6MultilevelFeedbackQueueScheduling
Multilevelfeedbackqueueschedulingissimilartotheordinarymultilevelqueueschedulingdescribedabove,exceptjobsmaybemovedfromonequeuetoanotherforavarietyofreasons:
IfthecharacteristicsofajobchangebetweenCPUintensiveandI/Ointensive,thenitmaybeappropriatetoswitchajobfromonequeuetoanother.Agingcanalsobeincorporated,sothatajobthathaswaitedforalongtimecangetbumpedupintoahigherpriorityqueueforawhile.
Multilevelfeedbackqueueschedulingisthemostflexible,becauseitcanbetunedforanysituation.Butitisalsothemostcomplextoimplementbecauseofalltheadjustableparameters.Someoftheparameterswhichdefineoneofthesesystemsinclude:
Thenumberofqueues.Theschedulingalgorithmforeachqueue.Themethodsusedtoupgradeordemoteprocessesfromonequeuetoanother.(Whichmaybedifferent.)Themethodusedtodeterminewhichqueueaprocessentersinitially.
-
2/19/2015 OperatingSystems:CPUScheduling
http://www.cs.uic.edu/~jbell/CourseNotes/OperatingSystems/5_CPU_Scheduling.html 9/20
5.4ThreadScheduling
Theprocessschedulerschedulesonlythekernelthreads.UserthreadsaremappedtokernelthreadsbythethreadlibraryTheOS(andinparticularthescheduler)isunawareofthem.
5.4.1ContentionScope
ContentionscopereferstothescopeinwhichthreadscompetefortheuseofphysicalCPUs.Onsystemsimplementingmanytooneandmanytomanythreads,ProcessContentionScope,PCS,occurs,becausecompetitionoccursbetweenthreadsthatarepartofthesameprocess.(Thisisthemanagement/schedulingofmultipleuserthreadsonasinglekernelthread,andismanagedbythethreadlibrary.)SystemContentionScope,SCS,involvesthesystemschedulerschedulingkernelthreadstorunononeormoreCPUs.Systemsimplementingonetoonethreads(XP,Solaris9,Linux),useonlySCS.PCSschedulingistypicallydonewithpriority,wheretheprogrammercansetand/orchangethepriorityofthreadscreatedbyhisorherprograms.Eventimeslicingisnotguaranteedamongthreadsofequalpriority.
5.4.2PthreadScheduling
ThePthreadlibraryprovidesforspecifyingscopecontention:PTHREAD_SCOPE_PROCESSschedulesthreadsusingPCS,byschedulinguserthreadsontoavailableLWPsusingthemanytomanymodel.PTHREAD_SCOPE_SYSTEMschedulesthreadsusingSCS,bybindinguserthreadstoparticularLWPs,effectivelyimplementingaonetoonemodel.
getscopeandsetscopemethodsprovidefordeterminingandsettingthescopecontentionrespectively:
-
2/19/2015 OperatingSystems:CPUScheduling
http://www.cs.uic.edu/~jbell/CourseNotes/OperatingSystems/5_CPU_Scheduling.html 10/20
-
2/19/2015 OperatingSystems:CPUScheduling
http://www.cs.uic.edu/~jbell/CourseNotes/OperatingSystems/5_CPU_Scheduling.html 11/20
Figure5.8
5.5MultipleProcessorScheduling
Whenmultipleprocessorsareavailable,thentheschedulinggetsmorecomplicated,becausenowthereismorethanoneCPUwhichmustbekeptbusyandineffectiveuseatalltimes.Loadsharingrevolvesaroundbalancingtheloadbetweenmultipleprocessors.Multiprocessorsystemsmaybeheterogeneous,(differentkindsofCPUs),orhomogenous,(allthesamekindofCPU).Eveninthelattercasetheremaybespecialschedulingconstraints,suchasdeviceswhichareconnectedviaaprivatebustoonlyoneoftheCPUs.Thisbookwillrestrictitsdiscussiontohomogenoussystems.
5.5.1ApproachestoMultipleProcessorScheduling
Oneapproachtomultiprocessorschedulingisasymmetricmultiprocessing,inwhichoneprocessoristhemaster,controllingallactivitiesandrunningallkernelcode,whiletheotherrunsonlyusercode.Thisapproachisrelativelysimple,asthereisnoneedtosharecriticalsystemdata.Anotherapproachissymmetricmultiprocessing,SMP,whereeachprocessorschedulesitsownjobs,eitherfromacommonreadyqueueorfromseparatereadyqueuesforeachprocessor.VirtuallyallmodernOSessupportSMP,includingXP,Win2000,Solaris,Linux,andMacOSX.
5.5.2ProcessorAffinity
Processorscontaincachememory,whichspeedsuprepeatedaccessestothesamememorylocations.Ifaprocessweretoswitchfromoneprocessortoanothereachtimeitgotatimeslice,thedatainthecache(forthatprocess)wouldhavetobeinvalidatedandreloadedfrommainmemory,therebyobviatingthebenefitofthecache.ThereforeSMPsystemsattempttokeepprocessesonthesameprocessor,viaprocessoraffinity.Softaffinityoccurswhenthesystemattemptstokeepprocessesonthesameprocessorbutmakesnoguarantees.LinuxandsomeotherOSessupporthardaffinity,inwhichaprocessspecifiesthatitisnottobemovedbetweenprocessors.Mainmemoryarchitecturecanalsoaffectprocessaffinity,ifparticularCPUshavefasteraccesstomemoryonthesamechiporboardthantoothermemoryloadedelsewhere.(NonUniformMemoryAccess,NUMA.)Asshownbelow,ifaprocesshasanaffinityforaparticularCPU,thenitshouldpreferentiallybeassignedmemorystoragein"local"fastaccessareas.
5.5.3LoadBalancing
Obviouslyanimportantgoalinamultiprocessorsystemistobalancetheloadbetweenprocessors,sothatoneprocessorwon'tbesittingidlewhileanotherisoverloaded.Systemsusingacommonreadyqueuearenaturallyselfbalancing,anddonotneedanyspecialhandling.Mostsystems,however,maintainseparatereadyqueuesforeachprocessor.
-
2/19/2015 OperatingSystems:CPUScheduling
http://www.cs.uic.edu/~jbell/CourseNotes/OperatingSystems/5_CPU_Scheduling.html 12/20
Balancingcanbeachievedthrougheitherpushmigrationorpullmigration:Pushmigrationinvolvesaseparateprocessthatrunsperiodically,(e.g.every200milliseconds),andmovesprocessesfromheavilyloadedprocessorsontolessloadedones.Pullmigrationinvolvesidleprocessorstakingprocessesfromthereadyqueuesofotherprocessors.Pushandpullmigrationarenotmutuallyexclusive.
Notethatmovingprocessesfromprocessortoprocessortoachieveloadbalancingworksagainsttheprincipleofprocessoraffinity,andifnotcarefullymanaged,thesavingsgainedbybalancingthesystemcanbelostinrebuildingcaches.Oneoptionistoonlyallowmigrationwhenimbalancesurpassesagiventhreshold.
5.5.4MulticoreProcessors
TraditionalSMPrequiredmultipleCPUchipstorunmultiplekernelthreadsconcurrently.RecenttrendsaretoputmultipleCPUs(cores)ontoasinglechip,whichappeartothesystemasmultipleprocessors.Computecyclescanbeblockedbythetimeneededtoaccessmemory,whenevertheneededdataisnotalreadypresentinthecache.(Cachemisses.)InFigure5.10,asmuchashalfoftheCPUcyclesarelosttomemorystall.
Byassigningmultiplekernelthreadstoasingleprocessor,memorystallcanbeavoided(orreduced)byrunningonethreadontheprocessorwhiletheotherthreadwaitsformemory.
Adualthreadeddualcoresystemhasfourlogicalprocessorsavailabletotheoperatingsystem.TheUltraSPARCT1CPUhas8coresperchipand4hardwarethreadspercore,foratotalof32logicalprocessorsperchip.Therearetwowaystomultithreadaprocessor:
1. Coarsegrainedmultithreadingswitchesbetweenthreadsonlywhenonethreadblocks,sayonamemoryread.Contextswitchingissimilartoprocessswitching,withconsiderableoverhead.
2. Finegrainedmultithreadingoccursonsmallerregularintervals,sayontheboundaryofinstructioncycles.Howeverthearchitectureisdesignedtosupportthreadswitching,sotheoverheadisrelativelyminor.
Notethatforamultithreadedmulticoresystem,therearetwolevelsofscheduling,atthekernellevel:TheOSscheduleswhichkernelthread(s)toassigntowhichlogicalprocessors,andwhentomakecontextswitchesusingalgorithmsasdescribedabove.Onalowerlevel,thehardwarescheduleslogicalprocessorsoneachphysicalcoreusingsomeotheralgorithm.
-
2/19/2015 OperatingSystems:CPUScheduling
http://www.cs.uic.edu/~jbell/CourseNotes/OperatingSystems/5_CPU_Scheduling.html 13/20
TheUltraSPARCT1usesasimpleroundrobinmethodtoschedulethe4logicalprocessors(kernelthreads)oneachphysicalcore.TheIntelItaniumisadualcorechipwhichusesa7levelpriorityscheme(urgency)todeterminewhichthreadtoschedulewhenoneof5differenteventsoccurs.
5.5.5VirtualizationandScheduling
Virtualizationaddsanotherlayerofcomplexityandscheduling.Typicallythereisonehostoperatingsystemoperatingon"real"processor(s)andanumberofguestoperatingsystemsoperatingonvirtualprocessors.TheHostOScreatessomenumberofvirtualprocessorsandpresentsthemtotheguestOSesasiftheywererealprocessors.TheguestOSesdon'trealizetheirprocessorsarevirtual,andmakeschedulingdecisionsontheassumptionofrealprocessors.Asaresult,interactiveandespeciallyrealtimeperformancecanbeseverelycompromisedonguestsystems.Thetimeofdayclockwillalsofrequentlybeoff.
5.6OperatingSystemExamples
5.6.1Example:SolarisScheduling
Prioritybasedkernelthreadscheduling.Fourclasses(realtime,system,interactive,andtimesharing),andmultiplequeues/algorithmswithineachclass.Defaultistimesharing.
Processprioritiesandtimeslicesareadjusteddynamicallyinamultilevelfeedbackpriorityqueuesystem.TimeslicesareinverselyproportionaltopriorityHigherpriorityjobsgetsmallertimeslices.InteractivejobshavehigherprioritythanCPUBoundones.Seethetablebelowforsomeofthe60prioritylevelsandhowtheyshift."Timequantumexpired"and"returnfromsleep"indicatethenewprioritywhenthoseeventsoccur.(Largernumbersareahigher,i.e.betterpriority.)
-
2/19/2015 OperatingSystems:CPUScheduling
http://www.cs.uic.edu/~jbell/CourseNotes/OperatingSystems/5_CPU_Scheduling.html 14/20
Figure5.12
Solaris9introducedtwonewschedulingclasses:Fixedpriorityandfairshare.Fixedpriorityissimilartotimesharing,butnotadjusteddynamically.FairshareusessharesofCPUtimeratherthanprioritiestoschedulejobs.AcertainshareoftheavailableCPUtimeisallocatedtoaproject,whichisasetofprocesses.
Systemclassisreservedforkerneluse.(UserprogramsrunninginkernelmodeareNOTconsideredinthesystemschedulingclass.)
-
2/19/2015 OperatingSystems:CPUScheduling
http://www.cs.uic.edu/~jbell/CourseNotes/OperatingSystems/5_CPU_Scheduling.html 15/20
5.6.2Example:WindowsXPScheduling
WindowsXPusesaprioritybasedpreemptiveschedulingalgorithm.Thedispatcherusesa32levelpriorityschemetodeterminetheorderofthreadexecution,dividedintotwoclassesvariableclassfrom1to15andrealtimeclassfrom16to31,(plusathreadatpriority0managingmemory.)Thereisalsoaspecialidlethreadthatisscheduledwhennootherthreadsareready.WinXPidentifies7priorityclasses(rowsonthetablebelow),and6relativeprioritieswithineachclass(columns.)Processesarealsoeachgivenabaseprioritywithintheirpriorityclass.Whenvariableclassprocessesconsumetheirentiretimequanta,thentheirprioritygetslowered,butnotbelowtheirbasepriority.Processesintheforeground(activewindow)havetheirschedulingquantamultipliedby3,togivebetterresponsetointeractiveprocessesintheforeground.
-
2/19/2015 OperatingSystems:CPUScheduling
http://www.cs.uic.edu/~jbell/CourseNotes/OperatingSystems/5_CPU_Scheduling.html 16/20
Figure5.14
5.6.3Example:LinuxScheduling
ModernLinuxschedulingprovidesimprovedsupportforSMPsystems,andaschedulingalgorithmthatrunsinO(1)timeasthenumberofprocessesincreases.TheLinuxschedulerisapreemptiveprioritybasedalgorithmwithtwopriorityrangesRealtimefrom0to99andanicerangefrom100to140.UnlikeSolarisorXP,Linuxassignslongertimequantumstohigherprioritytasks.
Figure5.15
Arunnabletaskisconsideredeligibleforexecutionaslongasithasnotconsumedallthetimeavailableinit'stimeslice.Thosetasksarestoredinanactivearray,indexedaccordingtopriority.Whenaprocessconsumesitstimeslice,itismovedtoanexpiredarray.Thetasksprioritymaybereassignedaspartofthetransferal.Whentheactivearraybecomesempty,thetwoarraysareswapped.Thesearraysarestoredinrunqueuestructures.Onmultiprocessormachines,eachprocessorhasitsownschedulerwithitsownrunqueue.
-
2/19/2015 OperatingSystems:CPUScheduling
http://www.cs.uic.edu/~jbell/CourseNotes/OperatingSystems/5_CPU_Scheduling.html 17/20
Figure5.16
5.7AlgorithmEvaluation
Thefirststepindeterminingwhichalgorithm(andwhatparametersettingswithinthatalgorithm)isoptimalforaparticularoperatingenvironmentistodeterminewhatcriteriaaretobeused,whatgoalsaretobetargeted,andwhatconstraintsifanymustbeapplied.Forexample,onemightwantto"maximizeCPUutilization,subjecttoamaximumresponsetimeof1second".Oncecriteriahavebeenestablished,thendifferentalgorithmscanbeanalyzedanda"bestchoice"determined.Thefollowingsectionsoutlinesomedifferentmethodsfordeterminingthe"bestchoice".
5.7.1DeterministicModeling
Ifaspecificworkloadisknown,thentheexactvaluesformajorcriteriacanbefairlyeasilycalculated,andthe"best"determined.Forexample,considerthefollowingworkload(withallprocessesarrivingattime0),andtheresultingschedulesdeterminedbythreedifferentalgorithms:
Process BurstTime
P1 10
P2 29
P3 3
P4 7
P5 12
-
2/19/2015 OperatingSystems:CPUScheduling
http://www.cs.uic.edu/~jbell/CourseNotes/OperatingSystems/5_CPU_Scheduling.html 18/20
TheaveragewaitingtimesforFCFS,SJF,andRRare28ms,13ms,and23msrespectively.Deterministicmodelingisfastandeasy,butitrequiresspecificknowninput,andtheresultsonlyapplyforthatparticularsetofinput.Howeverbyexaminingmultiplesimilarcases,certaintrendscanbeobserved.(Likethefactthatforprocessesarrivingatthesametime,SJFwillalwaysyieldtheshortestaveragewaittime.)
5.7.2QueuingModels
Specificprocessdataisoftennotavailable,particularlyforfuturetimes.Howeverastudyofhistoricalperformancecanoftenproducestatisticaldescriptionsofcertainimportantparameters,suchastherateatwhichnewprocessesarrive,theratioofCPUburststoI/Otimes,thedistributionofCPUbursttimesandI/Obursttimes,etc.Armedwiththoseprobabilitydistributionsandsomemathematicalformulas,itispossibletocalculatecertainperformancecharacteristicsofindividualwaitingqueues.Forexample,Little'sFormulasaysthatforanaveragequeuelengthofN,withanaveragewaitingtimeinthequeueofW,andanaveragearrivalofnewjobsinthequeueofLambda,thenthesethreetermscanberelatedby:
N=Lambda*W
Queuingmodelstreatthecomputerasanetworkofinterconnectedqueues,eachofwhichisdescribedbyitsprobabilitydistributionstatisticsandformulassuchasLittle'sformula.Unfortunatelyrealsystemsandmodernschedulingalgorithmsaresocomplexastomakethemathematicsintractableinmanycaseswithrealsystems.
5.7.3Simulations
Anotherapproachistoruncomputersimulationsofthedifferentproposedalgorithms(andadjustmentparameters)underdifferentloadconditions,andtoanalyzetheresultstodeterminethe"best"choiceofoperationforaparticularloadpattern.Operatingconditionsforsimulationsareoftenrandomlygeneratedusingdistributionfunctionssimilartothosedescribedabove.Abetteralternativewhenpossibleistogeneratetracetapes,bymonitoringandloggingtheperformanceofarealsystemundertypicalexpectedworkloads.Thesearebetterbecausetheyprovideamoreaccuratepictureofsystemloads,andalsobecausetheyallowmultiplesimulationstoberunwiththeidenticalprocessload,andnotjuststatisticallyequivalentloads.Acompromiseistorandomlydeterminesystemloadsandthensavetheresultsintoafile,sothatallsimulationscanberunagainstidenticalrandomlydeterminedsystemloads.Althoughtracetapesprovidemoreaccurateinputinformation,theycanbedifficultandexpensivetocollectandstore,andtheiruseincreasesthecomplexityofthesimulationssignificantly.Thereisalsosomequestionastowhetherthefutureperformanceofthenewsystemwillreallymatchthepastperformanceoftheoldsystem.(Ifthesystemrunsfaster,usersmaytakefewercoffeebreaks,andsubmitmoreprocessesperhourthanundertheoldsystem.Converselyiftheturnaroundtimeforjobsislonger,intelligentusersmaythinkmorecarefullyaboutthejobstheysubmitratherthanrandomlysubmittingjobsandhopingthatoneofthemworksout.)
-
2/19/2015 OperatingSystems:CPUScheduling
http://www.cs.uic.edu/~jbell/CourseNotes/OperatingSystems/5_CPU_Scheduling.html 19/20
Figure5.17
5.7.4Implementation
Theonlyrealwaytodeterminehowaproposedschedulingalgorithmisgoingtooperateistoimplementitonarealsystem.Forexperimentalalgorithmsandthoseunderdevelopment,thiscancausedifficultiesandresistanceamonguserswhodon'tcareaboutdevelopingOSesandareonlytryingtogettheirdailyworkdone.Eveninthiscase,themeasuredresultsmaynotbedefinitive,foratleasttwomajorreasons:(1)Systemworkloadsarenotstatic,butchangeovertimeasnewprogramsareinstalled,newusersareaddedtothesystem,newhardwarebecomesavailable,newworkprojectsgetstarted,andevensocietalchanges.(ForexampletheexplosionoftheInternethasdrasticallychangedtheamountofnetworktrafficthatasystemseesandtheimportanceofhandlingitwithrapidresponsetimes.)(2)Asmentionedabove,changingtheschedulingsystemmayhaveanimpactontheworkloadandthewaysinwhichusersusethesystem.(Thebookgivesanexampleofaprogrammerwhomodifiedhiscodetowriteanarbitrarycharactertothescreenatregularintervals,justsohisjobwouldbeclassifiedasinteractiveandplacedintoahigherpriorityqueue.)Mostmodernsystemsprovidesomecapabilityforthesystemadministratortoadjustschedulingparameters,eitherontheflyorastheresultofarebootorakernelrebuild.
5.8Summary
MaterialOmittedfromtheEighthEdition:
Was5.4.4SymmetricMultithreading(Omittedfrom8thedition)
AnalternativestrategytoSMPisSMT,SymmetricMultiThreading,inwhichmultiplevirtual(logical)CPUsareusedinsteadof(orincombinationwith)multiplephysicalCPUs.SMTmustbesupportedinhardware,aseachlogicalCPUhasitsownregistersandhandlesitsowninterrupts.(IntelreferstoSMTashyperthreadingtechnology.)TosomeextenttheOSdoesnotneedtoknowiftheprocessorsitismanagingarerealorvirtual.Ontheotherhand,someschedulingdecisionscanbeoptimizediftheschedulerknowsthemappingofvirtualprocessorstorealCPUs.(ConsidertheschedulingoftwoCPUintensiveprocessesonthearchitectureshownbelow.)
-
2/19/2015 OperatingSystems:CPUScheduling
http://www.cs.uic.edu/~jbell/CourseNotes/OperatingSystems/5_CPU_Scheduling.html 20/20
omitted