DeadlockCharacterization
Deadlockcanariseif fourconditionshold
simultaneously.
●Mutualexclusion:onlyoneprocessatatimecanusea
resource
●Holdandwait:aprocessholdingatleastoneresourceis
waiting to acquire additional resources held by other
processes
●Nopreemption:a resource can be released only
voluntarilybytheprocessholdingitafterthatprocesshas
completeditstask
●Circularwait:there exists a set {P0, P1, …, P0} of waiting
processessuchthatP0iswaitingforaresourcethatisheld
byP1,P1iswaitingforaresourcethatisheldby
P2,…,Pn–1iswaitingforaresourcethatisheldby
Pn,andPniswaitingforaresourcethatisheldbyP0
DeadlockPrevention
Topreventdeadlock,wecanrestrainthewaysthatarequestcanbe
made
●MutualExclusion–Themutual-exclusionconditionmust
holdfornon-sharableresources
●Hold and Wait –we must guarantee that whenever a
processrequestsaresource,it doesnotholdanyother
resources
●Require a process to request and be allocated all its
resourcesbeforeitbeginsexecution,orallowaprocessto
requestresourcesonlywhentheprocesshasnone
●Result:Lowresourceutilization;starvationpossible
DeadlockPrevention(Cont.)
●NoPreemption–
●Ifaprocessthatisholdingsomeresourcesrequestsanother
resource that cannot be immediately allocated to it, then all
resourcescurrentlybeingheldarereleased
●Preemptedresourcesareaddedtothelistofresourcesfor
whichtheprocessiswaiting
●Aprocesswillberestartedonlywhenitcanregainitsold
resources,aswellasthenewonesthatitisrequesting
●Circular Wait –impose a total orderingof all resource types, and
requirethateachprocessrequestsresourcesinanincreasingorder
ofenumeration.Forexample:
F(tapedrive)=1
F(diskdrive)=5
F(printer)=12
DeadlockAvoidance
Requiresthatthesystemhassomeadditionalapriori
information
available.
●Simplest and most useful model requires that each process
declarethemaximumnumberofresourcesofeachtypethatit
mayneed
●Thedeadlock-avoidancealgorithmdynamicallyexaminesthe
resource-allocation state to ensure that there can neverbe a
circular-waitcondition
●Aresource-allocationstateisdefinedbythenumberofavailable
and allocated resources, and the maximum demands of the
processes
apriori:formedorconceived
beforehand
SafeState
●When a process requests an available resource, the system
mustdecideifimmediateallocationleavesthesysteminasafe
state
●Asystemisinasafestateonlyifthereexistsasafesequence
●Asequenceofprocesses<P1,P2,…,Pn>isasafesequencefor
the current allocation state if, for each Pi, the resource requests
that Pi can still make, can be satisfied by currently available
resourcesplusresourcesheldbyallPj,withj<i.
●Thatis:
●IftheP
iresourceneedsarenotimmediatelyavailable,thenP
ican
waituntilallP
jhavefinished
●WhenP
jisfinished,P
icanobtainneededresources,execute,return
allocatedresources,andterminate
●WhenP
iterminates, P
i+1canobtainitsneededresources,andsoon
Resource-Allocation GraphScheme
●Introduceanewkindofedgecalledaclaimedge
●ClaimedgeP
i R
jindicates that process Pjmay
requestresourceR
j;whichisrepresentedbyadashedline
●Aclaimedgeconvertstoarequestedgewhenaprocess
requestsaresource
●Arequestedgeconvertstoanassignmentedgewhenthe
resourceisallocatedtotheprocess
●Whenaresourceisreleasedbyaprocess,anassignment
edgereconvertstoaclaimedge
●Resourcesmustbeclaimedaprioriinthesystem
Resource-Allocation Graphwith
ClaimEdges
Requ
e st
edge
Assignm
e nt
edge
Clai
m
edg
e
Clai
m
edge
UnsafeStateInResource-AllocationGraph
Assignm
e nt
edge
Requ
e st
edge
Assignm
e nt
edge
Clai
m
edg
e
Resource-AllocationGraphAlgorithm
●SupposethatprocessP
irequestsaresourceR
j
●The request can be granted only if converting the
requestedgetoanassignmentedgedoesnotresult
in the formation of a cyclein the resource allocation
graph
DataStructuresfortheBanker’sAlgorithm
Letn=numberofprocesses,andm=numberofresources
types.
●Available:Vector of length m. If available [j] =k, there are k instances
ofresourcetypeRjavailable.
●Max:nxmmatrix.IfMax[i,j]=k,thenprocessPimayrequestat
mostkinstancesofresourcetypeRj.
●Allocation:nxmmatrix.IfAllocation[i,j]=kthenPiiscurrently
allocatedkinstancesofRj.
●Need:nxmmatrix.IfNeed[i,j]=k,thenPimayneedkmore
instancesofRjtocompleteitstask.
Need[i,j]=Max[i,j]–Allocation[i,j]
SingleInstanceofEachResourceType
●Requiresthecreationandmaintenanceofawait-forgraph
●Consistsofavariantoftheresource-allocationgraph
●Thegraphisobtainedbyremovingtheresourcenodesfrom
a resource-allocation graph and collapsing the appropriate
edges
●Consequently;allnodesareprocesses
●P
i→P
jif P
iiswaitingforP
j.
●Periodicallyinvokeanalgorithmthatsearchesforacycle
inthegraph
●Ifthereisacycle,thereexistsadeadlock
●Analgorithmtodetectacycleinagraphrequiresanorderof
n
2
operations,wherenisthenumberofverticesinthegraph
Detection-AlgorithmUsage
●When,andhowoften,toinvokethedetectionalgorithmdependson:
●Howoftenisadeadlocklikelytooccur?
●Howmanyprocesseswillbeaffectedbydeadlockwhenithappens?
●If the detection algorithm is invoked arbitrarily, there may be many
cyclesintheresourcegraphandsowewouldnotbeabletotellwhich
oneofthemanydeadlockedprocesses“caused”thedeadlock
●Ifthedetectionalgorithmisinvokedforeveryresourcerequest,suchan
actionwillincuraconsiderableoverheadincomputationtime
●AlessexpensivealternativeistoinvokethealgorithmwhenCPU
utilizationdropsbelow40%,forexample
●Thisisbasedontheobservationthatadeadlockeventuallycripplessystem
throughputandcausesCPUutilizationtodrop
Recovery fromDeadlock:ResourcePreemption
●With this approach, we successively preempt some resources
fromprocessesandgivetheseresourcestootherprocessesuntil
thedeadlockcycleisbroken
●Whenpreemptionisrequiredtodealwithdeadlocks,thenthree
issuesneedtobeaddressed:
●Selectingavictim–Whichresourcesandwhichprocessesaretobe
preempted?
●Rollback–Ifwepreemptaresourcefromaprocess,whatshouldbe
donewiththatprocess?
●Starvation–Howdoweensurethatstarvationwillnotoccur?Thatis,
how can we guarantee that resources will not always be preempted
fromthesameprocess?
Summary
●Fournecessaryconditionsmustholdinthesystemforadeadlock
tooccur
●Mutualexclusion
●Holdandwait
●Nopreemption
●Circularwait
●Fourprincipalmethodsfordealingwithdeadlocks
●Usesomeprotocolto(1)preventor(2)avoiddeadlocks,ensuring
thatthesystemwillneverenteradeadlockstate
●Allowthesystemtoenteradeadlockstate,(3)detectit,andthen
recover
Recoverbyprocessterminationorresourcepreemption
●(4) Do nothing; ignore the problem altogether and pretend that
deadlocksneveroccurinthesystem(usedbyWindowsandUnix)
●Topreventdeadlocks,wecanensurethatatleastoneofthefour
necessaryconditionsneverholds