Upload
others
View
3
Download
0
Embed Size (px)
Citation preview
Announcements
• Homework2isdue.– Nonewhomeworkthisweek.
• Project1:– Deadlinetonightatmidnight.
• Project2isnowonline.
• MidtermExam– Friday7/17– Coverseverythingthroughtoday’slecture
2
Review
• SchedulingMetrics– Averagewait=me,CPUu=liza=on,etc
• SchedulingAlgorithms– Firstcome,firstserve– ShortestJobFirst/ShortestTimeRemainingFirst– RoundRobin– Priority
3
MarsPathfinder
• July4,1997landingonMar=alsurface• Seriesofso[wareglitchesstartedafewdaysa[er
landing– EventuallydebuggedandpatchedremotelyfromEarth
Note:informa=onandimagesadaptedfrom[mdwelsh,harvard] 4
VxWorksOpera=ngSystem
• Real‐=meOS(WindRiverSystems)– OSusesapriorityscheduler
• Importantforreal‐=metasks
– Concurrentthreadscommunicateoninforma=onbus• Sharedmemoryareaguardedbyamonitor
Informa=onBus(Monitor)
MutexWeatherDataThread
Communica=onThread
Informa=onBusThread
5
VxWorksOpera=ngSystem
• Real‐=meOS(WindRiverSystems)– OSusesapriorityscheduler
• Importantforreal‐=metasks
– Concurrentthreadscommunicateoninforma=onbus• Sharedmemoryareaguardedbyamonitor
Informa=onBus(Monitor)
MutexWeatherDataThread
Communica=onThread
Informa=onBusThread
Obtainmutex;writedata
Waitformutextoreaddata
6
VxWorksOpera=ngSystem
• Real‐=meOS(WindRiverSystems)– OSusesapriorityscheduler
• Importantforreal‐=metasks
– Concurrentthreadscommunicateoninforma=onbus• Sharedmemoryareaguardedbyamonitor
Informa=onBus(Monitor)
MutexWeatherDataThread
Communica=onThread
Informa=onBusThread
Freemutex;
Waitformutextoreaddata
7
VxWorksOpera=ngSystem
• Real‐=meOS(WindRiverSystems)– OSusesapriorityscheduler
• Importantforreal‐=metasks
– Concurrentthreadscommunicateoninforma=onbus• Sharedmemoryareaguardedbyamonitor
Informa=onBus(Monitor)
MutexWeatherDataThread
Communica=onThread
Informa=onBusThreadObtainmutex;readdata
8
VxWorksOpera=ngSystem
• Introducepriori=estothesethreads
Informa=onBus(Monitor)
MutexWeatherDataThread
Communica=onThread
Informa=onBusThread
Lowpriority
Mediumpriority
Highpriority
9
VxWorksOpera=ngSystem
• Introducepriori=estothesethreads
Informa=onBus(Monitor)
MutexWeatherDataThread
Communica=onThread
Informa=onBusThread
Lowpriority
Mediumpriority
Highpriority
Obtainmutex;writedata
Waitformutextoreaddata
10
VxWorksOpera=ngSystem
• Introducepriori=estothesethreads– OS=merinterrupt
– Schedulerrunshigherprioritythread
Informa=onBus(Monitor)
MutexWeatherDataThread
Communica=onThread
Informa=onBusThread
Lowpriority
Mediumpriority
Highpriority
Obtainmutex;writedata
Waitformutextoreaddata
Interrupt!
11
VxWorksOpera=ngSystem• Introducepriori=estothesethreads
– OS=merinterrupt– Schedulerrunshigherprioritythread
• Communica=onthreadhashigherprioritythanweatherdatathread• Butthehighpriorityinforma=onbusthreadisstuckwai=ng
– Thisscenarioiscalledpriorityinversion
Informa=onBus(Monitor)
MutexWeatherDataThread
Communica=onThread
Informa=onBusThread
Lowpriority
Mediumpriority
Highpriority
Obtainmutex;writedata
Waitformutextoreaddata
12
PriorityInversion
• Problemdescrip=on– Ahighprioritythreadisstuckwai=ngforalowprioritythreadtofinish
– Inthiscase,themediumprioritythreadwasholdingupthelow‐prioritythread
• Howdowefixthis?
13
PriorityInheritance
• Lowprioritythreadwinstheraceforthelock.
Informa=onBus(Monitor)
MutexWeatherDataThread
Communica=onThread
Informa=onBusThread
Lowpriority
Mediumpriority
Highpriority
Obtainmutex;writedata
Waitformutextoreaddata
14
PriorityInheritance
• Lowprioritythreadwinstheraceforthelock.• Lowprioritythreadtemporarilyupgradedtohighpriority.
Informa=onBus(Monitor)
MutexWeatherDataThread
Communica=onThread
Informa=onBusThread
Highpriority
Mediumpriority
Highpriority
Obtainmutex;writedata
Waitformutextoreaddata
15
PriorityInheritance
• Lowprioritythreadwinstheraceforthelock.• Lowprioritythreadtemporarilyupgradedtohighpriority.
Informa=onBus(Monitor)
MutexWeatherDataThread
Communica=onThread
Informa=onBusThread
Highpriority
Mediumpriority
Highpriority
Obtainmutex;writedata
Waitformutextoreaddata
16
Interrupt!
Informa=onBus(Monitor)
MutexWeatherDataThread
Communica=onThread
Informa=onBusThread
HighpriorityLowpriority
Mediumpriority
Highpriority
Releasemutex
Waitformutextoreaddata
17
• Lowprioritythreadwinstheraceforthelock.• Lowprioritythreadtemporarilyupgradedtohighpriority.• A[erreleasingmutex,downgradedtolowpriority.
PriorityInheritance
PriorityInheritance
• Solu=onapproach:PriorityInheritance– Allowathreadtoinheritthepriorityofanythreadthatis
wai=ngforit– Blocked,higherprioritythreads“donate”theirprioritytolower
prioritythreads• Dona=ononlyoccursaslongasthehigherprioritythreadisblocked–oncethisisnolongertrue,threadsretaintheiroriginalpriority
• Forourexample– Mediumthreadwillnolongerblocklowprioritythread– FortheMarspathfinder,theyuploadedthefixremotelyand
everythingwasfinea[erthat• Abletoreplaybugsinlab;resetrealmachinetoknown“goodstate”anduploadfix–goodlessonsfordevelopinglarge,real‐=mesystems
18
Starva=onandDeadlock• Twotypesofresources:
– Preemp=ble:cantakeawayfromathread(e.g.,CPU..)– Non‐preemp=ble:can’ttakeawayfromathread(e.g.,mutex,lock..)
• Starva=on:– Athreadnevermakesprogressbecauseotherthreadsareusingaresourceitneeds
• Deadlock:– Acircularresourcedependencybetweenmul=plethreads(note:threadsisinterchangeablewithprocesses,jobs,tasksinthiscontext)
• Starva=onisnotthesameasdeadlock 19
DeadlockExampleMonitor account { double balance;
double withdraw(amount) { balance = balance - amount; return balance; }
double deposit(amount) { balance = balance + amount; return balance; } }
// transfer from Alice to Bob Alice.mutex.acquire(); Alice.withdraw(amount); Bob.mutex.acquire(); Bob.deposit(amount); …Alice.mutex.release();
// two bank accounts protected by a // monitor, with an associated mutex
Monitor Alice, Bob, Eve;
Thread1
// transfer from Bob to Alice Bob.mutex.acquire(); Bob.withdraw(amount); Alice.mutex.acquire(); Alice.deposit(amount); …Bob.mutex.release();
Thread2
Thread1andThread2areDeadlocked!
20
Deadlock
• Processesthatacquiremul=pleresourcesaredependentonthoseresources– E.g.,lockssemaphores,monitors,etc.
• Whatifoneprocesstriestoallocatearesourcethatasecondprocessholds,andvice‐versa?– Neithercanmakeprogress!
• Wecallthissitua=ondeadlockandwe’lllookat:– Defini=onandcondi=onsnecessaryfordeadlock– Representa=onofdeadlockcondi=ons– Approachestodealingwithdeadlock
21
DeadlockDefini=on
• Defini=on:– Deadlockexistsamongasetofprocessesifeveryprocessiswai=ngforaneventthatcanbecausedonlybyanotherprocessintheset.
• Deadlockisaproblemthatcanarise:– Whenprocessescompeteforaccesstolimitedresources
– Whenprocessesareincorrectlysynchronized
22
Condi=onsforDeadlock
Deadlockcanexistifandonlyiffourcondi=onshold:• Mutualexclusion–Atleastoneresourcemustbeheld
inanon‐sharablemode(i.e.,onlyoneinstance)• Holdandwait–Theremustbeoneprocessholding
oneresourceandwai=ngforanotherresource• Nopreemp=on–resourcescannotbepreempted
(i.e.,cri=calsec=onscannotbeabortedexternally)• Circularwait–Theremustexistasetofprocesses{P1,
P2,P3,…,Pn}suchthatP1iswai=ngforaresourceheldbyP2,P2iswai=ngforP3,…,andPnforP1
23
ResourceAlloca=onGraph
• Deadlockcanbedescribedusingaresourcealloca=ongraph(RAG)
• TheRAGconsistsofsetsofver=cesP={P1,P2,P3,…,Pn}ofprocessesandR={R1,R2,R3,…,Rm}resources– AdirectededgefromaprocesstoaresourcePiRj,impliesthatPihasrequestedRj
– AdirectededgefromaresourcetoaprocessRiPj,impliesthatRihasbeenacquiredbyPj
– Eachresourcehasafixednumberofunits
• Ifthegraphhasnocycles,deadlockcannotexist• Ifthegraphhasacycle,deadlockmayexist
24
DealingwithDeadlock
Therearefourwaystodealwithdeadlock:• Ignoreit
– Doyoufeellucky?
• Preven=on– Makeitimpossiblefordeadlocktohappen
• Avoidance– Controlalloca=onofresources
• Detec=onandrecovery– Lookforacycleindependencies
26
DeadlockPreven=on
Preventatleastoneofnecessarycondi=ons:• Mutualexclusion
– Makeresourcessharable
• Holdandwait– Processcan’tholdoneresourcewhenreques=nganother– Processrequests,releasesallneededresourcesatonce
• Preemp=on– OScanpreemptresource(costly)
• Circularwait– Imposeanordering(numbering)ontheresourcesand
requesttheminorder(populartechnique)27
DeadlockAvoidance
• Howtoavoiddeadlock– Requireprocessestoprovideinforma=oninadvanceaboutwhatresourceswillbeneeded
– Systemgrantsresourcestoavoidcirculari=es(waitdependencies)
• Toughtodo– Hardtodetermineallresourceneedsinadvance
– Goodtheore=calproblem,notasprac=cal
28
Banker’sAlgorithm
TheBanker’sAlgorithmistheclassicapproachtodeadlockavoidanceforresourceswithmul=pleunits
• Assignacreditlimittoeachcustomer(task)– Maximumcreditclaimmustbestatedinadvance
• Rejectanyrequestthatleadstoadangerousstate– Adangerousstateisonewhereasuddenrequestbyany
customerforthefullcreditlimitcouldleadtodeadlock– Arecursivereduc=onprocedurerecognizesdangerous
states• Inprac=ce,thesystemmustkeepresourceusagewell
belowcapacitytomaintainaresourcesurplus– Rarelyusedinprac=ceduetolowresourceu=liza=on
29
Banker’sAlgorithmExample
A B C
2 2 1
30
Availablesystemresources
A B C
P1 3 2 2
P2 2 1 2
P3 2 4 1
Currentlyallocatedresources
perProcess
A B C
P1 1 1 0
P2 2 0 1
P3 0 1 1
Processes(maximumresources)
Banker’sAlgorithmExample
A B C
2 2 1
31
Availablesystemresources
A B C
P1 3 2 2
P2 2 1 2
P3 2 4 1
Currentlyallocatedresources
perProcess
A B C
P1 1 1 0
P2 2 0 1
P3 0 1 1
Processes(maximumresources)
Astateissafeifitispossibleforallprocessestoexecuteandterminate
Banker’sAlgorithmExample
A B C
2 1 0
32
Availablesystemresources
A B C
P1 3 2 2
P2 2 1 2
P3 2 4 1
Currentlyallocatedresources
perProcess
A B C
P1 1 1 0
P2 2 1 2
P3 0 1 1
Processes(maximumresources)
Astateissafeifitispossibleforallprocessestoexecuteandterminate
P2canterminate
Banker’sAlgorithmExample
A B C
4 2 2
33
Availablesystemresources
A B C
P1 3 2 2
P2 2 1 2
P3 2 4 1
Currentlyallocatedresources
perProcess
A B C
P1 1 1 0
P2 0 0 0
P3 0 1 1
Processes(maximumresources)
Astateissafeifitispossibleforallprocessestoexecuteandterminate
Banker’sAlgorithmExample
A B C
2 1 0
34
Availablesystemresources
A B C
P1 3 2 2
P2 2 1 2
P3 2 4 1
Currentlyallocatedresources
perProcess
A B C
P1 3 2 2
P2 0 0 0
P3 0 1 1
Processes(maximumresources)
Astateissafeifitispossibleforallprocessestoexecuteandterminate
P1canterminate
Banker’sAlgorithmExample
A B C
5 3 2
35
Availablesystemresources
A B C
P1 3 2 2
P2 2 1 2
P3 2 4 1
Currentlyallocatedresources
perProcess
A B C
P1 0 0 0
P2 0 0 0
P3 0 1 1
Processes(maximumresources)
Astateissafeifitispossibleforallprocessestoexecuteandterminate
Banker’sAlgorithmExample
A B C
3 0 2
36
Availablesystemresources
A B C
P1 3 2 2
P2 2 1 2
P3 2 4 1
Currentlyallocatedresources
perProcess
A B C
P1 0 0 0
P2 0 0 0
P3 2 4 1
Processes(maximumresources)
Astateissafeifitispossibleforallprocessestoexecuteandterminate
P3canterminate
Banker’sAlgorithmExample
A B C
2 2 1
37
Availablesystemresources
A B C
P1 3 2 2
P2 2 1 2
P3 2 4 1
Currentlyallocatedresources
perProcess
A B C
P1 1 1 0
P2 2 0 1
P3 0 1 1
Processes(maximumresources)
P1’srequestA B C
2 1 1
Banker’sAlgorithmExample
A B C
0 1 0
38
Availablesystemresources
A B C
P1 3 2 2
P2 2 1 2
P3 2 4 1
Currentlyallocatedresources
perProcess
A B C
P1 3 2 1
P2 2 0 1
P3 0 1 1
Processes(maximumresources)
P1’srequestA B C
2 1 1
P1cannotterminate
Banker’sAlgorithmExample
A B C
0 1 0
39
Availablesystemresources
A B C
P1 3 2 2
P2 2 1 2
P3 2 4 1
Currentlyallocatedresources
perProcess
A B C
P1 3 2 1
P2 2 0 1
P3 0 1 1
Processes(maximumresources)
P1’srequestA B C
2 1 1
P2cannotterminate
Banker’sAlgorithmExample
A B C
0 1 0
40
Availablesystemresources
A B C
P1 3 2 2
P2 2 1 2
P3 2 4 1
Currentlyallocatedresources
perProcess
A B C
P1 3 2 1
P2 2 0 1
P3 0 1 1
Processes(maximumresources)
P1’srequestA B C
2 1 1
P3cannotterminate
Resourcerequestisdeniedordelayedbecauseitdoesnotleadtoasafestate
DeadlockDetec=onandRecovery
• Detec=onandRecovery– Ifwedon’thavedeadlockpreven=onoravoidance,thendeadlockmayoccur
– Inthiscase,weneedtodetectdeadlockandrecover• Todothis,weneedtwoalgorithms
– Onetodeterminewhetheradeadlockhasoccurred– Anothertorecoverfromthedeadlock
• Possible,butexpensive(=meconsuming)– ImplementedinVMS– Rundetec=onalgorithmifresourcerequest=mesout
41
DeadlockDetec=on
• Detec=on– Traversetheresourcegraphlookingforcycles– Ifacycleisfound,needtotryanddeterminewhichresourceorprocesstopreempt
• Expensive– Manyprocessesandresourcestotraverse
• Onlyinvokedetec=onalgorithmdependingon– Howo[enorlikelydeadlockis– Howmanyprocessesarelikelytobeaffectedwhenitoccurs
• Thereisatradeoffbetweenoverheadfromrunningalgorithmfrequently,andbeingabletoiden=fyoffendingprocess
42
DeadlockRecovery
Onceadeadlockisdetected,wehavetwoop=ons…• Abortprocesses
– Abortalldeadlockedprocesses• Processesneedtostartoveragain
– Abortoneprocessata=meun=lcycleiseliminated• Systemneedstorerundetec=ona[ereachabort
• Preemptresources(forcetheirrelease)– Needtoselectprocessandresourcetopreempt– Needtorollbackprocesstopreviousstate– Needtopreventstarva=on
43
DeadlockSummary
• Deadlockoccurswhenprocessesarewai=ngoneachotherandcannotmakeprogress– CyclesinResourceAlloca=onGraph(RAG)
• Deadlockrequiresfourcondi=ons:– Mutualexclusion,Holdandwait,Noresourcepreemp=on,circularwait
• Fourapproachestodealingwithdeadlock:– Ignoreit:risky– Preven=on:makeoneofthefourcondi=onsimpossible– Avoidance:banker’salgorithm(controlalloca=on)– Detec=onandRecovery:lookforacycle,preemptorabort
44