lecture 5: scheduling · • 5 points/queson. – queson 5 was worth 1 extra point. • memcpy ‐...
TRANSCRIPT
Lecture5:SchedulingCSE120:PrinciplesofOpera=ngSystems
UCSanDiego:SummerSessionI,2009FrankUyeda
Announcements
• Homework2isdueMonday.• Project1:
– Milestone(tonight)Friday7/10
– DeadlineMonday7/13
• MidtermExam– Friday7/17(oneweekfromtoday)– CoverseverythingthroughMonday’slecture
2
PeerWise
• Whatdoesthevalueofasemaphoredenote?– Numberofwai=ngprocesses– Numberofavailableresources– Priorityofwai=ngprocess– Numberofwai=ngthreads
• Requiresinsightaboutwhatthevalueofasemaphore“means”,notjusthowasemaphorebehaves.
3
PeerWise
• Asitua=oninwhichprocesseswaitindefinitelywithinasemaphoreiscalled:– PriorityInversion– Starva=on– BusyWai=ng– BoundedWai=ng– MutualExclusion
• Again,goodwronganswers– Severaltermsthatwe’veheardbefore+afewnewones.
4
Homework
• DueinclassonMondays• Latepolicy:Iwillaccepthomeworkun=lthesolu=onsarepostedonline.– Ifyourassignmentisnotinmyhandswhenthesolu=ongetsposted,Iwillnotacceptit.
5
Homework(cont)
• Ques=on2wasambiguouslyworded.– Gavepointsifyouonlylistedtheopera=onsthatshouldbeprivileged.
– Gavepointsifyoujus=fiedyouranswerwell.• 5points/ques=on.
– Ques=on5wasworth1extrapoint.• Memcpy‐“Protec=ngmemorystate”
– Notprivileged.– Memorystate=meta‐dataconcerningmemory
• TLB,virtualmemorymanager– Normalreads/writesdon’tneedtobeprivileged.– Badreads/writescauseafaultthatishandledbytheOS.
6
Homework(cont)
• Whatyouwanttohearabout:– Security(8)– FutureofOS’s(2)– Real‐=mesystems(2)– SchedulingAlgorithms(2)– HowtobuildyourownOS– Mul=threading&performance– Influen=alOS’s(Linux,Windows)– FileSystems– Interac=onbetweenOSandhardware– Virtualmemory
7
Review
• Locks– Spin‐locksareinefficient
– Disablinginterruptsonlyforshortcri=calsec=ons
• Higher‐levelSynchroniza=onPrimi=ves– Blockedwaitersusingqueue– Interruptscanbeenabledincri=calsec=on
8
Review(2)
• Semaphores– wait()/signal()implementblockingmutualexclusion– Alsousedasatomiccounters(coun=ngsemaphores)
• Monitors– Definemoduleofencapsulateddataandprocedures– Synchronizesexecu=onwithinprocedures
• Onlyonethreadcanexecutewithinamonitorata=me– Reliesuponhigh‐levellanguagesupport
• Condi=onvariables– Usedbythreadsasasynchroniza=onpointtowaitforevents
– Insidemonitors,oroutsidewithlocks
9
Synchroniza=onCaseStudy:Therac‐25
10
• Radia=ontherapymachine(1976)• Completelycomputercontrolledby1982• Therac‐6andTherac‐20werepredecessors
• “25”stoodfor25MeVthatmachinecandisperse• Machinehas2modesofopera=on
• Electronandphoton(X‐Ray)treatments• SonwarewrioenforPDP‐11computer
Note:Imagesandinforma=onfrom[gallagher]
CaseStudy:Therac‐25(2)
11
CaseStudy:Therac‐25(3)
• Accidents– 6accidentsbetween1985and1987
• 3fatal– Cryp=cerrormessagestotechnicians
• “Dosenotdelivered”onerror‐>overdoses– e.g.,13,000‐17,000radsdeliveredinsteadof200‐1000rads
• Modeswitchesoccurredonaccident– Protec=veplate(beamspreader)notdeployedeither
• Whathappened?– Racecondi=ons
12
RaceCondi=on1• Theraceoccurredwhenanoperatoreditedthedosageonthe
consoletooquickly• Themachinehadtwobasicsonwaretasks
– Treatmenttask:checkstoseeifadosageentryhasbeenenteredontheterminal
– Configura=ontask:checkstoseeifmachineryalignedcorrectly(e.g.,thedispersionplate,dispersionmodes)
• Racecondi=on:– Operator“enters”dosage
• TreatmenttaskasksConfigura=ontasktosetthehardware– Operatoreditsdosagewhilethehardwareisbeingconfigured
• Configura=ontasksignals“ready”toTreatmenttaskwithouteditsbeingaccepted
– Racecondi=onoccurswhen“machineready”displayisinresponsetothefirstdosage,nottheedits
13Note:Informa=onfrom[mdwelsh,harvard]
“Race”Condi=on2
• Sonwaretaskusedtomoveaperturethatcontrolsintensityanddirec=onofbeam
• Operatormustpress“set”buoontoconfigureturntableonceprescrip=onentered
• “Setuptest”taskrunsperiodicallytocheckposi=onofturntable– Incrementavariable“Class3”oneachitera=on– Otherwise,aseriesofinterlockchecksareperformedtoensure
apertureincorrectposi=on• ThesecheckswillsetClass3to0whentheyarecomplete
– If“Class3==0”everythingisreadyandthedosagecanbegin• IntegeroverflowwillcauseClass3to“start”at0.
– 8‐bitvariable‐>256itera=onscancauseoverflow
14
CaseStudy:Therac‐25(4)
• Whyweren’ttheseerrorscaught?– RaceCondi=on1
• Hardwareinterlocksmaskedprevious(reused)code– Previously,thephysicalhardwarewouldnotallowx‐raymodetooccurwithoutbeamspreader(plate)
• Errorsoccurredonlyonstrange(includingerroneouskeystrokes)
– Notcaughtduringtes=ng– RaceCondi=on2
• Onlyreproducibleaner256itera=ons,andtherightsequenceofkeystrokesonthatwrap‐around
• Unfortunatelyeven1errorisn’tacceptable15
GoalsToday
• DescribewheretheOpera=ngSystemmustmakeschedulingdecisions
• Describepossiblegoalsofaschedulingpolicy• LearnbasicschedulingpoliciesanOSmayuse
• Understandthebasiclimita=onsofeachpolicy
16
SchedulingDecisions• Processes,Threads,SchedulingQueues,WaitQueues,…..
17
Running
Wai=ng
Ready
scheduleprocess
unscheduleprocess
blockingonevent(I/O)
eventoccurred(e.g.,I/Oreceived)
processexit
New
Terminated
createprocess
head
tail
ReadyqueueFirefoxPCB XServerPCB IdlePCB
head
tail
DiskI/OPCBEmacsPCB BashPCB
Monitor bounded_buffer {
Condition not_full; …other variables… Condition not_empty;
void put_resource() { …wait(not_full)… ….. …signal(not_empty)… }
Resource get_resource() { ….. } }
Wai=ngtoenter
Wai=ngoncondi=onvariables
Execu=nginsidethemonitor
SchedulingOverview
• Indiscussingprocessmanagementandsynchroniza=on,wetalkedaboutcontextswitchingamongprocesses/threadsonthereadyqueue
• Butwehaveglossedoverthedetailsofexactlywhichthreadischosenfromthereadyqueue
• Makingthisdecisioniscalledscheduling• Inthislecture,we’lllookat:
– Thegoalsofscheduling– Starva=on– Variouswell‐knownschedulingalgorithms
– StandardUnixschedulingalgorithms
18
Mul=programming
• Inamul=programmingsystem,wetrytoincreaseCPUu=liza=onandjobthroughputbyoverlappingI/OandCPUac=vi=es– Doingthisrequiresacombina=onofmechanismsandpolicy
• Wehavecoveredthemechanisms– Contextswitching,howandwhenithappens– Schedulingqueuesandexecu=onstates
• Nowwe’lllookatthepolicies– Whichprocess(thread)torun,forhowlong,etc.
• We’llrefertoschedulableen==esasjobs(standardusage)–couldbeprocesses,threads,people,etc.
19
SchedulingHorizons
• Schedulingworksattwolevelsinanopera=ngsystem– Todeterminethemul=programminglevel–thenumberofjobsloadedintoprimarymemory
• Movingjobsto/frommemoryisonencalledswapping– Todecidewhatjobtorunnexttoguarantee“goodservice”
• Goodservicecouldbeoneofmanydifferentcriteria
• Thesedecisionsareknownaslong‐termandshort‐termschedulingdecisions,respec=vely– Long‐termschedulinghappensrela=velyinfrequently
• Significantoverheadinswappingaprocessouttodisk– Short‐termschedulinghappensrela=velyfrequently
• Wanttominimizetheoverheadofscheduling
20
SchedulingGoals
• Schedulingalgorithmscanhavemanydifferentgoals:– CPUu=liza=on– I/Ou=liza=on– Jobthroughput(#jobs/unit=me)
– Turnaround=me(Tfinish–Tarrival)
– Wai=ng=me(Avg(Twait):avg=mespentonwaitqueues)
– Response=me(Avg(Tready):avg=mespentonreadyqueue)
• Batchsystems– Striveforjobthroughput,turnaround=me(supercomputers)
• Interac=vesystems– Strivetominimizeresponse=meforinterac=vejobs(PC)
21
Starva=on
• Starva=onoccurswhenajobcannotmakeprogressbecausesomeotherjobhastheresourceitrequires– We’veseenlocks,Monitors,Semaphores,etc.– ThesamethingcanhappenwiththeCPU!
• Starva=oncanbeasideeffectofsynchroniza=on– Constantsupplyofreadersalwaysblocksoutwriters– Well‐wrioencri=calsec=onsshouldensureboundedwai=ng
• Starva=onusuallyasideeffectoftheschedulingalgorithm– Ahighpriorityprocessalwayspreventsalowpriorityprocess
fromrunningontheCPU– Onethreadalwaysbeatsanotherwhenacquiringalock
22
OSScheduler
• Thescheduler(akadispatcher)isthemodulethatmanipulatesthequeues,movingjobstoandfromstates
• Theschedulingalgorithmdetermineswhichjobsarechosentorunnextandwhatqueuestheywaiton
• Ingeneral,theschedulerruns:– Whenajobswitchesstates(running,wai=ng,etc.)– Whenaninterruptoccurs(e.g.,=mer,preemp=on)
– Whenajobiscreatedorterminated
• We’lldiscussschedulingalgorithmsintwocontexts– Apreemp=veschedulercaninterruptarunningjob
– Anon‐preemp=veschedulerwaitsforrunningjobtoblock
23
FCFS/FIFOAlgorithms
• First‐comefirst‐served(FCFS),first‐infirst‐out(FIFO)– Jobsarescheduledinorderofarrivaltoreadyqueue– “Real‐world”schedulingofpeopleinlines(e.g.,supermarket)
– Typicallynon‐preemp=ve(nocontextswitchingatmarket)– Jobstreatedequally,nostarva=on
• Problem– Averagewai=ng=mecanbelargeifsmalljobswaitbehindlongones(highturnaround=me)
• Youhaveabasket,butyou’restuckbehindsomeonewithacart• Thisiscalled“Head‐of‐line(HoL)Blocking”
24
ShortestJobFirst(SJF)
• ShortestJobFirst(SJF)– ChoosethejobwiththesmallestexpectedCPUburst
• Personwithsmallestnumberofitemstobuy– Provablyop=malminimumaveragewai=ng=me
• Problem– UsuallyimpossibletoknowsizeofCPUburst
• Likechoosingpersoninlinewithoutlookinginsidebasket/cart• Howcanyoumakeareasonableguess?• Canpoten=allystarve
– Flavors• Canbeeitherpreemp=veornon‐preemp=ve• Preemp=veSJFiscalledshortestremaining=mefirst(SRTF)
25
RoundRobin(RR)
• RoundRobin– Excellentfor=mesharing– Readyqueueistreatedasacircularqueue(FIFO)– Eachjobisgivena=meslicecalledaquantum
• Ajobexecutesforthedura=onofthequantum,orun=litblocksorisinterrupted
• Ifitblocks,itisplacedontheappropriatequeue,otherwise,itisputbackonthereadyqueue(almostlikeathread_yield())
– Preemp=ve– Nostarva=on
• Problem– Contextswitchesarefrequentandneedtobeveryfast– Howtosetthe=mequantum?
26
Example
• FCFS• SJF• SRTF(quantum=2)• RoundRobin(quantum=2)
P1:=me=10P2:=me=1P3:=me=5P4:=me=3
27
PriorityScheduling
• PriorityScheduling– Choosenextjobbasedonpriority
• Airlinecheck‐inforfirstclasspassengers– CanimplementSJF,priority=1/(expectedCPUburst)– Alsocanbeeitherpreemp=veornon‐preemp=ve
• Problem– Starva=on–lowpriorityjobscanwaitindefinitely
• Solu=on– “Age”processes
• Increasepriorityasafunc=onofwai=ng=me• Decreasepriorityasafunc=onofCPUconsump=on
28
LooeryScheduling
• Arandomizedpriorityscheme– Eachjobisgivensomenumberoflooery=ckets
• Numberof=cketsispropor=onaltotheirpriority(inthiscase,higherprioritymeansmore=ckets)
– Oneachschedulinginterval,a=cketischosen• Jobholdingthewinning=cketisscheduled
• Howdoesthisavoidstarva=on?– Evenlowpriorityjobshaveachancetorun
29
CombiningAlgorithms
• Schedulingalgorithmscanbecombined– Havemul=plequeues– Useadifferentalgorithmforeachqueue– Moveprocessesamongqueues
• Example:Mul=pleLevelFeedbackQueues(MLFQ)– Mul=plequeuesrepresen=ngdifferentjobtypes
• Interac=ve,CPU‐bound,batch,system,etc.
– Queueshavepriori=es,jobsonsamequeuescheduledRR– Jobscanmoveamongqueuesbaseduponexecu=onhistory
• Feedback:Switchfrominterac=vetoCPU‐boundbehavior
30
UnixScheduler
• ThecanonicalUnixschedulerusesaMLFQ– 3‐4classesspanning~170prioritylevels
• Timesharing:first60priori=es• System:next40priori=es• Real‐=me:next60priori=es• Interrupt:next10(Solaris)
• Priorityschedulingacrossqueues,RRwithinaqueue– Theprocesswiththehighestpriorityalwaysruns– ProcesseswiththesamepriorityarescheduledRR
• Processesdynamicallychangepriority– Increasesover=meifprocessblocksbeforeendofquantum– Decreasesover=meifprocessusesen=requantum
31
Mo=va=onofUnixScheduler
• TheideabehindtheUnixscheduleristorewardinterac=veprocessesoverCPUhogs
• Interac=veprocesses(shell,editor,etc.)typicallyrunusingshortCPUbursts– Theydonotfinishquantumbeforewai=ngformoreinput
• Wanttominimizeresponse=me– Timefromkeystroke(pu|ngprocessonreadyqueue)toexecu=ngkeystrokehandler(processrunning)
– Don’twanteditortowaitun=lCPUhogfinishesquantum
• Thispolicydelaysexecu=onofCPU‐boundjobs– Butthat’sok
32
SchedulingSummary
• Scheduler(dispatcher)isthemodulethatgetsinvokedwhenacontextswitchneedstohappen
• Schedulingalgorithmdetermineswhichprocessruns,whereprocessesareplacedonqueues
• Manypoten=algoalsofschedulingalgorithms– U=liza=on,throughput,wait=me,response=me,etc.
• Variousalgorithmstomeetthesegoals– FCFS/FIFO,SJF,Priority,RR
• Cancombinealgorithms– Mul=ple‐levelfeedbackqueues– Unixexample
33
NextTime
• ReadChapter7(Deadlocks)• Homework2dueonMonday.
• CheckWebsiteforcourseannouncements– hop://www.cs.ucsd.edu/classes/su09/cse120
34