Escolar Documentos
Profissional Documentos
Cultura Documentos
Certificate Program in Software Development CSE-TC and CSIM, AIT September -- November, 2003
6. Deadlock
(Ch. 7, S&G)
Objectives
Contents
1. What is Deadlock? 2. Dealing with Deadlock 3. Deadlock Prevention
4. Deadlock Avoidance
5. Deadlock Detection
6. Deadlock Recovery
OSes: 6. Deadlock 2
1. What is Deadlock?
An
When two trains approach each other at a crossing, both shall come to a full stop and neither shall start up again until the other has gone.
OSes: 6. Deadlock
In Picture Form:
VUW CS 305
OSes: 6. Deadlock
process must request a resource before using it, and must release the resource after finishing with it.
set of processes is in a deadlock state when every process in the set is waiting for a resource that can only be released by another process in the set.
5
OSes: 6. Deadlock
Exclusion
and Wait
OSes: 6. Deadlock
continued
No
Preemption
Wait
{P0, P1, , Pn} Pi is waiting for the resource held by Pi+1; Pn is waiting for the resource held by P0
OSes: 6. Deadlock
Pj
set of resource types {R1, R2, }, together with instances of those types.
Rk
OSes: 6. Deadlock
Edge Notation
Pi
Rj
Pi
Example Graph
R1 R3
P1
P2 R4
P3
R2
OSes: 6. Deadlock
10
Finding a Deadlock
If
the graph has no cycles then there are no deadlock processes. there is a cycle, then there may be a deadlock.
If
OSes: 6. Deadlock
11
P1
P2
P3 R4
R2
OSes: 6. Deadlock
12
R1
P2
P1
P3
R2
P4
OSes: 6. Deadlock 13
deadlock prevention
disallow
deadlock avoidance
see
OSes: 6. Deadlock
continued
14
Deadlock Ignore
the problem
done by most OSes, including UNIX cheap solution infrequent, manual reboots may be acceptable
OSes: 6. Deadlock
15
3. Deadlock Prevention
Eliminate
mutual exclusion hold and wait no preemption (i.e. have preemption) circular wait
OSes: 6. Deadlock
16
But
OSes: 6. Deadlock
17
approach requires that each process be allocated all of its resources before it begins executing
eliminates the wait possibility
Alternatively,
OSes: 6. Deadlock
18
a process automatically release its current resources if it cannot obtain all the ones it wants
restart the process when it can obtain everything
Alternatively,
OSes: 6. Deadlock
19
a total ordering on all the resource types, and force each process to request resources in increasing order.
approach: require a process to release larger numbered resources when it obtains a smaller numbered resource.
Another
OSes: 6. Deadlock
20
4. Deadlock Avoidance
In
deadlock avoidance, the necessary conditions are untouched. extra information about resources is used by the OS to do better forward planning of process/resource allocation
indirectly avoids circular wait
Instead,
OSes: 6. Deadlock
21
OS is in a safe state if there is a safe sequence of process executions <P1, P2, , Pn>.
In
a safe sequence, each Pi can satisfy its resource requests by using the currently available resources and (if necessary) the resources held by Pj (j < i)
only when Pj has finished
22
OSes: 6. Deadlock
Deadlock Unsafe
An
Safe Deadlock
OSes: 6. Deadlock
23
Example 1
Max
p.218
P0 P1 P2
Currently,
The
OSes: 6. Deadlock
Example 2
Same
as last slide, but P2 now has 3 tape drives allocated currently. Max needs
P0 P1 P2 10 4 9
Current Allocation
5 2 3
The
OSes: 6. Deadlock
OS is in an unsafe state.
25
Add
OSes: 6. Deadlock
26
When
the resource is actually requested, the claim edge is changed to a request edge.
an assignment is released, the assignment edge is changed back to a claim edge.
continued
When
OSes: 6. Deadlock
27
All
resources must be claimed before system start-up. unsafe state is caused by a cycle in the resource allocation graph.
An
OSes: 6. Deadlock
28
Example
R1 P1 R2 P2
OSes: 6. Deadlock
29
that:
a resource can have multiple instances the OS has N processes, M resource types
Initially,
each process must declare the maximum no. of resources it will need. a safe sequence if possible.
30
Calculate
OSes: 6. Deadlock
Available[M]
no. of available resource instances for each resource type e.g. Available[j] == k means K Rjs
Max[N][M]
continued
31
Work[M]
no. of resource instances available for work (by all processes) e.g. Work[j] == k means K Rjs are available
Finish[N]
continued
32
Allocation[N][M]
no. of resource instances allocated to each process e.g. Allocation[i][j] == k means Pi currently has k Rjs
Need[N][M]
no. of resource instances still needed by each process e.g. Need[i][j] == k means Pi still needs k Rjs
Need[i][j] == Max[i][j] - Allocation[i][j]
OSes: 6. Deadlock
continued
33
Request[N][M]
no. of resource instances currently requested by each process e.g. Request[i][j] == k means Pi has requested k Rjs
OSes: 6. Deadlock
34
Vectors
Allocation[i]
Need[i]
Request[i]
Vector Copy: Work := Available; Finish := false Find i such that Pi hasnt finished but could: Finish[i] == false Need[i] <= Work If no suitable i, go to step 4. Assume Pi completes: Work := Work + Allocation[i] Finish[i] := true go to step 2 If for all i Finish[i] == true then Safe-State
36
OSes: 6. Deadlock
Safety Example
Resource
Type
Instances
10 5 7
A B C
OSes: 6. Deadlock
continued
37
Allocation
P0 P1 P2 P3 P4
Max
A 7 3 9 2 4 B 5 2 0 2 3 C 3 2 2 2 3
Available
A B C 3 3 2
Need
A 7 1 6 0 4 B 4 2 0 1 3 C 3 2 0 1 1
A 0 2 3 2 0
B 1 0 0 1 0
C 0 0 2 1 2
The OS is in a safe state since <P1, P3, P4, P2, P0> is a safe sequence.
38
OSes: 6. Deadlock
If (Need[i] < Request[i]) then max-error While (Available < Request[i]) do wait Construct a new state by:
Available = Available - Request[i] Allocation[i] = Allocation[i] + Request [i] Need[i] = Need[i] - Request [i]
OSes: 6. Deadlock
Request Example 1
At
Does
OSes: 6. Deadlock
40
Allocation
P0 P1 P2 P3 P4
Max
A 7 3 9 2 4 B 5 2 0 2 3 C 3 2 2 2 3
Available
A B C 2 3 0
Need
A 7 0 6 0 4 B 4 2 0 1 3 C 3 0 0 1 3
A 0 3 3 2 0
B 1 0 0 1 0
C 0 2 2 1 2
The OS is in a safe state since <P1, P3, P4, P0, P2> is a safe sequence.
41
OSes: 6. Deadlock
OSes: 6. Deadlock
42
5. Deadlock Detection
If
there are no prevention or avoidance mechanisms in place, then deadlock may occur. detection should return enough information so the OS can recover. often should the detection algorithm be executed?
43
Deadlock
How
OSes: 6. Deadlock
that each resource has only one instance. a wait-for graph by removing the resource types nodes from a resource allocation graph.
Create
Deadlock
OSes: 6. Deadlock
Example
P5
R1
R3
R4
P5
P1
P2 P4
P3
P1
P2 P4
P3
R2
R5
OSes: 6. Deadlock
45
a resource type can have multiple instances, then an algorithm very similar to the bankers algorithm can be used.
algorithm investigates every possible allocation sequence for the processes that remain to be completed.
The
OSes: 6. Deadlock
46
Detection Algorithm
1
Vector Copy: Work := Available; Finish := false Find i such that Pi hasnt finished but could: Finish[i] == false Request[i] <= Work If no suitable i, go to step 4. Assume Pi completes: Work := Work + Allocation[i] Finish[i] := true go to step 2 If Finish[i] == false then Pi is deadlocked
47
OSes: 6. Deadlock
Example 1
Resource
Type
Instances
7 2 6
A B C
OSes: 6. Deadlock
continued
48
Allocation
P0 P1 P2 P3 P4
Request
A 0 2 0 1 0 B 0 0 0 0 0 C 0 2 0 0 2
Available
A B C 0 0 0
A 0 2 3 2 0
B 1 0 0 1 0
C 0 0 3 1 2
The OS is not in a deadlocked state since <P0, P2, P3, P1, P4> is a safe sequence.
49
OSes: 6. Deadlock
Example 2
Change A 0 2 3 2 0 B 1 0 0 1 0
P2 to request 1 C instance
Request
A 0 2 0 1 0 B 0 0 0 0 0 C 0 2 1 0 2
Allocation
P0 P1 P2 P3 P4
OSes: 6. Deadlock
Available
A B C 0 0 0
C 0 0 3 1 2
The OS is deadlocked.
50
6. Deadlock Recovery
Tell
the operator
recovery:
System-based
OSes: 6. Deadlock
51
Ther e are two methods for breaking a deadlock. One solution is simply to abort one by one process to break the circular wait. The second option is to preempt some resources from one or more of the deadlocked processes. 1 Process termination Process termination is the one method to recover from deadlock. We use the one of two methods for process termination .These are 1.Abort all deadlocked processes----It means the all processes in the deadlocked state ,and start the allocation from the starting point. It is a great expensive method. 2.Abort one by one process until the deadlock cycle is eliminated.----In this method first abort the one of the processes in the deadlocked state, and allocated the resources to some other process in the deadlock state then check whether the deadlock is broken or not. If not abort the another process from the deadlock state. Continue this process until we recover from deadlock. This method is also expensive.
OSes: 6. Deadlock 52
2.Resource Preemption
There are three methods to eliminate deadlocks using resource preemption. These are 1.Selecting a victim----------Select a victim from the deadlock state, and preempt that one. 2.Rollback------------Rollback the process and resources up to some safe state, and restart it from that state. This method requires the system to keep more information about the state of all the running processes. 3.StarvationA process or resource can be picked as victim only a finite number of times,it is said to be starvation
OSes: 6. Deadlock
53
OSes: 6. Deadlock
54
how to select a resource (e.g. by using minimum cost) how to rollback the process which has just lost its resources