You are on page 1of 20

2/19/2015

OperatingSystems:CPUScheduling

CPUScheduling
References:
1. AbrahamSilberschatz,GregGagne,andPeterBaerGalvin,"OperatingSystemConcepts,EighthEdition",Chapter5

5.1BasicConcepts
AlmostallprogramshavesomealternatingcycleofCPUnumbercrunchingandwaitingforI/Oofsomekind.(Evena
simplefetchfrommemorytakesalongtimerelativetoCPUspeeds.)
Inasimplesystemrunningasingleprocess,thetimespentwaitingforI/Oiswasted,andthoseCPUcyclesarelost
forever.
AschedulingsystemallowsoneprocesstousetheCPUwhileanotheriswaitingforI/O,therebymakingfulluseof
otherwiselostCPUcycles.
Thechallengeistomaketheoverallsystemas"efficient"and"fair"aspossible,subjecttovaryingandoftendynamic
conditions,andwhere"efficient"and"fair"aresomewhatsubjectiveterms,oftensubjecttoshiftingprioritypolicies.
5.1.1CPUI/OBurstCycle
Almostallprocessesalternatebetweentwostatesinacontinuingcycle,asshowninFigure5.1below:
ACPUburstofperformingcalculations,and
AnI/Oburst,waitingfordatatransferinoroutofthesystem.

CPUburstsvaryfromprocesstoprocess,andfromprogramtoprogram,butanextensivestudyshows
http://www.cs.uic.edu/~jbell/CourseNotes/OperatingSystems/5_CPU_Scheduling.html

1/20

2/19/2015

OperatingSystems:CPUScheduling

frequencypatternssimilartothatshowninFigure5.2:

5.1.2CPUScheduler
WhenevertheCPUbecomesidle,itisthejoboftheCPUScheduler(a.k.a.theshorttermscheduler)to
selectanotherprocessfromthereadyqueuetorunnext.
Thestoragestructureforthereadyqueueandthealgorithmusedtoselectthenextprocessarenotnecessarily
aFIFOqueue.Thereareseveralalternativestochoosefrom,aswellasnumerousadjustableparametersfor
eachalgorithm,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,orselectadifferent
one.
Ifschedulingtakesplaceonlyunderconditions1and4,thesystemissaidtobenonpreemptive,or
cooperative.Undertheseconditions,onceaprocessstartsrunningitkeepsrunning,untiliteithervoluntarily
blocksoruntilitfinishes.Otherwisethesystemissaidtobepreemptive.
WindowsusednonpreemptiveschedulinguptoWindows3.x,andstartedusingpreemptiveschedulingwith
Win95.MacsusednonpreemptivepriortoOSX,andpreemptivesincethen.Notethatpreemptive
schedulingisonlypossibleonhardwarethatsupportsatimerinterrupt.
Notethatpreemptiveschedulingcancauseproblemswhentwoprocessessharedata,becauseoneprocess
maygetinterruptedinthemiddleofupdatingshareddatastructures.Chapter6willexaminethisissuein
greaterdetail.
Preemptioncanalsobeaproblemifthekernelisbusyimplementingasystemcall(e.g.updatingcritical
kerneldatastructures)whenthepreemptionoccurs.MostmodernUNIXesdealwiththisproblembymaking
http://www.cs.uic.edu/~jbell/CourseNotes/OperatingSystems/5_CPU_Scheduling.html

2/20

2/19/2015

OperatingSystems:CPUScheduling

theprocesswaituntilthesystemcallhaseithercompletedorblockedbeforeallowingthepreemption
Unfortunatelythissolutionisproblematicforrealtimesystems,asrealtimeresponsecannolongerbe
guaranteed.
Somecriticalsectionsofcodeprotectthemselvesfromconcurrencyproblemsbydisablinginterruptsbefore
enteringthecriticalsectionandreenablinginterruptsonexitingthesection.Needlesstosay,thisshouldonly
bedoneinraresituations,andonlyonveryshortpiecesofcodethatwillfinishquickly,(usuallyjustafew
machineinstructions.)
5.1.4Dispatcher
ThedispatcheristhemodulethatgivescontroloftheCPUtotheprocessselectedbythescheduler.This
functioninvolves:
Switchingcontext.
Switchingtousermode.
Jumpingtotheproperlocationinthenewlyloadedprogram.
Thedispatcherneedstobeasfastaspossible,asitisrunoneverycontextswitch.Thetimeconsumedbythe
dispatcherisknownasdispatchlatency.

