Upload
shannon-sparks
View
223
Download
1
Tags:
Embed Size (px)
Citation preview
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
Circular WaitCircular Wait– {P{P00, P, P11, …, P, …, Pnn}}
– PPii is waiting for the resource held by P is waiting for the resource held by P i+1i+1;;
PPnn is waiting for the resource held by P is waiting for the resource held by P00
OSes: 6. Deadlock 8
1.3. Resource Allocation Graph1.3. Resource Allocation Graph
A set of processes {PA set of processes {P00, P, P11, …}, …}
A set of resource types {RA set of resource types {R11, R, R22, …}, , …},
together with instances of those types.together with instances of those types.
Pj
Rk
OSes: 6. Deadlock 9
Edge NotationEdge Notation
PPii R Rjj
– process i has requested an instance of resource jprocess i has requested an instance of resource j– called a called a request edgerequest edge
RRjj P Pii
– an instance of resource j has been assigned to an instance of resource j has been assigned to process iprocess i
– called an called an assignment edgeassignment edge
OSes: 6. Deadlock 10
Example GraphExample Graph Fig. 7.1, p.211
R1
P1 P2 P3
R3
R2
R4
OSes: 6. Deadlock 11
Finding a DeadlockFinding a Deadlock
If the graph has no cycles then there are no If the graph has no cycles then there are no deadlock processes.deadlock processes.
If there is a cycle, then there If there is a cycle, then there maymay be a be a deadlock.deadlock.
OSes: 6. Deadlock 12
Graph with a DeadlockGraph with a Deadlock Fig. 7.2, p.212
R1
P1 P2 P3
R3
R2
R4
OSes: 6. Deadlock 13
Graph without a DeadlockGraph without a DeadlockFig. 7.3, p.213
R1
P1
P2
P3
R2
P4
OSes: 6. Deadlock 14
2. Deadling with Deadlocks2. Deadling with Deadlocks
Stop a deadlock ever occuringStop a deadlock ever occuring– deadlock preventiondeadlock prevention
disallow at least one of the necessary conditionsdisallow at least one of the necessary conditions
– deadlock avoidancedeadlock avoidance see a deadlock coming and alter the see a deadlock coming and alter the
process/resource allocation strategyprocess/resource allocation strategy
continued
OSes: 6. Deadlock 15
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
OSes: 6. Deadlock 17
3.1. Eliminate Mutual Exclusion3.1. Eliminate Mutual Exclusion
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
OSes: 6. Deadlock 19
3.3. Eliminate “No Preemption”3.3. Eliminate “No Preemption”
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
OSes: 6. Deadlock 20
3.4. Eliminate Circular Wait3.4. Eliminate Circular Wait
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
drives allocated currently.drives allocated currently.
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
PP00 0 1 00 1 0 7 5 37 5 3 3 3 23 3 2 7 4 37 4 3PP11 2 0 02 0 0 3 2 23 2 2 1 2 21 2 2PP22 3 0 23 0 2 9 0 29 0 2 6 0 06 0 0PP33 2 1 12 1 1 2 2 22 2 2 0 1 10 1 1PP44 0 0 20 0 2 4 3 34 3 3 4 3 14 3 1
The OS is in a The OS is in a safe statesafe state since since<P<P11, P, P33, P, P44, P, P22, P, P00> is a safe sequence.> is a safe sequence.
OSes: 6. Deadlock 39
Request Resource AlgorithmRequest Resource Algorithm
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:
AvailableAvailable = = AvailableAvailable - - RequestRequest[[ii]]AllocationAllocation[[ii] = ] = AllocationAllocation[[ii] + ] + Request Request [[ii]] NeedNeed[[ii] = ] = NeedNeed[[ii] - ] - Request Request [[ii]]
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
PP00 0 1 00 1 0 7 5 37 5 3 2 3 02 3 0 7 4 37 4 3PP11 3 0 23 0 2 3 2 23 2 2 0 2 00 2 0PP22 3 0 23 0 2 9 0 29 0 2 6 0 06 0 0PP33 2 1 12 1 1 2 2 22 2 2 0 1 10 1 1PP44 0 0 20 0 2 4 3 34 3 3 4 3 34 3 3
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.
OSes: 6. Deadlock 45
ExampleExample Fig. 7.7, p.225
R1
P2
R3 R4
R2 R5
P1 P3
P5
P4
P2P1 P3
P5
P4
OSes: 6. Deadlock 46
5.2. Banker’s Algorithm Variation5.2. Banker’s Algorithm Variation
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
PP00 0 1 00 1 0 0 0 00 0 0 0 0 00 0 0PP11 2 0 02 0 0 2 0 22 0 2PP22 3 0 33 0 3 0 0 00 0 0PP33 2 1 12 1 1 1 0 01 0 0PP44 0 0 20 0 2 0 0 20 0 2
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
PP00 0 1 00 1 0 0 0 00 0 0 0 0 00 0 0PP11 2 0 02 0 0 2 0 22 0 2PP22 3 0 33 0 3 0 0 0 0 11PP33 2 1 12 1 1 1 0 01 0 0PP44 0 0 20 0 2 0 0 20 0 2
The OS The OS isis deadlocked. deadlocked.
OSes: 6. Deadlock 51
6. Deadlock Recovery6. Deadlock Recovery
Tell the operatorTell the operator
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