ICS 143 - Principles of Operating Systems
ICS 143 - Principles of Operating Systems
Operating Systems
Lectures 8 and 9 - Deadlocks
Prof. Nalini Venkatasubramanian
[email protected]
Outline
System Model
Deadlock Characterization
Methods for handling deadlocks
Deadlock Prevention
Deadlock Avoidance
Deadlock Detection
Recovery from Deadlock
Combined Approach to Deadlock Handling
2
Example 1
Example 2
P1
wait(A)
wait(B)
wait(B)
wait(A)
Definitions
Resources
Resource
Consumable Resources
System Model
Resource types
R1, R2,.Rm
No preemption:
Circular wait:
Process
Pi requests instance of Rj
Pi is holding an instance of Rj
10
P1
R2
P2
R3
P3
R4
11
P1
R2
P2
R3
P3
R4
12
P1
P2
P3
R2
P4
13
P1
R2
P2
R3
P3
R4
14
Basic facts
NO DEADLOCK
15
16
17
Deadlock Management
Prevention
Avoidance
Detection
Recovery
18
Deadlock Prevention
Mutual Exclusion
No Preemption
Circular Wait
Deadlock Avoidance
Deadlock Avoidance
22
Safe state
If Pi resource needs are not available, Pi can wait until all Pj have
finished.
When Pj is finished, Pi can obtain needed resources, execute,
return allocated resources, and terminate.
When Pi terminates, Pi+1 can obtain its needed resources...
23
Basic Facts
24
Claim Graph
26
Claim Graph
1
Possible Deadlock!!
27
Bankers Algorithm
Safety Algorithm
Finish[i] = false
Need_i <= Work
If no such i exists, go to step 4.
Work := Available
Finish[i] := false for i = 1,2,,n.
Finish[i] := true
go to step 2
Resource-Request Algorithm
for Process Pi
Example of Bankers
Algorithm
5 processes
3 resource types
P0 - P4;
A(10 instances), B (5 instances), C (7 instances)
Snapshot at time T0
P0
P1
P2
P3
P4
Allocation
A B C
0 1 0
2 0 0
3 0 2
2 1 1
0 0 2
Max
A B
7 5
3 2
9 0
2 2
4 3
Available
C A B C
3 3 3 2
2
2
2
3
32
Example (cont.)
Need
A B
7 4
1 2
6 0
0 1
4
3
C
3
2
0
1
1
33
P0
P1
P2
P3
P4
Allocation Need
Available
A B C A B C A B C
0 1 0 7 4 3 2 3 0
3 0 2 0 2
0
3 0 2 6 0 0
2 1 1 0 1 1
0 0 2 4 3 1
34
Example (cont.)
35
Deadlock Detection
36
37
Several instances of a
resource type
Data Structures
38
Work := Available
For i = 1,2,,n, if Allocation(i) 0, then Finish[i] := false,
otherwise Finish[i] := true.
Finish[i] = false
Request (i) Work
If no such i exists, go to step 4.
39
Finish[i] := true
go to step 2
40
Example of Detection
Algorithm
P0
P1
P2
P3
P4
41
Example (cont.)
P0
P1
P2
P3
P4
Request
A B C
0 0 0
2 0 2
0 0 1
1 0 0
0 0 2
42
Detection-Algorithm Use
How often -
Starvation
45
Combined approach to
deadlock handling