+ All Categories
Home > Documents > Lecture 5: Scheduling · • 5 points/queson. – Queson 5 was worth 1 extra point. • Memcpy ‐...

Lecture 5: Scheduling · • 5 points/queson. – Queson 5 was worth 1 extra point. • Memcpy ‐...

Date post: 08-Oct-2020
Category:
Upload: others
View: 0 times
Download: 0 times
Share this document with a friend
34
Lecture 5: Scheduling CSE 120: Principles of Opera=ng Systems UC San Diego: Summer Session I, 2009 Frank Uyeda
Transcript
Page 1: Lecture 5: Scheduling · • 5 points/queson. – Queson 5 was worth 1 extra point. • Memcpy ‐ “Protecng memory state” – Not privileged. – Memory state = meta‐data concerning

Lecture5:SchedulingCSE120:PrinciplesofOpera=ngSystems

UCSanDiego:SummerSessionI,2009FrankUyeda

Page 2: Lecture 5: Scheduling · • 5 points/queson. – Queson 5 was worth 1 extra point. • Memcpy ‐ “Protecng memory state” – Not privileged. – Memory state = meta‐data concerning

Announcements

•  Homework2isdueMonday.•  Project1:

– Milestone(tonight)Friday7/10

– DeadlineMonday7/13

•  MidtermExam– Friday7/17(oneweekfromtoday)– CoverseverythingthroughMonday’slecture

2

Page 3: Lecture 5: Scheduling · • 5 points/queson. – Queson 5 was worth 1 extra point. • Memcpy ‐ “Protecng memory state” – Not privileged. – Memory state = meta‐data concerning

PeerWise

•  Whatdoesthevalueofasemaphoredenote?– Numberofwai=ngprocesses– Numberofavailableresources– Priorityofwai=ngprocess– Numberofwai=ngthreads

•  Requiresinsightaboutwhatthevalueofasemaphore“means”,notjusthowasemaphorebehaves.

3

Page 4: Lecture 5: Scheduling · • 5 points/queson. – Queson 5 was worth 1 extra point. • Memcpy ‐ “Protecng memory state” – Not privileged. – Memory state = meta‐data concerning

PeerWise

•  Asitua=oninwhichprocesseswaitindefinitelywithinasemaphoreiscalled:–  PriorityInversion–  Starva=on–  BusyWai=ng–  BoundedWai=ng– MutualExclusion

•  Again,goodwronganswers–  Severaltermsthatwe’veheardbefore+afewnewones.

4

Page 5: Lecture 5: Scheduling · • 5 points/queson. – Queson 5 was worth 1 extra point. • Memcpy ‐ “Protecng memory state” – Not privileged. – Memory state = meta‐data concerning

Homework

•  DueinclassonMondays•  Latepolicy:Iwillaccepthomeworkun=lthesolu=onsarepostedonline.–  Ifyourassignmentisnotinmyhandswhenthesolu=ongetsposted,Iwillnotacceptit.

5

Page 6: Lecture 5: Scheduling · • 5 points/queson. – Queson 5 was worth 1 extra point. • Memcpy ‐ “Protecng memory state” – Not privileged. – Memory state = meta‐data concerning

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

Page 7: Lecture 5: Scheduling · • 5 points/queson. – Queson 5 was worth 1 extra point. • Memcpy ‐ “Protecng memory state” – Not privileged. – Memory state = meta‐data concerning

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

Page 8: Lecture 5: Scheduling · • 5 points/queson. – Queson 5 was worth 1 extra point. • Memcpy ‐ “Protecng memory state” – Not privileged. – Memory state = meta‐data concerning

Review

•  Locks– Spin‐locksareinefficient

– Disablinginterruptsonlyforshortcri=calsec=ons

•  Higher‐levelSynchroniza=onPrimi=ves– Blockedwaitersusingqueue–  Interruptscanbeenabledincri=calsec=on

8

