Chapter 7: Deadlocks: Silberschatz, Galvin and Gagne ©2009 Operating System Concepts - 8 Edition
Chapter 7: Deadlocks: Silberschatz, Galvin and Gagne ©2009 Operating System Concepts - 8 Edition
Chapter 7: Deadlocks
System Model
Deadlock Characterization
Deadlock Prevention
Deadlock Avoidance
Deadlock Detection
7.2
Chapter Objectives
To develop a description of deadlocks, which prevent sets of
7.3
Example
P1 and P2 each hold one disk drive and each needs another one
Example
7.4
wait(A)
7.5
System Model
Resource types R1, R2, . . ., Rm
request
use
release
7.6
Deadlock Characterization
Deadlock can arise if four conditions hold simultaneously.
Mutual exclusion: only one process at a time can use a
resource
the process holding it, after that process has completed its task
7.7
Resource-Allocation Graph
A set of vertices V and a set of edges E.
V is partitioned into two types:
7.8
Pi requests instance of Rj
Pi
Pi is holding an instance of Rj
Rj
Pi
Rj
Operating System Concepts 8th Edition
7.9
7.10
7.11
7.12
Basic Facts
If graph contains no cycles no deadlock
If graph contains a cycle
7.13
7.14
Deadlock Prevention
Restrain the ways request can be made
Mutual Exclusion not required for sharable resources; must
7.15
Process will be restarted only when it can regain its old resources,
as well as the new ones that it is requesting
7.16
Deadlock Avoidance
Requires that the system has some additional a priori information
available
Simplest and most useful model requires that each process declare
7.17
Safe State
When a process requests an available resource, system must decide if
ALL the processes in the systems such that for each Pi, the
resources that Pi can still request can be satisfied by currently
available resources + resources held by all the Pj, with j < I
That is:
7.18
Basic Facts
If a system is in safe state no deadlocks
If a system is in unsafe state possibility of deadlock
Avoidance ensure that a system will never enter an unsafe state.
7.19
7.20
Avoidance algorithms
Single instance of a resource type
7.21
resource
7.22
Resource-Allocation Graph
7.23
7.24
7.25
Bankers Algorithm
Multiple instances
Each process must a priori claim maximum use
When a process requests a resource it may have to wait
When a process gets all its resources it must return them in a finite
amount of time
7.26
allocated k instances of Rj
7.27
Safety Algorithm
1. Let Work and Finish be vectors of length m and n, respectively.
Initialize:
Work = Available
Finish [i] = false for i = 0, 1, , n- 1
2. Find an i such that both:
(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
4. If Finish [i] == true for all i, then the system is in a safe state
7.28
7.29
3 resource types:
A (10 instances), B (5instances), and C (7 instances)
Snapshot at time T0:
Allocation
ABC
Max
ABC
010
753
P1 2 0 0
322
P2 3 0 2
902
P3 2 1 1
222
P4 0 0 2
433
P0
Available
ABC
332
7.30
Example (Cont.)
The content of the matrix Need is defined to be Max Allocation
Need
ABC
P0 7 4 3
P1 1 2 2
P2 6 0 0
P3 0 1 1
P4 4 3 1
The system is in a safe state since the sequence < P1, P3, P4, P2, P0>
7.31
Allocation
P0
P1
Need
Available
ABC
ABC
ABC
010
743
230
302
020
P2
302
600
P3
211
011
P4
002
431
Executing safety algorithm shows that sequence < P1, P3, P4, P0, P2>
7.32
Deadlock Detection
Allow system to enter deadlock state
Detection algorithm
Recovery scheme
7.33
Pi Pj if Pi is waiting for Pj
7.34
Resource-Allocation Graph
7.35
7.36
Detection Algorithm
1. Let Work and Finish be vectors of length m and n, respectively Initialize:
(a) Work = Available
(b) For i = 1,2, , n, if Allocationi 0, then
Finish[i] = false; otherwise, Finish[i] = true
2. Find an index i such that both:
(a) Finish[i] == false
(b) Requesti Work
If no such i exists, go to step 4
7.37
7.38
Allocation
ABC
Request Available
ABC ABC
010
P0
000 000
200
P1
202
303
P2
P3
211
100
P4
002
002
000
Sequence <P0, P2, P3, P1, P4> will result in Finish[i] = true for all i
7.39
Example (Cont.)
P2 requests an additional instance of type C
Request
ABC
P0 0 0 0
P1 2 0 2
P2 0 0 1
P3 1 0 0
P4 0 0 2
State of system?
7.40
Detection-Algorithm Usage
When, and how often, to invoke depends on:
the resource graph and so we would not be able to tell which of the
many deadlocked processes caused the deadlock.
7.41
7.42
7.43
End of Chapter 7