Operating Systems Concepts 5th Edition Chapter7
Operating Systems Concepts 5th Edition Chapter7
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
System Model
System consists of resources
Resource types R1, R2, . . ., Rm
request
use
release
7.4
Deadlock Characterization
Deadlock can arise if four conditions hold simultaneously.
Mutual exclusion: only one process at a time can use a
resource
Hold and wait: a process holding at least one resource is
the process holding it, after that process has completed its task
Circular wait: there exists a set {P0, P1, , Pn} of waiting
7.5
7.6
Resource-Allocation Graph
A set of vertices V and a set of edges E.
V is partitioned into two types:
7.7
Pi requests instance of Rj
Pi
Rj
Pi is holding an instance of Rj
Pi
Rj
Operating System Concepts 9 th Edition
7.8
7.9
7.10
7.11
Basic Facts
If graph contains no cycles no deadlock
If graph contains a cycle
7.12
7.13
Deadlock Prevention
Restrain the ways request can be made
Mutual Exclusion not required for sharable resources; must
7.14
Process will be restarted only when it can regain its old resources,
as well as the new ones that it is requesting
7.15
Deadlock Example
/* thread one runs in this function */
void *do work one(void *param)
{
7.16
7.17
Deadlock Avoidance
Requires that the system has some additional a priori information
available
Simplest and most useful model requires that each process declare
7.18
Safe State
When a process requests an available resource, system must decide
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.19
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.20
7.21
Avoidance algorithms
Single instance of a resource type
7.22
resource
Request edge converted to an assignment edge when the resource
7.23
Resource-Allocation Graph
7.24
7.25
7.26
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.27
allocated k instances of Rj
Need: n x m matrix. If Need[i,j] = k, then Pi may need k more
7.28
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.29
7.30
3 resource types:
A (10 instances), B (5instances), and C (7 instances)
Snapshot at time T0:
Allocation
Max
Available
ABC
ABC
ABC
P0
010
753
332
P1
200
322
P2
302
902
P3
211
222
P4
002
433
7.31
Example (Cont.)
The content of the matrix Need is defined to be Max Allocation
Need
ABC
P0
743
P1
122
P2
600
P3
011
P4
431
The system is in a safe state since the sequence < P1, P3, P4, P2, P0>
7.32
Allocation
Need
Available
ABC
ABC
ABC
P0
010
743
230
P1
302
020
P2
302
600
P3
211
011
P4
002
431
Executing safety algorithm shows that sequence < P1, P3, P4, P0, P2>
7.33
Deadlock Detection
Allow system to enter deadlock state
Detection algorithm
Recovery scheme
7.34
Pi Pj if Pi is waiting for Pj
7.35
Resource-Allocation Graph
7.36
7.37
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.38
7.39
Allocation
Request
Available
ABC
ABC
ABC
P0
010
000
000
P1
200
202
P2
303
000
P3
211
100
P4
002
002
Sequence <P0, P2, P3, P1, P4> will result in Finish[i] = true for all i
7.40
Example (Cont.)
P2 requests an additional instance of type C
Request
ABC
P0
000
P1
202
P2
001
P3
100
P4
002
State of system?
7.41
Detection-Algorithm Usage
When, and how often, to invoke depends on:
in the resource graph and so we would not be able to tell which of the
many deadlocked processes caused the deadlock.
7.42
2.
3.
4.
5.
6.
7.43
7.44