Page 9: Lecture 5: Scheduling · • 5 points/queson. – Queson 5 was worth 1 extra point. • Memcpy ‐ “Protecng memory state” – Not privileged. – Memory state = meta‐data concerning

Review(2)

•  Semaphores–  wait()/signal()implementblockingmutualexclusion–  Alsousedasatomiccounters(coun=ngsemaphores)

•  Monitors–  Definemoduleofencapsulateddataandprocedures–  Synchronizesexecu=onwithinprocedures

•  Onlyonethreadcanexecutewithinamonitorata=me–  Reliesuponhigh‐levellanguagesupport

•  Condi=onvariables–  Usedbythreadsasasynchroniza=onpointtowaitforevents

–  Insidemonitors,oroutsidewithlocks

9

Page 10: Lecture 5: Scheduling · • 5 points/queson. – Queson 5 was worth 1 extra point. • Memcpy ‐ “Protecng memory state” – Not privileged. – Memory state = meta‐data concerning

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]

Page 11: Lecture 5: Scheduling · • 5 points/queson. – Queson 5 was worth 1 extra point. • Memcpy ‐ “Protecng memory state” – Not privileged. – Memory state = meta‐data concerning

CaseStudy:Therac‐25(2)

11

Page 12: Lecture 5: Scheduling · • 5 points/queson. – Queson 5 was worth 1 extra point. • Memcpy ‐ “Protecng memory state” – Not privileged. – Memory state = meta‐data concerning

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

Page 13: Lecture 5: Scheduling · • 5 points/queson. – Queson 5 was worth 1 extra point. • Memcpy ‐ “Protecng memory state” – Not privileged. – Memory state = meta‐data concerning

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]

Page 14: Lecture 5: Scheduling · • 5 points/queson. – Queson 5 was worth 1 extra point. • Memcpy ‐ “Protecng memory state” – Not privileged. – Memory state = meta‐data concerning

“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

Page 15: Lecture 5: Scheduling · • 5 points/queson. – Queson 5 was worth 1 extra point. • Memcpy ‐ “Protecng memory state” – Not privileged. – Memory state = meta‐data concerning

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

Page 16: Lecture 5: Scheduling · • 5 points/queson. – Queson 5 was worth 1 extra point. • Memcpy ‐ “Protecng memory state” – Not privileged. – Memory state = meta‐data concerning

GoalsToday

•  DescribewheretheOpera=ngSystemmustmakeschedulingdecisions

•  Describepossiblegoalsofaschedulingpolicy•  LearnbasicschedulingpoliciesanOSmayuse

•  Understandthebasiclimita=onsofeachpolicy

16

Page 17: Lecture 5: Scheduling · • 5 points/queson. – Queson 5 was worth 1 extra point. • Memcpy ‐ “Protecng memory state” – Not privileged. – Memory state = meta‐data concerning

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

Page 18: Lecture 5: Scheduling · • 5 points/queson. – Queson 5 was worth 1 extra point. • Memcpy ‐ “Protecng memory state” – Not privileged. – Memory state = meta‐data concerning

SchedulingOverview

•  Indiscussingprocessmanagementandsynchroniza=on,wetalkedaboutcontextswitchingamongprocesses/threadsonthereadyqueue

•  Butwehaveglossedoverthedetailsofexactlywhichthreadischosenfromthereadyqueue

•  Makingthisdecisioniscalledscheduling•  Inthislecture,we’lllookat:

–  Thegoalsofscheduling–  Starva=on–  Variouswell‐knownschedulingalgorithms

–  StandardUnixschedulingalgorithms

18

Page 19: Lecture 5: Scheduling · • 5 points/queson. – Queson 5 was worth 1 extra point. • Memcpy ‐ “Protecng memory state” – Not privileged. – Memory state = meta‐data concerning

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

Page 20: Lecture 5: Scheduling · • 5 points/queson. – Queson 5 was worth 1 extra point. • Memcpy ‐ “Protecng memory state” – Not privileged. – Memory state = meta‐data concerning

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

