Chapter 7: Deadlocks
Silberschatz, Galvin and Gagne ©2013
Operating System Concepts – 9th Edition
Chapter 7: Deadlocks
7.2
Silberschatz, Galvin and Gagne ©2013
Operating System Concepts – 9th Edition
Deadlocks
7.3
Silberschatz, Galvin and Gagne ©2013
Operating System Concepts – 9th Edition
System Model
7.4
Silberschatz, Galvin and Gagne ©2013
Operating System Concepts – 9th Edition
System Model
7.5
Silberschatz, Galvin and Gagne ©2013
Operating System Concepts – 9th Edition
Deadlock Characterization
Necessary Conditions
7.6
Silberschatz, Galvin and Gagne ©2013
Operating System Concepts – 9th Edition
Deadlock Characterization
4. Circular wait. A set {P0, P1, ..., Pn} of waiting processes must exist such that P0 is waiting for a resource held by P1, P1 is waiting for a resource held by P2, ..., Pn−1 is waiting for a resource held by Pn, and Pn is waiting for a resource held by P0.
7.7
Silberschatz, Galvin and Gagne ©2013
Operating System Concepts – 9th Edition
Resource-Allocation Graph
7.8
Silberschatz, Galvin and Gagne ©2013
Operating System Concepts – 9th Edition
Resource-Allocation Graph
7.9
Silberschatz, Galvin and Gagne ©2013
Operating System Concepts – 9th Edition
Resource-Allocation Graph (Cont.)
Pi
Pi
Rj
Rj
7.10
Silberschatz, Galvin and Gagne ©2013
Operating System Concepts – 9th Edition
Example of a Resource Allocation Graph
Figure: Resource-allocation graph.
7.11
Silberschatz, Galvin and Gagne ©2013
Operating System Concepts – 9th Edition
Resource-Allocation Graph
7.12
Silberschatz, Galvin and Gagne ©2013
Operating System Concepts – 9th Edition
Resource-Allocation Graph
7.13
Silberschatz, Galvin and Gagne ©2013
Operating System Concepts – 9th Edition
Resource-Allocation Graph
7.14
Silberschatz, Galvin and Gagne ©2013
Operating System Concepts – 9th Edition
Resource Allocation Graph With A Deadlock
Figure 7.2 Resource-allocation graph with a deadlock.
7.15
Silberschatz, Galvin and Gagne ©2013
Operating System Concepts – 9th Edition
Resource-Allocation Graph
7.16
Silberschatz, Galvin and Gagne ©2013
Operating System Concepts – 9th Edition
Graph With A Cycle But No Deadlock
Figure 7.3 Resource-allocation graph with a cycle but no deadlock.
7.17
Silberschatz, Galvin and Gagne ©2013
Operating System Concepts – 9th Edition
Methods for Handling Deadlocks
7.18
Silberschatz, Galvin and Gagne ©2013
Operating System Concepts – 9th Edition
Methods for Handling Deadlocks
7.19
Silberschatz, Galvin and Gagne ©2013
Operating System Concepts – 9th Edition
Deadlock Prevention
Mutual Exclusion
7.20
Silberschatz, Galvin and Gagne ©2013
Operating System Concepts – 9th Edition
Deadlock Prevention (Cont.)
Hold and Wait
7.21
Silberschatz, Galvin and Gagne ©2013
Operating System Concepts – 9th Edition
Deadlock Prevention (Cont.)
No Preemption
7.22
Silberschatz, Galvin and Gagne ©2013
Operating System Concepts – 9th Edition
Deadlock Prevention (Cont.)
Circular Wait
7.23
Silberschatz, Galvin and Gagne ©2013
Operating System Concepts – 9th Edition
Deadlock Prevention (Cont.)
7.24
Silberschatz, Galvin and Gagne ©2013
Operating System Concepts – 9th Edition
Deadlock Avoidance
7.25
Silberschatz, Galvin and Gagne ©2013
Operating System Concepts – 9th Edition
Deadlock Avoidance
7.26
Silberschatz, Galvin and Gagne ©2013
Operating System Concepts – 9th Edition
Safe State
7.27
Silberschatz, Galvin and Gagne ©2013
Operating System Concepts – 9th Edition
Safe State
7.28
Silberschatz, Galvin and Gagne ©2013
Operating System Concepts – 9th Edition
Safe State
Process | Maximum Needs | Current Needs |
P1 | 10 | 5 |
P2 | 4 | 2 |
P3 | 9 | 2 |
7.29
Silberschatz, Galvin and Gagne ©2013
Operating System Concepts – 9th Edition
Safe State
7.30
Silberschatz, Galvin and Gagne ©2013
Operating System Concepts – 9th Edition
Avoidance Algorithms
7.31
Silberschatz, Galvin and Gagne ©2013
Operating System Concepts – 9th Edition
Resource-Allocation-Graph Algorithm
7.32
Silberschatz, Galvin and Gagne ©2013
Operating System Concepts – 9th Edition
Resource-Allocation Graph
Figure 7.7 : Resource-allocation graph for deadlock avoidance.
7.33
Silberschatz, Galvin and Gagne ©2013
Operating System Concepts – 9th Edition
Resource-Allocation-Graph Algorithm
7.34
Silberschatz, Galvin and Gagne ©2013
Operating System Concepts – 9th Edition
Unsafe State In Resource-Allocation Graph
Figure 7.8 An unsafe state in a resource-allocation graph.
7.35
Silberschatz, Galvin and Gagne ©2013
Operating System Concepts – 9th Edition
Banker’s Algorithm
7.36
Silberschatz, Galvin and Gagne ©2013
Operating System Concepts – 9th Edition
Data Structures for the Banker’s Algorithm
7.37
Silberschatz, Galvin and Gagne ©2013
Operating System Concepts – 9th Edition
Data Structures for the Banker’s Algorithm
7.38
Silberschatz, Galvin and Gagne ©2013
Operating System Concepts – 9th Edition
Safety Algorithm
a. Finish[i] == false
b. Needi ≤Work
If no such i exists, go to step 4.
3. Work =Work + Allocationi
Finish[i] = true
Go to step 2.
7.39
Silberschatz, Galvin and Gagne ©2013
Operating System Concepts – 9th Edition
Resource-Request Algorithm
Available = Available–Requesti ;
Allocationi = Allocationi + Requesti ;
Needi = Needi –Requesti ;
7.40
Silberschatz, Galvin and Gagne ©2013
Operating System Concepts – 9th Edition
Example of Banker’s Algorithm
3 resource types:
A (10 instances), B (5instances), and C (7 instances)
Allocation Max Available
A B C A B C A B C
P0 0 1 0 7 5 3 3 3 2
P1 2 0 0 3 2 2
P2 3 0 2 9 0 2
P3 2 1 1 2 2 2
P4 0 0 2 4 3 3
7.41
Silberschatz, Galvin and Gagne ©2013
Operating System Concepts – 9th Edition
Example (Cont.)
Need
A B C
P0 7 4 3
P1 1 2 2
P2 6 0 0
P3 0 1 1
P4 4 3 1 �
7.42
Silberschatz, Galvin and Gagne ©2013
Operating System Concepts – 9th Edition
Example: P1 Request (1,0,2)
Allocation Need Available
A B C A B C A B C
P0 0 1 0 7 4 3 2 3 0
P1 3 0 2 0 2 0
P2 3 0 2 6 0 0
P3 2 1 1 0 1 1
P4 0 0 2 4 3 1
7.43
Silberschatz, Galvin and Gagne ©2013
Operating System Concepts – 9th Edition
Example (Cont.)
7.44
Silberschatz, Galvin and Gagne ©2013
Operating System Concepts – 9th Edition
Deadlock Detection
7.45
Silberschatz, Galvin and Gagne ©2013
Operating System Concepts – 9th Edition
Single Instance of Each Resource Type
7.46
Silberschatz, Galvin and Gagne ©2013
Operating System Concepts – 9th Edition
Resource-Allocation Graph and Wait-for Graph
Figure 7.9 (a) Resource-allocation graph. (b) Corresponding wait-for graph.
7.47
Silberschatz, Galvin and Gagne ©2013
Operating System Concepts – 9th Edition
Several Instances of a Resource Type
7.48
Silberschatz, Galvin and Gagne ©2013
Operating System Concepts – 9th Edition
Detection Algorithm
a. Finish[i] == false
b. Requesti ≤Work
If no such i exists, go to step 4.
3. Work =Work + Allocationi
Finish[i] = true
Go to step 2.
Moreover, if Finish[i] == false, then process Pi is deadlocked.
7.49
Silberschatz, Galvin and Gagne ©2013
Operating System Concepts – 9th Edition
Example of Detection Algorithm
Allocation Request Available
A B C A 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
7.50
Silberschatz, Galvin and Gagne ©2013
Operating System Concepts – 9th Edition
Example (Cont.)
A B C
P0 0 0 0
P1 2 0 2
P2 0 0 1
P3 1 0 0
P4 0 0 2
7.51
Silberschatz, Galvin and Gagne ©2013
Operating System Concepts – 9th Edition
Detection-Algorithm Usage
7.52
Silberschatz, Galvin and Gagne ©2013
Operating System Concepts – 9th Edition
Recovery from Deadlock: Process Termination
Process Termination
7.53
Silberschatz, Galvin and Gagne ©2013
Operating System Concepts – 9th Edition
Recovery from Deadlock: Process Termination
7.54
Silberschatz, Galvin and Gagne ©2013
Operating System Concepts – 9th Edition
Recovery from Deadlock: Process Termination
7.55
Silberschatz, Galvin and Gagne ©2013
Operating System Concepts – 9th Edition
Recovery from Deadlock: Resource Preemption
7.56
Silberschatz, Galvin and Gagne ©2013
Operating System Concepts – 9th Edition
End of Chapter 7
Silberschatz, Galvin and Gagne ©2013
Operating System Concepts – 9th Edition