Deadlock
Deadlock
6. Deadlock
(Ch. 7, S&G)
ch 8 in the 6th ed.
Objectives
– describe deadlock, and forms of prevention,
avoidance, detection, and recovery
OSes: 6. Deadlock 1
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?
OSes: 6. Deadlock 3
In Picture Form: VUW CS 305
OSes: 6. Deadlock 4
1.1. System Deadlock
Mutual Exclusion
– at least one resource must be held in
non-shareable mode
OSes: 6. Deadlock
continued 6
No Preemption
– only the process can release its held resource
Circular 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 7
1.3. Resource Allocation Graph
Rk
OSes: 6. Deadlock 8
Edge Notation
Pi R j
– process i has requested an instance of resource j
– called a request edge
Rj P i
– an instance of resource j has been assigned to
process i
– called an assignment edge
OSes: 6. Deadlock 9
Example Graph Fig. 7.1, p.211
R1 R3
P1 P2 P3
R4
R2
OSes: 6. Deadlock 10
Finding a Deadlock
OSes: 6. Deadlock 11
Graph with a Deadlock Fig. 7.2, p.212
R1 R3
P1 P2 P3
R4
R2
OSes: 6. Deadlock 12
Graph without a Deadlock
Fig. 7.3, p.213
R1
P2
P1 P3
R2
P4
OSes: 6. Deadlock 13
2. Deadling with Deadlocks
– deadlock avoidance
see a deadlock coming and alter the
process/resource allocation strategy
OSes: 6. Deadlock
continued 14
Deadlock detection and recovery
OSes: 6. Deadlock 15
3. Deadlock Prevention
OSes: 6. Deadlock 16
3.1. Eliminate Mutual Exclusion
OSes: 6. Deadlock 17
3.2. Eliminate Hold & Wait
OSes: 6. Deadlock 18
3.3. Eliminate “No Preemption”
OSes: 6. Deadlock 19
3.4. Eliminate Circular Wait
OSes: 6. Deadlock 20
4. Deadlock Avoidance
OSes: 6. Deadlock 21
4.1. Safe States
An OS is in a safe state if there is a
safe sequence of process executions
<P1, P2, …, Pn>.
OSes: 6. Deadlock 23
Example 1 p.218
OSes: 6. Deadlock 25
4.2. Using Resource Allocation Graphs
OSes: 6. Deadlock
continued 26
When the resource is actually requested,
the claim edge is changed to a request
edge.
OSes: 6. Deadlock
continued 27
All resources must be claimed before
system start-up.
OSes: 6. Deadlock 28
Example Figs 7.5, 7.6, p.220-221
R1 R1
P1 P2 P1 P2
R2 R2
R2 allocation to P2
creates an unsafe state
OSes: 6. Deadlock 29
4.3. Banker’s Algorithm
Assume that:
– a resource can have multiple instances
– the OS has N processes, M resource types
Max[N][M]
– max demand of each process
– e.g. max[i][j] == k means Pi wants k Rj’s
OSes: 6. Deadlock
continued 31
Work[M]
– no. of resource instances available for work
(by all processes)
– e.g. Work[j] == k means K Rj’s are available
Finish[N]
– record of finished processes
– e.g. Pi is finished if Finish[i] == true
OSes: 6. Deadlock
continued 32
Allocation[N][M]
– no. of resource instances allocated to each process
– e.g. Allocation[i][j] == k
means Pi currently has k Rj’s
Need[N][M]
– no. of resource instances still needed by each process
– e.g. Need[i][j] == k
means Pi still needs k Rj’s
– 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 Rj’s
OSes: 6. Deadlock 34
Vectors shorthand for
referring to the
Allocation[i] 2D data structures
Need[i]
– resources still needed by Pi
Request[i]
– resources currently requested by Pi
OSes: 6. Deadlock 35
The Safety Algorithm VUW CS 305;
p221-222
OSes: 6. Deadlock
continued 37
Allocation Max Available Need
A B CA B C A B C A B C
P0 0 1 0 7 5 3 3 3 2 7 4 3
P1 2 0 0 3 2 2 1 2 2
P2 3 0 2 9 0 2 6 0 0
P3 2 1 1 2 2 2 0 1 1
P4 0 0 2 4 3 3 4 3 1
OSes: 6. Deadlock 38
VUW CS 305;
Request Resource Algorithm p.222
OSes: 6. Deadlock 40
Allocation Max Available Need
A B CA B C A B C A B C
P0 0 1 0 7 5 3 2 3 0 7 4 3
P1 3 0 2 3 2 2 0 2 0
P2 3 0 2 9 0 2 6 0 0
P3 2 1 1 2 2 2 0 1 1
P4 0 0 2 4 3 3 4 3 3
OSes: 6. Deadlock 41
Further Request Examples
OSes: 6. Deadlock 42
5. Deadlock Detection
P5
R1 R3 R4 P5
P1 P2 P3 P1 P2 P3
R2 P4 R5 P4
OSes: 6. Deadlock 45
5.2. Banker’s Algorithm Variation
OSes: 6. Deadlock 46
VUW CS 305;
Detection Algorithm p.225
OSes: 6. Deadlock
continued 48
Allocation Request Available
A B CA B C A B C
P0 0 1 0 0 0 0 0 0 0
P1 2 0 0 2 0 2
P2 3 0 3 0 0 0
P3 2 1 1 1 0 0
P4 0 0 2 0 0 2
OSes: 6. Deadlock 49
Example 2
Change P2 to request 1 C instance
Allocation Request Available
A B CA B C A B C
P0 0 1 0 0 0 0 0 0 0
P1 2 0 0 2 0 2
P2 3 0 3 0 0 1
P3 2 1 1 1 0 0
P4 0 0 2 0 0 2
The OS is deadlocked.
OSes: 6. Deadlock 50
6. Deadlock Recovery
System-based recovery:
– abort one or more processes in the circular wait
OSes: 6. Deadlock 51
6.1. Process Termination
OSes: 6. Deadlock 52
6.2. Resource Preemption
Issues:
– how to select a resource
(e.g. by using minimum cost)
OSes: 6. Deadlock 53