Page 21: Lecture 5: Scheduling · • 5 points/queson. – Queson 5 was worth 1 extra point. • Memcpy ‐ “Protecng memory state” – Not privileged. – Memory state = meta‐data concerning

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

Page 22: Lecture 5: Scheduling · • 5 points/queson. – Queson 5 was worth 1 extra point. • Memcpy ‐ “Protecng memory state” – Not privileged. – Memory state = meta‐data concerning

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

Page 23: Lecture 5: Scheduling · • 5 points/queson. – Queson 5 was worth 1 extra point. • Memcpy ‐ “Protecng memory state” – Not privileged. – Memory state = meta‐data concerning

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

Page 24: Lecture 5: Scheduling · • 5 points/queson. – Queson 5 was worth 1 extra point. • Memcpy ‐ “Protecng memory state” – Not privileged. – Memory state = meta‐data concerning

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

Page 25: Lecture 5: Scheduling · • 5 points/queson. – Queson 5 was worth 1 extra point. • Memcpy ‐ “Protecng memory state” – Not privileged. – Memory state = meta‐data concerning

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

Page 26: Lecture 5: Scheduling · • 5 points/queson. – Queson 5 was worth 1 extra point. • Memcpy ‐ “Protecng memory state” – Not privileged. – Memory state = meta‐data concerning

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

Page 27: Lecture 5: Scheduling · • 5 points/queson. – Queson 5 was worth 1 extra point. • Memcpy ‐ “Protecng memory state” – Not privileged. – Memory state = meta‐data concerning

Example

•  FCFS•  SJF•  SRTF(quantum=2)•  RoundRobin(quantum=2)

P1:=me=10P2:=me=1P3:=me=5P4:=me=3

27

Page 28: Lecture 5: Scheduling · • 5 points/queson. – Queson 5 was worth 1 extra point. • Memcpy ‐ “Protecng memory state” – Not privileged. – Memory state = meta‐data concerning

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

Page 29: Lecture 5: Scheduling · • 5 points/queson. – Queson 5 was worth 1 extra point. • Memcpy ‐ “Protecng memory state” – Not privileged. – Memory state = meta‐data concerning

LooeryScheduling

•  Arandomizedpriorityscheme–  Eachjobisgivensomenumberoflooery=ckets

•  Numberof=cketsispropor=onaltotheirpriority(inthiscase,higherprioritymeansmore=ckets)

–  Oneachschedulinginterval,a=cketischosen•  Jobholdingthewinning=cketisscheduled

•  Howdoesthisavoidstarva=on?–  Evenlowpriorityjobshaveachancetorun

29

Page 30: Lecture 5: Scheduling · • 5 points/queson. – Queson 5 was worth 1 extra point. • Memcpy ‐ “Protecng memory state” – Not privileged. – Memory state = meta‐data concerning

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

Page 31: Lecture 5: Scheduling · • 5 points/queson. – Queson 5 was worth 1 extra point. • Memcpy ‐ “Protecng memory state” – Not privileged. – Memory state = meta‐data concerning

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

Page 32: Lecture 5: Scheduling · • 5 points/queson. – Queson 5 was worth 1 extra point. • Memcpy ‐ “Protecng memory state” – Not privileged. – Memory state = meta‐data concerning

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

Page 33: Lecture 5: Scheduling · • 5 points/queson. – Queson 5 was worth 1 extra point. • Memcpy ‐ “Protecng memory state” – Not privileged. – Memory state = meta‐data concerning

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

Page 34: Lecture 5: Scheduling · • 5 points/queson. – Queson 5 was worth 1 extra point. • Memcpy ‐ “Protecng memory state” – Not privileged. – Memory state = meta‐data concerning

NextTime

•  ReadChapter7(Deadlocks)•  Homework2dueonMonday.

•  CheckWebsiteforcourseannouncements–  hop://www.cs.ucsd.edu/classes/su09/cse120

34


Recommended