5.2SchedulingCriteria
Thereareseveraldifferentcriteriatoconsiderwhentryingtoselectthe"best"schedulingalgorithmforaparticular
situationandenvironment,including:
CPUutilizationIdeallytheCPUwouldbebusy100%ofthetime,soastowaste0CPUcycles.Onarealsystem
CPUusageshouldrangefrom40%(lightlyloaded)to90%(heavilyloaded.)
ThroughputNumberofprocessescompletedperunittime.Mayrangefrom10/secondto1/hourdependingon
thespecificprocesses.
TurnaroundtimeTimerequiredforaparticularprocesstocomplete,fromsubmissiontimetocompletion.(Wall
clocktime.)
WaitingtimeHowmuchtimeprocessesspendinthereadyqueuewaitingtheirturntogetontheCPU.
(LoadaverageTheaveragenumberofprocessessittinginthereadyqueuewaitingtheirturntogetintothe
CPU.Reportedin1minute,5minute,and15minuteaveragesby"uptime"and"who".)
ResponsetimeThetimetakeninaninteractiveprogramfromtheissuanceofacommandtothecommenceofa
responsetothatcommand.
Ingeneralonewantstooptimizetheaveragevalueofacriteria(MaximizeCPUutilizationandthroughput,andminimize
alltheothers.)Howeversometimesonewantstodosomethingdifferent,suchastominimizethemaximumresponse
time.
Sometimesitismostdesirabletominimizethevarianceofacriteriathantheactualvalue.I.e.usersaremoreacceptingof
aconsistentpredictablesystemthananinconsistentone,evenifitisalittlebitslower.

5.3SchedulingAlgorithms
Thefollowingsubsectionswillexplainseveralcommonschedulingstrategies,lookingatonlyasingleCPUburst
eachforasmallnumberofprocesses.Obviouslyrealsystemshavetodealwithalotmoresimultaneousprocesses
executingtheirCPUI/Oburstcycles.
5.3.1FirstComeFirstServeScheduling,FCFS
FCFSisverysimpleJustaFIFOqueue,likecustomerswaitinginlineatthebankorthepostofficeorata
copyingmachine.
Unfortunately,however,FCFScanyieldsomeverylongaveragewaittimes,particularlyifthefirstprocessto
gettheretakesalongtime.Forexample,considerthefollowingthreeprocesses:
Process

BurstTime

P1

24

P2

P3

InthefirstGanttchartbelow,processP1arrivesfirst.Theaveragewaitingtimeforthethreeprocessesis(0
+24+27)/3=17.0ms.
InthesecondGanttchartbelow,thesamethreeprocesseshaveanaveragewaittimeof(0+3+6)/3=3.0
ms.Thetotalruntimeforthethreeburstsisthesame,butinthesecondcasetwoofthethreefinishmuch
quicker,andtheotherprocessisonlydelayedbyashortamount.

http://www.cs.uic.edu/~jbell/CourseNotes/OperatingSystems/5_CPU_Scheduling.html

3/20

2/19/2015

OperatingSystems:CPUScheduling

FCFScanalsoblockthesysteminabusydynamicsysteminanotherway,knownastheconvoyeffect.When
oneCPUintensiveprocessblockstheCPU,anumberofI/Ointensiveprocessescangetbackedupbehindit,
leavingtheI/Odevicesidle.WhentheCPUhogfinallyrelinquishestheCPU,thentheI/Oprocessespass
throughtheCPUquickly,leavingtheCPUidlewhileeveryonequeuesupforI/O,andthenthecyclerepeats
itselfwhentheCPUintensiveprocessgetsbacktothereadyqueue.
5.3.2ShortestJobFirstScheduling,SJF
TheideabehindtheSJFalgorithmistopickthequickestfastestlittlejobthatneedstobedone,getitoutof
thewayfirst,andthenpickthenextsmallestfastestjobtodonext.
(TechnicallythisalgorithmpicksaprocessbasedonthenextshortestCPUburst,nottheoverallprocess
time.)
Forexample,theGanttchartbelowisbaseduponthefollowingCPUbursttimes,(andtheassumptionthat
alljobsarriveatthesametime.)
Process

BurstTime

P1

P2

P3

P4

