2/3/16
1
CSE30341Opera,ngSystemPrinciples
Scheduling
CSE30341–Opera,ngSystemPrinciples 2
Objec,ves
• TointroduceCPUscheduling,whichisthebasisformul,-programmedandmul,-taskingsystems
• TodescribevariousCPU-schedulingalgorithms
• Todiscussevalua;oncriteriaforselec,ngaCPU-schedulingalgorithmforapar,cularsystem
• Toexaminetheschedulingalgorithmsofseveralopera,ngsystems
2/3/16
2
CSE30341–Opera,ngSystemPrinciples 3
WhySchedule?
Firefox
Word
Thunderbird
Spo,fy
CPU1
CPU2
Scheduler
Whoshouldrun?
Whereshouldtheprocess(thread)run?
I/O
WhatdoIwanttoop,mize?
ResponsiveMostWorkDone
ScaleWell
CSE30341–Opera,ngSystemPrinciples 4
Burs,ness
• Computa,onandI/Otendtobebursty– Readsomedata– Computeabunch– Writesomedata– Repeat
2/3/16
3
CSE30341–Opera,ngSystemPrinciples 5
“KeepCPUbusy”
Process1
Process2
CPUProcess1 Process2
CSE30341–Opera,ngSystemPrinciples 6
Scheduling
Scheduler
ReadyQueue
Jobsarereadyforcomputa,on“NeedCPU”
Shorttermscheduler
CPU
Runningprocess
WhendoIchoosethenextonetorun?
2/3/16
4
CSE30341–Opera,ngSystemPrinciples 7
SchedulingChoices
• Non-preemp,ve(voluntarily):– Processyields– Processgoesfromrunningtowai,ngstate– Processterminates
• Preemp,ve(forced,canhappenany,me):– OSforcesprocessfromrunningtoreadystate
CSE30341–Opera,ngSystemPrinciples 8
Dispatcher
• MechanismthatgivescontroloftheCPUtoselectedprocess;includes:– Contextswitch• Save/restorestack,registers,…
– Switchbacktousermode– ResumePCforprocess
DispatchLatency:Timeittakestostoponeprocessandswaptoanother
2/3/16
5
CSE30341–Opera,ngSystemPrinciples 9
SchedulingCriteria• CPUu;liza;on–keeptheCPUasbusyaspossible(“howbusyis
theCPU”)
• Throughput–#ofprocessesthatcompletetheirexecu,onper,meunit(“howmuchworkisgefngdone”)
• Turnaround;me–amountof,metoexecuteapar,cularprocess(“howlongdoesittaketoexecuteaprocess”)
• Wai;ng;me–amountof,meaprocesshasbeenwai,ngintheREADYQUEUE(RUNQUEUE)
• Response;me–amountof,meittakesfromwhenarequestwassubmijedun,lthefirstresponseisproduced
CSE30341–Opera,ngSystemPrinciples 10
SchedulingAlgorithmOp,miza,onCriteria
• MaxCPUu,liza,on• Maxthroughput• Minturnaround,me• Minwai,ng,me• Minresponse,me
Thefancierwegetop,miza,on-wise,themorecomplexitywehavein
thesystem
2/3/16
6
CSE30341–Opera,ngSystemPrinciples 11
First-Come,First-Served(FCFS)Scheduling
ProcessBurstTime P1 24 P2 3 P3 3• SupposethattheprocessesarriveintheorderP1,P2,P3andreadyforexecu,onat,me0
Wai;ngTime
P1=0;P2=24;P3=27
AverageWai;ngTime(0+24+27)/3=17
Computa,onTime
P1 P2 P3
0 24 27 30
CSE30341–Opera,ngSystemPrinciples 12
DifferentOrder
• SupposethattheprocessesarriveintheorderP2,P3,P1(readyforexecu,onat,me0)
Wai;ngTimeP1=6;P2=0;P3=3
AverageWai;ngTime(6+0+3)/3=3
ConvoyEffect
P1P3P2
0 3 6 30
2/3/16
7
CSE30341–Opera,ngSystemPrinciples 13
FCFS(FIFO)
• Verysimple(addprocessestoendofrunqueue,takeprocessesfrombeginningofqueue)
• Note:processesreturningfromwaitqueuesalwaysgotothebackoftherunqueue!
• NON-PREEMPTIVE(,me-sharing/interac,ve?)
CSE30341–Opera,ngSystemPrinciples 14
Shortest-Job-First(SJF)Scheduling
• AssociatewitheachprocessthelengthofitsnextCPUburst– Usetheselengthstoscheduletheprocesswiththeshortest,me
• SJFisop;mal–givesminimumaveragewai;ng;meforagivensetofprocesses– ThedifficultyisknowingthelengthofthenextCPUrequest
2/3/16
8
CSE30341–Opera,ngSystemPrinciples 15
Revisi,ngwithSJF
ProcessBurstTime P1 24 P2 3 P3 3• SupposethattheprocessesarriveintheorderP1,P2,P3(allinqueueat,me0)
Wai;ngTimeP1=6;P2=0;P3=3
AverageWai;ngTime(6+0+3)/3=3
Computa,onTime
P1P3P2
0 3 6 30
CSE30341–Opera,ngSystemPrinciples 16
Shortest-JobFirst
Process BurstTime P1 6P2 8P3 7P4 3
P3P1P4
0 3 9 24
P2
16
Wai;ngTimeP1=3;P2=16;P3=9
AverageWai;ngTime(3+16+9+0)/4=7
2/3/16
9
CSE30341–Opera,ngSystemPrinciples 17
DeterminingLengthofNextCPUBurst• Canonlyes,matethelength–shouldbesimilartothepreviousone
– ThenpickprocesswithshortestpredictednextCPUburst
• CanbedonebyusingthelengthofpreviousCPUbursts,usingexponen,alaveraging
• Commonly,α setto½
:Define 4.10 , 3.
burst CPU next the for value predicted 2.burst CPU of length actual 1.
≤≤
=
=
+
αα
τ 1n
thn nt
( ) .1 1 nnn t ταατ −+==
AlsocalledEWMA
Exponen,alWeightedMovingAverage
CSE30341–Opera,ngSystemPrinciples 18
Exponen,alAveraging
• α = 0? α = 1?
τn+1 = α*tn + (1-α) α*tn-1 + (1-α)2*αtn-2… + (1-α)j αtn-j + …… + (1-α)n+1τ0
2/3/16
10
CSE30341–Opera,ngSystemPrinciples 19
Predic,onoftheLengthoftheNextCPUBurst
6 4 6 4 13 13 13 …810 6 6 5 9 11 12 …
CPU burst (ti)
"guess" (τi)
ti
τi
2
time
4
6
8
10
12
CSE30341–Opera,ngSystemPrinciples 20
ShortestJobFirst(SJF)• Non-preemp;ve!• Preemp,veversioncalledshortest-remaining-;me-firstProcess-Arrival/BurstTime P1 0/8P2 1/4P3 2/9P4 3/5
P4P2P1
0 10 26
P1
171 5
P3
2/3/16
11
CSE30341–Opera,ngSystemPrinciples 21
PriorityScheduling• Aprioritynumber(integer)isassociatedwitheachprocess• TheCPUisallocatedtotheprocesswiththehighestpriority
(smallestinteger≡highestpriority)– Preemp,ve– Non-preemp,ve
• SJFispriorityschedulingwherepriorityistheinverseofpredictednextCPUburst,me
• Fixedpriori;es:donotchangeover,me• Dynamicpriori;es:canchangeover,me• Problem:Starva;on–lowpriorityprocessesmayneverexecute• Solu,on:Aging–as,meprogressesincreasethepriorityofthe
process
CSE30341–Opera,ngSystemPrinciples 22
ExampleofPriorityScheduling
ProcessAarriBurstTimeTPriority P1 10 3 P2 1 1 P3 24 P4 15 P5 52• Lownumber=highpriority• Dynamicversussta,c/fixedpriority
2/3/16
12
CSE30341–Opera,ngSystemPrinciples 23
RoundRobin(RR)• Switchbetweenprocessesata,meinterval– Timequantum,q– 10-100ms– Preemp;ve
• Whatdoesitmean?– Ntasks?– Maximumwait,me
• Performance– qlarge⇒FIFO– qsmall⇒qmustbelargewithrespecttocontextswitch,otherwiseoverheadistoohigh
CSE30341–Opera,ngSystemPrinciples 24
ExampleofRRwithTimeQuantum=4 Process BurstTime P1 24 P2 3 P3 3
• TheGanjchartis:
• Typically,higheraverageturnaroundthanSJF,butbejerresponse• qshouldbelargecomparedtocontextswitch,me• qusually10msto100ms,contextswitch<10usec
P1 P2 P3 P1 P1 P1 P1 P10 4 7 10 14 18 22 26 30
2/3/16
13
CSE30341–Opera,ngSystemPrinciples 25
TimeQuantumandContextSwitchTime
CSE30341–Opera,ngSystemPrinciples 26
TurnaroundTimeVariesWithTheTimeQuantum
80%ofCPUburstsshouldbeshorterthanq
2/3/16
14
CSE30341–Opera,ngSystemPrinciples 27
Mul,levelQueue• Readyqueueispar,,onedintoseparatequeues,e.g.:
– foreground(interac,ve)– background(batch)
• Processpermanentlyinagivenqueue• Eachqueuehasitsownschedulingalgorithm:
– foreground–RR– background–FCFS
• Schedulingmustbedonebetweenthequeues:– Fixedpriorityscheduling;(i.e.,serveallfromforegroundthenfrom
background).Possibilityofstarva;on!– Timeslice–eachqueuegetsacertainamountofCPU,mewhichit
canscheduleamongstitsprocesses;i.e.,80%toforegroundinRR– 20%tobackgroundinFCFS
CSE30341–Opera,ngSystemPrinciples 28
Mul,levelQueueScheduling
2/3/16
15
CSE30341–Opera,ngSystemPrinciples 29
Mul,levelFeedbackQueue
• Aprocesscanmovebetweenthevariousqueues;agingcanbeimplementedthisway
• Mul,level-feedback-queueschedulerdefinedbythefollowingparameters:– numberofqueues– schedulingalgorithmsforeachqueue– methodusedtodeterminewhentoupgradeaprocess– methodusedtodeterminewhentodemoteaprocess– methodusedtodeterminewhichqueueaprocesswillenterwhenthatprocessneedsservice
CSE30341–Opera,ngSystemPrinciples 30
ExampleofMul,levelFeedbackQueue
• Threequeues:– Q0–RRwith,mequantum8milliseconds– Q1–RR,mequantum16milliseconds– Q2–FCFS
• Scheduling– AnewjobentersqueueQ0whichisservedFCFS
• WhenitgainsCPU,jobreceives8milliseconds• Ifitdoesnotfinishin8milliseconds,jobismovedtoqueueQ1
– AtQ1jobisagainservedFCFSandreceives16addi,onalmilliseconds• Ifits,lldoesnotcomplete,itispreemptedandmovedtoqueueQ2
2/3/16
16
CSE30341–Opera,ngSystemPrinciples 31
ThreadScheduling
• Dis,nc,onbetweenuser-levelandkernel-levelthreads
• Kernel-level:threadsscheduled,notprocesses– system-conten;onscope(SCS)–compe,,onamongallthreadsinsystem
• User-level:threadlibraryschedulesuser-levelthreadstorunon“LWP”(“lightweightprocess”)– calledprocess-conten;onscope(PCS)sinceschedulingcompe,,oniswithintheprocess
– typicallydoneviaprioritysetbyprogrammer
CSE30341–Opera,ngSystemPrinciples 32
PthreadScheduling
• APIallowsspecifyingeitherPCSorSCSduringthreadcrea,on– PTHREAD_SCOPE_PROCESSschedulesthreadsusingPCSscheduling
– PTHREAD_SCOPE_SYSTEMschedulesthreadsusingSCSscheduling
• CanbelimitedbyOS–LinuxandMacOSXonlyallowPTHREAD_SCOPE_SYSTEM
2/3/16
17
CSE30341–Opera,ngSystemPrinciples 33
PthreadSchedulingAPI#include <pthread.h> #include <stdio.h> #define NUM_THREADS 5 int main(int argc, char *argv[]) { int i, scope; pthread_t tid[NUM_THREADS]; pthread_attr_t attr; /* get the default attributes */ pthread_attr_init(&attr); /* first inquire on the current scope */ if (pthread_attr_getscope(&attr, &scope) != 0) fprintf(stderr, "Unable to get scheduling scope\n"); else { if (scope == PTHREAD_SCOPE_PROCESS) printf("PTHREAD_SCOPE_PROCESS"); else if (scope == PTHREAD_SCOPE_SYSTEM) printf("PTHREAD_SCOPE_SYSTEM"); else fprintf(stderr, "Illegal scope value.\n"); }
CSE30341–Opera,ngSystemPrinciples 34
PthreadSchedulingAPI /* set the scheduling algorithm to PCS or SCS */ pthread_attr_setscope(&attr, PTHREAD_SCOPE_SYSTEM); /* create the threads */ for (i = 0; i < NUM_THREADS; i++) pthread_create(&tid[i],&attr,runner,NULL); /* now join on each thread */ for (i = 0; i < NUM THREADS; i++) pthread_join(tid[i], NULL); } /* Each thread will begin control in this function */ void *runner(void *param) { /* do some work ... */ pthread_exit(0); }
2/3/16
18
CSE30341–Opera,ngSystemPrinciples 35
Mul,ple-ProcessorScheduling• CPUschedulingmorecomplexwhenmul,pleCPUsareavailable• Homogeneousprocessorswithinamul,processor• Asymmetricmul;processing–onlyoneprocessoraccessesthe
systemdatastructures,allevia,ngtheneedfordatasharing• Symmetricmul;processing(SMP)–eachprocessorisself-
scheduling,allprocessesincommonreadyqueue,oreachhasitsownprivatequeueofreadyprocesses– Currently,mostcommon
• Processoraffinity–processhasaffinityforprocessoronwhichitiscurrentlyrunning– so[affinity– hardaffinity– “migra;on”:processchangesprocessor
CSE30341–Opera,ngSystemPrinciples 36
NUMAandCPUScheduling
CPU
fast access
memory
CPU
fast accessslow access
memory
computer
2/3/16
19
CSE30341–Opera,ngSystemPrinciples 37
LoadBalancing
• IfSMP,needtokeepallCPUsloadedforefficiency
• Loadbalancingajemptstokeepworkloadevenlydistributed
• Pushmigra;on–periodictaskchecksloadoneachprocessor,andifimbalancedfoundpushestaskfromoverloadedCPUtootherCPUs
• Pullmigra;on–idleprocessorspullswai,ngtaskfrombusyprocessor
CSE30341–Opera,ngSystemPrinciples 38
Mul,coreProcessors
• Recenttrendtoplacemul,pleprocessorcoresonsamephysicalchip
• Fasterandconsumeslesspower
• Mul,plethreadspercorealsogrowing– Takesadvantageofmemorystalltomakeprogressonanotherthreadwhilememoryretrievehappens
2/3/16
20
CSE30341–Opera,ngSystemPrinciples 39
Mul,threadedMul,coreSystem
CSE30341–Opera,ngSystemPrinciples 40
Real-TimeScheduling
Deadline
Process,me
Deadline
Process,me
2/3/16
21
CSE30341–Opera,ngSystemPrinciples 41
Priority-BasedScheduling• Manyreal-,meprocessesareperiodic,i.e.,theyrequireCPU
atconstantintervals– Hasprocessing,met,deadlined,periodp– 0≤t≤d≤p– Rateofperiodictaskis1/p
CSE30341–Opera,ngSystemPrinciples 42
RateMontonicScheduling
• Apriorityisassignedbasedontheinverseofitsperiod
• Shorterperiods=higherpriority• Longerperiods=lowerpriority
• P1isassignedahigherprioritythanP2.
2/3/16
22
CSE30341–Opera,ngSystemPrinciples 43
MissedDeadlineswithRateMonotonicScheduling
CSE30341–Opera,ngSystemPrinciples 44
EarliestDeadlineFirstScheduling(EDF)
• Priori,esareassignedaccordingtodeadlines:– theearlierthedeadline,thehigherthepriority– thelaterthedeadline,thelowerthepriority
2/3/16
23
CSE30341–Opera,ngSystemPrinciples 45
POSIXReal-TimeScheduling■ ThePOSIX.1bstandard
■ APIprovidesfunc,onsformanagingreal-,methreads
■ Definestwoschedulingclassesforreal-,methreads:
1. SCHED_FIFO-threadsarescheduledusingaFCFSstrategywithaFIFOqueue.Thereisno,me-slicingforthreadsofequalpriority
2. SCHED_RR-similartoSCHED_FIFOexcept,me-slicingoccursforthreadsofequalpriority
■ Definestwofunc,onsforgefngandsefngschedulingpolicy:
1. pthread_attr_getsched_policy(pthread_attr_t *attr, int *policy)
2. pthread_attr_setsched_policy(pthread_attr_t *attr, int policy)
CSE30341–Opera,ngSystemPrinciples 46
POSIXReal-TimeSchedulingAPI#include <pthread.h> #include <stdio.h> #define NUM_THREADS 5 int main(int argc, char *argv[]) { int i, policy; pthread_t tid[NUM THREADS]; pthread_attr_t attr; /* get the default attributes */ pthread_attr_init(&attr); /* get the current scheduling policy */ if (pthread_attr_getschedpolicy(&attr, &policy) != 0) fprintf(stderr, "Unable to get policy.\n"); else { if (policy == SCHED_OTHER) printf("SCHED_OTHER\n"); else if (policy == SCHED_RR) printf("SCHED_RR\n"); else if (policy == SCHED_FIFO) printf("SCHED_FIFO\n"); }
2/3/16
24
CSE30341–Opera,ngSystemPrinciples 47
POSIXReal-TimeSchedulingAPI(Cont.)
/* set the scheduling policy - FIFO, RR, or OTHER */ if (pthread_attr_setschedpolicy(&attr, SCHED_FIFO) != 0) fprintf(stderr, "Unable to set policy.\n"); /* create the threads */ for (i = 0; i < NUM_THREADS; i++) pthread_create(&tid[i],&attr,runner,NULL); /* now join on each thread */ for (i = 0; i < NUM_THREADS; i++) pthread_join(tid[i], NULL); } /* Each thread will begin control in this function */ void *runner(void *param) { /* do some work ... */ pthread_exit(0); }
CSE30341–Opera,ngSystemPrinciples 48
LinuxSchedulingThroughVersion2.5• Priortokernelversion2.5,ranvaria,onofstandardUNIX
schedulingalgorithm• Version2.5movedtoconstantorderO(1)scheduling,me
– Preemp,ve,prioritybased– Twopriorityranges:,me-sharingandreal-,me– Real-;merangefrom0to99andnicevaluefrom100to140– Mapintoglobalprioritywithnumericallylowervaluesindica,ng
higherpriority– Higherprioritygetslargerq– Taskrun-ableaslongas,mele{in,meslice(ac;ve)– Ifno,mele{(expired),notrun-ableun,lallothertasksusetheir
slices– Allrun-abletaskstrackedinper-CPUrunqueuedatastructure
• Twopriorityarrays(ac,ve,expired)• Tasksindexedbypriority• Whennomoreac,ve,arraysareexchanged
– Workedwell,butpoorresponse,mesforinterac,veprocesses
2/3/16
25
CSE30341–Opera,ngSystemPrinciples 49
LinuxSchedulinginVersion2.6.23+
CompletelyFairScheduler(CFS)• Schedulingclasses
– Eachhasspecificpriority– Schedulerpickshighestprioritytaskinhighestschedulingclass– Ratherthanquantumbasedonfixed,meallotments,basedonpropor,onofCPU,me– 2schedulingclassesincluded,otherscanbeadded
1. default2. real-,me
• Quantumcalculatedbasedonnicevaluefrom-20to+19– Lowervalueishigherpriority– Calculatestargetlatency–intervalof,meduringwhichtaskshouldrunatleastonce– Targetlatencycanincreaseifsaynumberofac,vetasksincreases
• CFSschedulermaintainspertaskvirtualrun;meinvariablevruntime – Associatedwithdecayfactorbasedonpriorityoftask–lowerpriorityishigherdecayrate– Normaldefaultpriorityyieldsvirtualrun,me=actualrun,me
• Todecidenexttasktorun,schedulerpickstaskwithlowestvirtualrun,me
CSE30341–Opera,ngSystemPrinciples 50
Top Related