OSes: 6. Deadlock Operating Systems Operating Systems Objectives Objectives – describe deadlock, and forms of describe deadlock, and forms of prevention, avoidance, detection, prevention, avoidance, detection, and recovery and recovery Certificate Program in Software D evelopment CSE-TC and CSIM, AIT September -- November, 2003 6. Deadlock (Ch. 7, S&G) ch 8 in the 6th ed.
53
Embed
OSes: 6. Deadlock 1 Operating Systems v Objectives –describe deadlock, and forms of prevention, avoidance, detection, and recovery Certificate Program.
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
OSes: 6. Deadlock 1
Operating SystemsOperating Systems
ObjectivesObjectives– describe deadlock, and forms of prevention, describe deadlock, and forms of prevention,
avoidance, detection, and recoveryavoidance, detection, and recovery
Certificate Program in Software DevelopmentCSE-TC and CSIM, AITSeptember -- November, 2003
6. Deadlock(Ch. 7, S&G)
ch 8 in the 6th ed.
OSes: 6. Deadlock 2
ContentsContents
1.1. What is Deadlock?What is Deadlock?
2.2. Dealing with DeadlockDealing with Deadlock
3.3. Deadlock PreventionDeadlock Prevention
4.4. Deadlock AvoidanceDeadlock Avoidance
5.5. Deadlock DetectionDeadlock Detection
6.6. Deadlock RecoveryDeadlock Recovery
OSes: 6. Deadlock 3
1. What is Deadlock?1. What is Deadlock?
An example from US Kansas law:An example from US Kansas law:– ““When two trains approach each other at a When two trains approach each other at a
crossing, both shall come to a full stop and crossing, both shall come to a full stop and neither shall start up again until the other has neither shall start up again until the other has gone.”gone.”
OSes: 6. Deadlock 4
In Picture Form:In Picture Form: VUW CS 305
Neither truck can proceed.
OSes: 6. Deadlock 5
1.1. System Deadlock1.1. System Deadlock
A process must request a resource before A process must request a resource before using it, and must release the resource after using it, and must release the resource after finishing with it.finishing with it.
A set of processes is in a A set of processes is in a deadlock statedeadlock state when every process in the set is waiting for when every process in the set is waiting for a resource that can only be released by a resource that can only be released by another process in the set.another process in the set.
OSes: 6. Deadlock 6
1.2. Necessary Conditions for Deadlock1.2. Necessary Conditions for Deadlock
Mutual ExclusionMutual Exclusion– at least one resource must be held in at least one resource must be held in
non-shareable modenon-shareable mode
Hold and WaitHold and Wait– a process is holding a resource and waiting a process is holding a resource and waiting
for othersfor others
continued
OSes: 6. Deadlock 7
No PreemptionNo Preemption– only the process can release its held resourceonly the process can release its held resource
Deadlock detection and recoveryDeadlock detection and recovery
Ignore the problemIgnore the problem– done by most OSes, including UNIXdone by most OSes, including UNIX– cheap solutioncheap solution– infrequent, manual reboots may be acceptableinfrequent, manual reboots may be acceptable
OSes: 6. Deadlock 16
3. Deadlock Prevention3. Deadlock Prevention
Eliminate one (or more) of:Eliminate one (or more) of:– mutual exclusionmutual exclusion– hold and waithold and wait– no preemption (i.e. no preemption (i.e. havehave preemption) preemption)– circular waitcircular wait
Shared resources do not require mutual Shared resources do not require mutual exclusionexclusion– e.g. read-only filese.g. read-only files
But some resources cannot be shared (at the But some resources cannot be shared (at the same time)same time)– e.g. printerse.g. printers
OSes: 6. Deadlock 18
3.2. Eliminate Hold & Wait3.2. Eliminate Hold & Wait
One approach requires that each process be One approach requires that each process be allocated allocated allall of its resources before it begins of its resources before it begins executingexecuting– eliminates the wait possibilityeliminates the wait possibility
Alternatively, only allow a process to Alternatively, only allow a process to request resources when it currently has nonerequest resources when it currently has none– eliminates the hold possibilityeliminates the hold possibility
Make a process automatically release its Make a process automatically release its current resources if it cannot obtain all the current resources if it cannot obtain all the ones it wantsones it wants– restart the process when it can obtain everythingrestart the process when it can obtain everything
Alternatively, the desired resources can be Alternatively, the desired resources can be preempted from other waiting processespreempted from other waiting processes
Impose a total ordering on all the resource Impose a total ordering on all the resource types, and force each process to request types, and force each process to request resources in increasing order.resources in increasing order.
Another approachAnother approach: require a process to : require a process to release larger numbered resources when it release larger numbered resources when it obtains a smaller numbered resource.obtains a smaller numbered resource.
OSes: 6. Deadlock 21
4. Deadlock Avoidance4. Deadlock Avoidance
In deadlock avoidance, the necessary In deadlock avoidance, the necessary conditions are untouched. conditions are untouched.
Instead, extra information about resources Instead, extra information about resources is used by the OS to do better forward is used by the OS to do better forward planning of process/resource allocationplanning of process/resource allocation– indirectly avoids circular wait indirectly avoids circular wait
OSes: 6. Deadlock 22
4.1. Safe States4.1. Safe States
An OS is in a An OS is in a safe statesafe state if there is a if there is a safe sequencesafe sequence of process executions of process executions <P<P11, P, P22, …, P, …, Pnn>.>.
In a safe sequence, each PIn a safe sequence, each Pii can satisfy its can satisfy its
resource requests by using the currently resource requests by using the currently available resources available resources andand (if necessary) the (if necessary) the resources held by Presources held by Pjj (j < i) (j < i)
– only when Ponly when Pjj has finished has finished
OSes: 6. Deadlock 23
Safe State ImplicationsSafe State Implications
A safe state cannot lead to deadlock.A safe state cannot lead to deadlock.
An unsafe state An unsafe state maymay lead to deadlock. lead to deadlock.
Deadlock is avoided by always Deadlock is avoided by always keeping the system in a safe statekeeping the system in a safe state– this may reduce resource utilizationthis may reduce resource utilization
Deadlock
Unsafe
Safe
Fig. 7.4, p.218
OSes: 6. Deadlock 24
Example 1Example 1 Max no. of resources: 12 tape drivesMax no. of resources: 12 tape drives Max needsMax needs Current AllocationCurrent AllocationPP00 1010 55PP11 4 4 22PP22 9 9 22
Currently, there are 3 free tape drivesCurrently, there are 3 free tape drives
The OS is in a The OS is in a safesafe state, since <P state, since <P11, P, P00, P, P22> >
is a safe sequence.is a safe sequence.
p.218
OSes: 6. Deadlock 25
Example 2Example 2
Same as last slide, but PSame as last slide, but P22 now has 3 tape now has 3 tape
Max needsMax needs Current AllocationCurrent AllocationPP00 1010 55PP11 4 4 22PP22 9 9 33
The OS is in an The OS is in an unsafeunsafe state. state.
OSes: 6. Deadlock 26
4.2. Using Resource Allocation Graphs4.2. Using Resource Allocation Graphs
Assume a resource type only has one Assume a resource type only has one instance.instance.
Add a Add a claim edgeclaim edge::– PPii R Rjj
– process Pprocess Pii may request a resource R may request a resource Rjj in the future in the future
– drawn as a dashed linedrawn as a dashed line
continued
OSes: 6. Deadlock 27
When the resource is actually requested, When the resource is actually requested, the claim edge is changed to a request the claim edge is changed to a request edge.edge.
When an assignment is released, the When an assignment is released, the assignment edge is changed back to a assignment edge is changed back to a claim edge.claim edge.
continued
OSes: 6. Deadlock 28
All resources must be claimed before All resources must be claimed before system start-up.system start-up.
An unsafe state is caused by a cycle in the An unsafe state is caused by a cycle in the resource allocation graph.resource allocation graph.
OSes: 6. Deadlock 29
ExampleExample Figs 7.5, 7.6, p.220-221
R1
R2
P2P1
R1
R2
P2P1
R2 allocation to P2
creates an unsafe state
OSes: 6. Deadlock 30
4.3. Banker’s Algorithm4.3. Banker’s Algorithm
Assume that:Assume that:– a resource can have multiple instancesa resource can have multiple instances– the OS has N processes, M resource typesthe OS has N processes, M resource types
Initially, each process must declare the Initially, each process must declare the maximum no. of resources it will need.maximum no. of resources it will need.
Calculate a safe sequence if possible.Calculate a safe sequence if possible.
OSes: 6. Deadlock 31
Banker Data StructuresBanker Data Structures
Available[M]Available[M]
– no. of available resource instances for each no. of available resource instances for each resource typeresource type
– e.g. e.g. Available[j] == kAvailable[j] == k means K R means K Rjj’s’s
Max[N][M]Max[N][M]
– max demand of each processmax demand of each process
– e.g. e.g. max[i][j] == kmax[i][j] == k means P means Pii wants k R wants k Rjj’s’s
continued
OSes: 6. Deadlock 32
Work[M]Work[M]
– no. of resource instances available for work no. of resource instances available for work (by all processes)(by all processes)
– e.g. e.g. Work[j] == kWork[j] == k means K R means K Rjj’s are ’s are
availableavailable
Finish[N]Finish[N]
– record of finished processes record of finished processes
– e.g. Pe.g. Pii is finished if is finished if Finish[i] == trueFinish[i] == true
continued
OSes: 6. Deadlock 33
Allocation[N][M]Allocation[N][M]
– no. of resource instances allocated to each processno. of resource instances allocated to each process– e.g. e.g. Allocation[i][j] == kAllocation[i][j] == k
means Pmeans Pii currently has k R currently has k Rjj’s ’s
Need[N][M]Need[N][M]
– no. of resource instances still needed by each processno. of resource instances still needed by each process– e.g. e.g. Need[i][j] == kNeed[i][j] == k
means Pmeans Pii still needs k R still needs k Rjj’s ’s – Need[i][j] == Max[i][j] - Allocation[i][j]Need[i][j] == Max[i][j] - Allocation[i][j]
continued
OSes: 6. Deadlock 34
Request[N][M]Request[N][M]
– no. of resource instances currently requested by no. of resource instances currently requested by each processeach process
– e.g. e.g. Request[i][j] == kRequest[i][j] == k means Pmeans Pii has requested k R has requested k Rjj’s ’s
OSes: 6. Deadlock 35
VectorsVectors
Allocation[i]Allocation[i]
– resources currently allocated to Presources currently allocated to P ii
Need[i]Need[i]
– resources still needed by Presources still needed by Pii
Request[i]Request[i]
– resources currently requested by Presources currently requested by P ii
shorthand forreferring to the2D data structures
OSes: 6. Deadlock 36
The Safety AlgorithmThe Safety Algorithm
1 Vector Copy:Vector Copy: Work Work := := AvailableAvailable; ; FinishFinish := := falsefalse2 Find i such that PFind i such that Pii hasn’t finished but could: hasn’t finished but could:
FinishFinish[[ii] == ] == falsefalse NeedNeed[[ii] <= ] <= WorkWorkIf no suitable i, go to step 4.If no suitable i, go to step 4.
3 Assume PAssume Pii completes: completes:
WorkWork := := WorkWork + + AllocationAllocation[[ii]]FinishFinish[[ii] := ] := truetruego to step 2go to step 2
4 If If for all i Finishfor all i Finish[[ii] == ] == truetrue then then Safe-StateSafe-State
VUW CS 305;p221-222
OSes: 6. Deadlock 37
Safety ExampleSafety Example
Resource TypeResource Type InstancesInstancesAA 1010BB 55CC 77
p.222
continued
OSes: 6. Deadlock 38
AllocationAllocation MaxMax AvailableAvailable NeedNeedA B CA B C A B CA B C A B CA B C A B CA B C
1 If (Need[If (Need[ii] < Request[i]) then ] < Request[i]) then max-errormax-error2 While (Available < Request[i]) do While (Available < Request[i]) do waitwait3 Construct a new state by:Construct a new state by:
4 If (If (new state is not safenew state is not safe) then ) then restorerestore and and waitwait
VUW CS 305;p.222
OSes: 6. Deadlock 40
Request Example 1Request Example 1
At some time, PAt some time, P11 requests an additional requests an additional
1 A instance and 2 C instances1 A instance and 2 C instances– i.e.i.e. Request[1] == (1, 0, 2)Request[1] == (1, 0, 2)
Does this lead to a safe state?Does this lead to a safe state?– Available >= Request[1]Available >= Request[1] so continue so continue– generate new state and test for safetygenerate new state and test for safety
OSes: 6. Deadlock 41
AllocationAllocation MaxMax AvailableAvailable NeedNeedA B CA B C A B CA B C A B CA B C A B CA B C
The OS is in a The OS is in a safe statesafe state since since<P<P11, P, P33, P, P44, P, P00, P, P22> is a safe sequence.> is a safe sequence.
OSes: 6. Deadlock 42
Further Request ExamplesFurther Request Examples
From this state, PFrom this state, P44 requests a further (3,3,0) requests a further (3,3,0)
– cannot be granted, since insufficient resourcescannot be granted, since insufficient resources
Alternatively, PAlternatively, P00 requests a further (0,2,0) requests a further (0,2,0)
– should not be granted since the resulting state is should not be granted since the resulting state is unsafeunsafe
OSes: 6. Deadlock 43
5. Deadlock Detection5. Deadlock Detection
If there are no prevention or avoidance If there are no prevention or avoidance mechanisms in place, then deadlock may occur.mechanisms in place, then deadlock may occur.
Deadlock detection should return enough Deadlock detection should return enough information so the OS can recover.information so the OS can recover.
How often should the detection algorithm be How often should the detection algorithm be executed?executed?
OSes: 6. Deadlock 44
5.1. Wait-for Graph5.1. Wait-for Graph
Assume that each resource has only one Assume that each resource has only one instance.instance.
Create a wait-for graph by removing the Create a wait-for graph by removing the resource types nodes from a resource resource types nodes from a resource allocation graph.allocation graph.
Deadlock exists if and only if the wait-for Deadlock exists if and only if the wait-for graph contains a cycle.graph contains a cycle.
If a resource type can have multiple If a resource type can have multiple instances, then an algorithm very similar to instances, then an algorithm very similar to the banker’s algorithm can be used.the banker’s algorithm can be used.
The algorithm investigates every possible The algorithm investigates every possible allocation sequence for the processes that allocation sequence for the processes that remain to be completed.remain to be completed.
OSes: 6. Deadlock 47
Detection AlgorithmDetection Algorithm
1 Vector Copy:Vector Copy: Work Work := := AvailableAvailable; ; FinishFinish := := falsefalse2 Find i such that PFind i such that Pii hasn’t finished but could: hasn’t finished but could:
FinishFinish[[ii] == ] == falsefalse Request[i]Request[i] <= <= WorkWorkIf no suitable i, go to step 4.If no suitable i, go to step 4.
3 Assume PAssume Pii completes: completes:
WorkWork := := WorkWork + + AllocationAllocation[[ii]]FinishFinish[[ii] := ] := truetruego to step 2go to step 2
4 If If FinishFinish[[ii] == ] == falsefalse then then Pi is deadlockedPi is deadlocked
VUW CS 305;p.225
OSes: 6. Deadlock 48
Example 1Example 1
Resource TypeResource Type InstancesInstancesAA 77BB 22CC 66
p.226
continued
OSes: 6. Deadlock 49
AllocationAllocation RequestRequest AvailableAvailableA B CA B C A B CA B C A B CA B C
The OS is The OS is notnot in a deadlocked state since in a deadlocked state since<P<P00, P, P22, P, P33, P, P11, P, P44> is a safe sequence.> is a safe sequence.
OSes: 6. Deadlock 50
Example 2Example 2
Change PChange P22 to request 1 C instance to request 1 C instance
AllocationAllocation RequestRequest AvailableAvailableA B CA B C A B CA B C A B CA B C
System-based recovery:System-based recovery:– abort one or more processes in the circular waitabort one or more processes in the circular wait
– preempt resources in one or more deadlocked preempt resources in one or more deadlocked processesprocesses
OSes: 6. Deadlock 52
6.1. Process Termination6.1. Process Termination
Abort all deadlocked processes, orAbort all deadlocked processes, or
Abort one process at a time until the Abort one process at a time until the deadlocked cycle disappearsdeadlocked cycle disappears– not always easy to abort a processnot always easy to abort a process– choice should be based on choice should be based on minimum costminimum cost
OSes: 6. Deadlock 53
6.2. Resource Preemption6.2. Resource Preemption
Issues:Issues:– how to select a resourcehow to select a resource
(e.g. by using minimum cost)(e.g. by using minimum cost)
– how to rollback the process which has just lost how to rollback the process which has just lost its resourcesits resources
– avoiding process starvationavoiding process starvation