Inthecaseabovetheaveragewaittimeis(0+3+9+16)/4=7.0ms,(asopposedto10.25msforFCFS
forthesameprocesses.)
SJFcanbeproventobethefastestschedulingalgorithm,butitsuffersfromoneimportantproblem:Howdo
youknowhowlongthenextCPUburstisgoingtobe?
Forlongtermbatchjobsthiscanbedonebaseduponthelimitsthatuserssetfortheirjobswhenthey
submitthem,whichencouragesthemtosetlowlimits,butriskstheirhavingtoresubmitthejobifthey
setthelimittoolow.HoweverthatdoesnotworkforshorttermCPUschedulingonaninteractive
system.
Anotheroptionwouldbetostatisticallymeasuretheruntimecharacteristicsofjobs,particularlyifthe
sametasksarerunrepeatedlyandpredictably.Butonceagainthatreallyisn'taviableoptionforshort
termCPUschedulingintherealworld.
Amorepracticalapproachistopredictthelengthofthenextburst,basedonsomehistorical
measurementofrecentbursttimesforthisprocess.Onesimple,fast,andrelativelyaccuratemethodis
theexponentialaverage,whichcanbedefinedasfollows.(Thebookusestauandtfortheirvariables,
butthosearehardtodistinguishfromoneanotheranddon'tworkwellinHTML.)
estimate[i+1]=alpha*burst[i]+(1.0alpha)*estimate[i]
Inthisschemethepreviousestimatecontainsthehistoryofallprevioustimes,andalphaservesasa
weightingfactorfortherelativeimportanceofrecentdataversuspasthistory.Ifalphais1.0,thenpast
historyisignored,andweassumethenextburstwillbethesamelengthasthelastburst.Ifalphais0.0,
thenallmeasuredbursttimesareignored,andwejustassumeaconstantbursttime.Mostcommonly
alphaissetat0.5,asillustratedinFigure5.3:
http://www.cs.uic.edu/~jbell/CourseNotes/OperatingSystems/5_CPU_Scheduling.html

4/20

2/19/2015

OperatingSystems:CPUScheduling

SJFcanbeeitherpreemptiveornonpreemptive.Preemptionoccurswhenanewprocessarrivesintheready
queuethathasapredictedbursttimeshorterthanthetimeremainingintheprocesswhoseburstiscurrently
ontheCPU.PreemptiveSJFissometimesreferredtoasshortestremainingtimefirstscheduling.
Forexample,thefollowingGanttchartisbaseduponthefollowingdata:
Process

ArrivalTime

BurstTime

P1

P2

P3

p4

Theaveragewaittimeinthiscaseis((53)+(101)+(172))/4=26/4=6.5ms.(Asopposedto
7.75msfornonpreemptiveSJFor8.75forFCFS.)
5.3.3PriorityScheduling
PriorityschedulingisamoregeneralcaseofSJF,inwhicheachjobisassignedapriorityandthejobwiththe
highestprioritygetsscheduledfirst.(SJFusestheinverseofthenextexpectedbursttimeasitspriorityThe
smallertheexpectedburst,thehigherthepriority.)
Notethatinpractice,prioritiesareimplementedusingintegerswithinafixedrange,butthereisnoagreed
uponconventionastowhether"high"prioritiesuselargenumbersorsmallnumbers.Thisbookuseslow
numberforhighpriorities,with0beingthehighestpossiblepriority.
Forexample,thefollowingGanttchartisbasedupontheseprocessbursttimesandpriorities,andyieldsan
http://www.cs.uic.edu/~jbell/CourseNotes/OperatingSystems/5_CPU_Scheduling.html

5/20

2/19/2015

OperatingSystems:CPUScheduling

averagewaitingtimeof8.2ms:
Process

BurstTime

Priority

P1

10

P2

P3

P4

P5

Prioritiescanbeassignedeitherinternallyorexternally.InternalprioritiesareassignedbytheOSusing
criteriasuchasaveragebursttime,ratioofCPUtoI/Oactivity,systemresourceuse,andotherfactors
availabletothekernel.Externalprioritiesareassignedbyusers,basedontheimportanceofthejob,feespaid,
politics,etc.
Priorityschedulingcanbeeitherpreemptiveornonpreemptive.
Priorityschedulingcansufferfromamajorproblemknownasindefiniteblocking,orstarvation,inwhicha
lowprioritytaskcanwaitforeverbecausetherearealwayssomeotherjobsaroundthathavehigherpriority.
Ifthisproblemisallowedtooccur,thenprocesseswilleitherruneventuallywhenthesystemload
lightens(atsay2:00a.m.),orwilleventuallygetlostwhenthesystemisshutdownorcrashes.(There
arerumorsofjobsthathavebeenstuckforyears.)
Onecommonsolutiontothisproblemisaging,inwhichprioritiesofjobsincreasethelongertheywait.
Underthisschemealowpriorityjobwilleventuallygetitspriorityraisedhighenoughthatitgetsrun.
5.3.4RoundRobinScheduling
RoundrobinschedulingissimilartoFCFSscheduling,exceptthatCPUburstsareassignedwithlimitscalled
timequantum.
WhenaprocessisgiventheCPU,atimerissetforwhatevervaluehasbeensetforatimequantum.
Iftheprocessfinishesitsburstbeforethetimequantumtimerexpires,thenitisswappedoutofthe
CPUjustlikethenormalFCFSalgorithm.
Ifthetimergoesofffirst,thentheprocessisswappedoutoftheCPUandmovedtothebackendofthe
readyqueue.
Thereadyqueueismaintainedasacircularqueue,sowhenallprocesseshavehadaturn,thenthescheduler
givesthefirstprocessanotherturn,andsoon.
RRschedulingcangivetheeffectofallprocessorssharingtheCPUequally,althoughtheaveragewaittime
canbelongerthanwithotherschedulingalgorithms.Inthefollowingexampletheaveragewaittimeis5.66
ms.
Process

BurstTime

P1

24

P2

P3

TheperformanceofRRissensitivetothetimequantumselected.Ifthequantumislargeenough,thenRR
reducestotheFCFSalgorithmIfitisverysmall,theneachprocessgets1/nthoftheprocessortimeandshare
theCPUequally.
BUT,arealsysteminvokesoverheadforeverycontextswitch,andthesmallerthetimequantumthemore
contextswitchesthereare.(SeeFigure5.4below.)Mostmodernsystemsusetimequantumbetween10and
http://www.cs.uic.edu/~jbell/CourseNotes/OperatingSystems/5_CPU_Scheduling.html

6/20

2/19/2015

OperatingSystems:CPUScheduling

100milliseconds,andcontextswitchtimesontheorderof10microseconds,sotheoverheadissmallrelative
tothetimequantum.

Turnaroundtimealsovarieswithquantumtime,inanonapparentmanner.Consider,forexamplethe
processesshowninFigure5.5:

Ingeneral,turnaroundtimeisminimizedifmostprocessesfinishtheirnextcpuburstwithinonetime
quantum.Forexample,withthreeprocessesof10msburstseach,theaverageturnaroundtimefor1ms
quantumis29,andfor10msquantumitreducesto20.However,ifitismadetoolarge,thenRRjust
degeneratestoFCFS.Aruleofthumbisthat80%ofCPUburstsshouldbesmallerthanthetimequantum.
5.3.5MultilevelQueueScheduling
http://www.cs.uic.edu/~jbell/CourseNotes/OperatingSystems/5_CPU_Scheduling.html

7/20

2/19/2015

OperatingSystems:CPUScheduling

Whenprocessescanbereadilycategorized,thenmultipleseparatequeuescanbeestablished,each
implementingwhateverschedulingalgorithmismostappropriateforthattypeofjob,and/orwithdifferent
parametricadjustments.
Schedulingmustalsobedonebetweenqueues,thatisschedulingonequeuetogettimerelativetoother
queues.Twocommonoptionsarestrictpriority(nojobinalowerpriorityqueuerunsuntilallhigherpriority
queuesareempty)androundrobin(eachqueuegetsatimesliceinturn,possiblyofdifferentsizes.)
NotethatunderthisalgorithmjobscannotswitchfromqueuetoqueueOncetheyareassignedaqueue,that
istheirqueueuntiltheyfinish.

5.3.6MultilevelFeedbackQueueScheduling
Multilevelfeedbackqueueschedulingissimilartotheordinarymultilevelqueueschedulingdescribedabove,
exceptjobsmaybemovedfromonequeuetoanotherforavarietyofreasons:
IfthecharacteristicsofajobchangebetweenCPUintensiveandI/Ointensive,thenitmaybe
appropriatetoswitchajobfromonequeuetoanother.
Agingcanalsobeincorporated,sothatajobthathaswaitedforalongtimecangetbumpedupintoa
higherpriorityqueueforawhile.
Multilevelfeedbackqueueschedulingisthemostflexible,becauseitcanbetunedforanysituation.Butitis
alsothemostcomplextoimplementbecauseofalltheadjustableparameters.Someoftheparameterswhich
defineoneofthesesystemsinclude:
Thenumberofqueues.
Theschedulingalgorithmforeachqueue.
Themethodsusedtoupgradeordemoteprocessesfromonequeuetoanother.(Whichmaybe
different.)
Themethodusedtodeterminewhichqueueaprocessentersinitially.

http://www.cs.uic.edu/~jbell/CourseNotes/OperatingSystems/5_CPU_Scheduling.html

8/20

2/19/2015

OperatingSystems:CPUScheduling

5.4ThreadScheduling
Theprocessschedulerschedulesonlythekernelthreads.
UserthreadsaremappedtokernelthreadsbythethreadlibraryTheOS(andinparticularthescheduler)isunawareof
them.
5.4.1ContentionScope
ContentionscopereferstothescopeinwhichthreadscompetefortheuseofphysicalCPUs.
Onsystemsimplementingmanytooneandmanytomanythreads,ProcessContentionScope,PCS,occurs,
becausecompetitionoccursbetweenthreadsthatarepartofthesameprocess.(Thisisthemanagement/
schedulingofmultipleuserthreadsonasinglekernelthread,andismanagedbythethreadlibrary.)
SystemContentionScope,SCS,involvesthesystemschedulerschedulingkernelthreadstorunononeor
moreCPUs.Systemsimplementingonetoonethreads(XP,Solaris9,Linux),useonlySCS.
PCSschedulingistypicallydonewithpriority,wheretheprogrammercansetand/orchangethepriorityof
threadscreatedbyhisorherprograms.Eventimeslicingisnotguaranteedamongthreadsofequalpriority.
5.4.2PthreadScheduling
ThePthreadlibraryprovidesforspecifyingscopecontention:
PTHREAD_SCOPE_PROCESSschedulesthreadsusingPCS,byschedulinguserthreadsonto
availableLWPsusingthemanytomanymodel.
PTHREAD_SCOPE_SYSTEMschedulesthreadsusingSCS,bybindinguserthreadstoparticular
LWPs,effectivelyimplementingaonetoonemodel.
getscopeandsetscopemethodsprovidefordeterminingandsettingthescopecontentionrespectively:

http://www.cs.uic.edu/~jbell/CourseNotes/OperatingSystems/5_CPU_Scheduling.html

9/20

2/19/2015

OperatingSystems:CPUScheduling

http://www.cs.uic.edu/~jbell/CourseNotes/OperatingSystems/5_CPU_Scheduling.html

10/20

2/19/2015

OperatingSystems:CPUScheduling

Figure5.8

5.5MultipleProcessorScheduling
Whenmultipleprocessorsareavailable,thentheschedulinggetsmorecomplicated,becausenowthereismorethanone
CPUwhichmustbekeptbusyandineffectiveuseatalltimes.
Loadsharingrevolvesaroundbalancingtheloadbetweenmultipleprocessors.
Multiprocessorsystemsmaybeheterogeneous,(differentkindsofCPUs),orhomogenous,(allthesamekindofCPU
).Eveninthelattercasetheremaybespecialschedulingconstraints,suchasdeviceswhichareconnectedviaaprivate
bustoonlyoneoftheCPUs.Thisbookwillrestrictitsdiscussiontohomogenoussystems.
5.5.1ApproachestoMultipleProcessorScheduling
Oneapproachtomultiprocessorschedulingisasymmetricmultiprocessing,inwhichoneprocessoristhe
master,controllingallactivitiesandrunningallkernelcode,whiletheotherrunsonlyusercode.This
approachisrelativelysimple,asthereisnoneedtosharecriticalsystemdata.
Anotherapproachissymmetricmultiprocessing,SMP,whereeachprocessorschedulesitsownjobs,either
fromacommonreadyqueueorfromseparatereadyqueuesforeachprocessor.
VirtuallyallmodernOSessupportSMP,includingXP,Win2000,Solaris,Linux,andMacOSX.
5.5.2ProcessorAffinity
Processorscontaincachememory,whichspeedsuprepeatedaccessestothesamememorylocations.
Ifaprocessweretoswitchfromoneprocessortoanothereachtimeitgotatimeslice,thedatainthecache(
forthatprocess)wouldhavetobeinvalidatedandreloadedfrommainmemory,therebyobviatingthe
benefitofthecache.
ThereforeSMPsystemsattempttokeepprocessesonthesameprocessor,viaprocessoraffinity.Softaffinity
occurswhenthesystemattemptstokeepprocessesonthesameprocessorbutmakesnoguarantees.Linux
andsomeotherOSessupporthardaffinity,inwhichaprocessspecifiesthatitisnottobemovedbetween
processors.
Mainmemoryarchitecturecanalsoaffectprocessaffinity,ifparticularCPUshavefasteraccesstomemory
onthesamechiporboardthantoothermemoryloadedelsewhere.(NonUniformMemoryAccess,
NUMA.)Asshownbelow,ifaprocesshasanaffinityforaparticularCPU,thenitshouldpreferentiallybe
assignedmemorystoragein"local"fastaccessareas.

5.5.3LoadBalancing
Obviouslyanimportantgoalinamultiprocessorsystemistobalancetheloadbetweenprocessors,sothatone
processorwon'tbesittingidlewhileanotherisoverloaded.
Systemsusingacommonreadyqueuearenaturallyselfbalancing,anddonotneedanyspecialhandling.
Mostsystems,however,maintainseparatereadyqueuesforeachprocessor.
http://www.cs.uic.edu/~jbell/CourseNotes/OperatingSystems/5_CPU_Scheduling.html

11/20

2/19/2015

OperatingSystems:CPUScheduling

Balancingcanbeachievedthrougheitherpushmigrationorpullmigration:
Pushmigrationinvolvesaseparateprocessthatrunsperiodically,(e.g.every200milliseconds),and
movesprocessesfromheavilyloadedprocessorsontolessloadedones.
Pullmigrationinvolvesidleprocessorstakingprocessesfromthereadyqueuesofotherprocessors.
Pushandpullmigrationarenotmutuallyexclusive.
Notethatmovingprocessesfromprocessortoprocessortoachieveloadbalancingworksagainsttheprinciple
ofprocessoraffinity,andifnotcarefullymanaged,thesavingsgainedbybalancingthesystemcanbelostin
rebuildingcaches.Oneoptionistoonlyallowmigrationwhenimbalancesurpassesagiventhreshold.
5.5.4MulticoreProcessors
TraditionalSMPrequiredmultipleCPUchipstorunmultiplekernelthreadsconcurrently.
RecenttrendsaretoputmultipleCPUs(cores)ontoasinglechip,whichappeartothesystemasmultiple
processors.
Computecyclescanbeblockedbythetimeneededtoaccessmemory,whenevertheneededdataisnot
alreadypresentinthecache.(Cachemisses.)InFigure5.10,asmuchashalfoftheCPUcyclesarelostto
memorystall.

Byassigningmultiplekernelthreadstoasingleprocessor,memorystallcanbeavoided(orreduced)by
runningonethreadontheprocessorwhiletheotherthreadwaitsformemory.

Adualthreadeddualcoresystemhasfourlogicalprocessorsavailabletotheoperatingsystem.The
UltraSPARCT1CPUhas8coresperchipand4hardwarethreadspercore,foratotalof32logical
processorsperchip.
Therearetwowaystomultithreadaprocessor:
1. Coarsegrainedmultithreadingswitchesbetweenthreadsonlywhenonethreadblocks,sayona
memoryread.Contextswitchingissimilartoprocessswitching,withconsiderableoverhead.
2. Finegrainedmultithreadingoccursonsmallerregularintervals,sayontheboundaryofinstruction
cycles.Howeverthearchitectureisdesignedtosupportthreadswitching,sotheoverheadisrelatively
minor.
Notethatforamultithreadedmulticoresystem,therearetwolevelsofscheduling,atthekernellevel:
TheOSscheduleswhichkernelthread(s)toassigntowhichlogicalprocessors,andwhentomake
contextswitchesusingalgorithmsasdescribedabove.
Onalowerlevel,thehardwarescheduleslogicalprocessorsoneachphysicalcoreusingsomeother
algorithm.
http://www.cs.uic.edu/~jbell/CourseNotes/OperatingSystems/5_CPU_Scheduling.html

12/20

2/19/2015

OperatingSystems:CPUScheduling

TheUltraSPARCT1usesasimpleroundrobinmethodtoschedulethe4logicalprocessors(
kernelthreads)oneachphysicalcore.
TheIntelItaniumisadualcorechipwhichusesa7levelpriorityscheme(urgency)to
determinewhichthreadtoschedulewhenoneof5differenteventsoccurs.
5.5.5VirtualizationandScheduling
Virtualizationaddsanotherlayerofcomplexityandscheduling.
Typicallythereisonehostoperatingsystemoperatingon"real"processor(s)andanumberofguestoperating
systemsoperatingonvirtualprocessors.
TheHostOScreatessomenumberofvirtualprocessorsandpresentsthemtotheguestOSesasiftheywere
realprocessors.
TheguestOSesdon'trealizetheirprocessorsarevirtual,andmakeschedulingdecisionsontheassumptionof
realprocessors.
Asaresult,interactiveandespeciallyrealtimeperformancecanbeseverelycompromisedonguestsystems.
Thetimeofdayclockwillalsofrequentlybeoff.

5.6OperatingSystemExamples
5.6.1Example:SolarisScheduling
Prioritybasedkernelthreadscheduling.
Fourclasses(realtime,system,interactive,andtimesharing),andmultiplequeues/algorithmswithineach
class.
Defaultistimesharing.
Processprioritiesandtimeslicesareadjusteddynamicallyinamultilevelfeedbackpriorityqueue
system.
TimeslicesareinverselyproportionaltopriorityHigherpriorityjobsgetsmallertimeslices.
InteractivejobshavehigherprioritythanCPUBoundones.
Seethetablebelowforsomeofthe60prioritylevelsandhowtheyshift."Timequantumexpired"and
"returnfromsleep"indicatethenewprioritywhenthoseeventsoccur.(Largernumbersareahigher,
i.e.betterpriority.)

http://www.cs.uic.edu/~jbell/CourseNotes/OperatingSystems/5_CPU_Scheduling.html

13/20

2/19/2015

OperatingSystems:CPUScheduling

Figure5.12
Solaris9introducedtwonewschedulingclasses:Fixedpriorityandfairshare.
Fixedpriorityissimilartotimesharing,butnotadjusteddynamically.
FairshareusessharesofCPUtimeratherthanprioritiestoschedulejobs.Acertainshareofthe
availableCPUtimeisallocatedtoaproject,whichisasetofprocesses.
Systemclassisreservedforkerneluse.(UserprogramsrunninginkernelmodeareNOTconsideredinthe
systemschedulingclass.)

http://www.cs.uic.edu/~jbell/CourseNotes/OperatingSystems/5_CPU_Scheduling.html

14/20

2/19/2015

OperatingSystems:CPUScheduling

5.6.2Example:WindowsXPScheduling
WindowsXPusesaprioritybasedpreemptiveschedulingalgorithm.
Thedispatcherusesa32levelpriorityschemetodeterminetheorderofthreadexecution,dividedintotwo
classesvariableclassfrom1to15andrealtimeclassfrom16to31,(plusathreadatpriority0managing
memory.)
Thereisalsoaspecialidlethreadthatisscheduledwhennootherthreadsareready.
WinXPidentifies7priorityclasses(rowsonthetablebelow),and6relativeprioritieswithineachclass(
columns.)
Processesarealsoeachgivenabaseprioritywithintheirpriorityclass.Whenvariableclassprocesses
consumetheirentiretimequanta,thentheirprioritygetslowered,butnotbelowtheirbasepriority.
Processesintheforeground(activewindow)havetheirschedulingquantamultipliedby3,togivebetter
responsetointeractiveprocessesintheforeground.

http://www.cs.uic.edu/~jbell/CourseNotes/OperatingSystems/5_CPU_Scheduling.html

15/20

2/19/2015

OperatingSystems:CPUScheduling

Figure5.14
5.6.3Example:LinuxScheduling
ModernLinuxschedulingprovidesimprovedsupportforSMPsystems,andaschedulingalgorithmthatruns
inO(1)timeasthenumberofprocessesincreases.
TheLinuxschedulerisapreemptiveprioritybasedalgorithmwithtwopriorityrangesRealtimefrom0to
99andanicerangefrom100to140.
UnlikeSolarisorXP,Linuxassignslongertimequantumstohigherprioritytasks.

Figure5.15
Arunnabletaskisconsideredeligibleforexecutionaslongasithasnotconsumedallthetimeavailableinit's
timeslice.Thosetasksarestoredinanactivearray,indexedaccordingtopriority.
Whenaprocessconsumesitstimeslice,itismovedtoanexpiredarray.Thetasksprioritymaybere
assignedaspartofthetransferal.
Whentheactivearraybecomesempty,thetwoarraysareswapped.
Thesearraysarestoredinrunqueuestructures.Onmultiprocessormachines,eachprocessorhasitsown
schedulerwithitsownrunqueue.

http://www.cs.uic.edu/~jbell/CourseNotes/OperatingSystems/5_CPU_Scheduling.html

16/20

2/19/2015

OperatingSystems:CPUScheduling

Figure5.16

5.7AlgorithmEvaluation
Thefirststepindeterminingwhichalgorithm(andwhatparametersettingswithinthatalgorithm)isoptimalfora
particularoperatingenvironmentistodeterminewhatcriteriaaretobeused,whatgoalsaretobetargeted,andwhat
constraintsifanymustbeapplied.Forexample,onemightwantto"maximizeCPUutilization,subjecttoamaximum
responsetimeof1second".
Oncecriteriahavebeenestablished,thendifferentalgorithmscanbeanalyzedanda"bestchoice"determined.The
followingsectionsoutlinesomedifferentmethodsfordeterminingthe"bestchoice".
5.7.1DeterministicModeling
Ifaspecificworkloadisknown,thentheexactvaluesformajorcriteriacanbefairlyeasilycalculated,andthe
"best"determined.Forexample,considerthefollowingworkload(withallprocessesarrivingattime0),and
theresultingschedulesdeterminedbythreedifferentalgorithms:
Process

BurstTime

P1

10

P2

29

P3

P4

P5

12

http://www.cs.uic.edu/~jbell/CourseNotes/OperatingSystems/5_CPU_Scheduling.html

17/20

2/19/2015

OperatingSystems:CPUScheduling

TheaveragewaitingtimesforFCFS,SJF,andRRare28ms,13ms,and23msrespectively.
Deterministicmodelingisfastandeasy,butitrequiresspecificknowninput,andtheresultsonlyapplyfor
thatparticularsetofinput.Howeverbyexaminingmultiplesimilarcases,certaintrendscanbeobserved.(
Likethefactthatforprocessesarrivingatthesametime,SJFwillalwaysyieldtheshortestaveragewaittime.
)
5.7.2QueuingModels
Specificprocessdataisoftennotavailable,particularlyforfuturetimes.Howeverastudyofhistorical
performancecanoftenproducestatisticaldescriptionsofcertainimportantparameters,suchastherateat
whichnewprocessesarrive,theratioofCPUburststoI/Otimes,thedistributionofCPUbursttimesandI/O
bursttimes,etc.
Armedwiththoseprobabilitydistributionsandsomemathematicalformulas,itispossibletocalculatecertain
performancecharacteristicsofindividualwaitingqueues.Forexample,Little'sFormulasaysthatforan
averagequeuelengthofN,withanaveragewaitingtimeinthequeueofW,andanaveragearrivalofnew
jobsinthequeueofLambda,thenthesethreetermscanberelatedby:
N=Lambda*W
Queuingmodelstreatthecomputerasanetworkofinterconnectedqueues,eachofwhichisdescribedbyits
probabilitydistributionstatisticsandformulassuchasLittle'sformula.Unfortunatelyrealsystemsand
modernschedulingalgorithmsaresocomplexastomakethemathematicsintractableinmanycaseswithreal
systems.
5.7.3Simulations
Anotherapproachistoruncomputersimulationsofthedifferentproposedalgorithms(andadjustment
parameters)underdifferentloadconditions,andtoanalyzetheresultstodeterminethe"best"choiceof
operationforaparticularloadpattern.
Operatingconditionsforsimulationsareoftenrandomlygeneratedusingdistributionfunctionssimilarto
thosedescribedabove.
Abetteralternativewhenpossibleistogeneratetracetapes,bymonitoringandloggingtheperformanceofa
realsystemundertypicalexpectedworkloads.Thesearebetterbecausetheyprovideamoreaccuratepicture
ofsystemloads,andalsobecausetheyallowmultiplesimulationstoberunwiththeidenticalprocessload,
andnotjuststatisticallyequivalentloads.Acompromiseistorandomlydeterminesystemloadsandthensave
theresultsintoafile,sothatallsimulationscanberunagainstidenticalrandomlydeterminedsystemloads.
Althoughtracetapesprovidemoreaccurateinputinformation,theycanbedifficultandexpensivetocollect
andstore,andtheiruseincreasesthecomplexityofthesimulationssignificantly.Thereisalsosomequestion
astowhetherthefutureperformanceofthenewsystemwillreallymatchthepastperformanceoftheold
system.(Ifthesystemrunsfaster,usersmaytakefewercoffeebreaks,andsubmitmoreprocessesperhour
thanundertheoldsystem.Converselyiftheturnaroundtimeforjobsislonger,intelligentusersmaythink
morecarefullyaboutthejobstheysubmitratherthanrandomlysubmittingjobsandhopingthatoneofthem
worksout.)

http://www.cs.uic.edu/~jbell/CourseNotes/OperatingSystems/5_CPU_Scheduling.html

18/20

2/19/2015

OperatingSystems:CPUScheduling

Figure5.17
5.7.4Implementation
Theonlyrealwaytodeterminehowaproposedschedulingalgorithmisgoingtooperateistoimplementiton
arealsystem.
Forexperimentalalgorithmsandthoseunderdevelopment,thiscancausedifficultiesandresistanceamong
userswhodon'tcareaboutdevelopingOSesandareonlytryingtogettheirdailyworkdone.
Eveninthiscase,themeasuredresultsmaynotbedefinitive,foratleasttwomajorreasons:(1)Systemwork
loadsarenotstatic,butchangeovertimeasnewprogramsareinstalled,newusersareaddedtothesystem,
newhardwarebecomesavailable,newworkprojectsgetstarted,andevensocietalchanges.(Forexamplethe
explosionoftheInternethasdrasticallychangedtheamountofnetworktrafficthatasystemseesandthe
importanceofhandlingitwithrapidresponsetimes.)(2)Asmentionedabove,changingthescheduling
systemmayhaveanimpactontheworkloadandthewaysinwhichusersusethesystem.(Thebookgivesan
exampleofaprogrammerwhomodifiedhiscodetowriteanarbitrarycharactertothescreenatregular
intervals,justsohisjobwouldbeclassifiedasinteractiveandplacedintoahigherpriorityqueue.)
Mostmodernsystemsprovidesomecapabilityforthesystemadministratortoadjustschedulingparameters,
eitherontheflyorastheresultofarebootorakernelrebuild.

5.8Summary
MaterialOmittedfromtheEighthEdition:
Was5.4.4SymmetricMultithreading(Omittedfrom8thedition)
AnalternativestrategytoSMPisSMT,SymmetricMultiThreading,inwhichmultiplevirtual(logical)CPUsareused
insteadof(orincombinationwith)multiplephysicalCPUs.
SMTmustbesupportedinhardware,aseachlogicalCPUhasitsownregistersandhandlesitsowninterrupts.(Intel
referstoSMTashyperthreadingtechnology.)
TosomeextenttheOSdoesnotneedtoknowiftheprocessorsitismanagingarerealorvirtual.Ontheotherhand,some
schedulingdecisionscanbeoptimizediftheschedulerknowsthemappingofvirtualprocessorstorealCPUs.(Consider
theschedulingoftwoCPUintensiveprocessesonthearchitectureshownbelow.)

http://www.cs.uic.edu/~jbell/CourseNotes/OperatingSystems/5_CPU_Scheduling.html

19/20

2/19/2015

OperatingSystems:CPUScheduling

omitted

http://www.cs.uic.edu/~jbell/CourseNotes/OperatingSystems/5_CPU_Scheduling.html

20/20

You might also like