Deadlock Condition System Programming & Operating System PDF
Deadlock Condition System Programming & Operating System PDF
System Model
Deadlock Characterization
Methods for Handling Deadlocks
Deadlock Prevention
Deadlock Avoidance
Deadlock Detection
Recovery from Deadlock
Combined Approach to Deadlock Handling
8.1
Example
semaphores A and B, initialized to 1
P0
P1
8.2
8.3
System Model
Resource types R1, R2, . . ., Rm
CPU cycles, memory space, I/O devices
Each resource type Ri has Wi instances.
Each process utilizes a resource as follows:
request
use
release
8.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 waiting to acquire additional resources held by other
processes.
No preemption: a resource can be released only
voluntarily by the process holding it, after that process has
completed its task.
Circular wait: there exists a set {P0, P1, , P0} of waiting
processes such that P0 is waiting for a resource that is
held by P1,
P1 is waiting for a resource that is held by P2, ,
Pn1 is waiting for a resource that is held by Pn,
and P0 is waiting for a resource that is held by P0.
Operating System Concepts
8.5
Resource-Allocation Graph
in the system.
8.6
Pi requests instance of Rj
Pi
Rj
Pi is holding an instance of Rj
Pi
Rj
Operating System Concepts
8.7
8.8
8.9
8.10
Basic Facts
If graph contains no cycles no deadlock.
If graph contains a cycle
if only one instance per resource type, then deadlock.
if several instances per resource type, possibility of deadlock .
8.11
state.
recover.
8.12
Deadlock Prevention
Restrain the ways request can be made.
Mutual Exclusion not required for sharable resources;
8.13
8.14
Deadlock Avoidance
Requires that the system has some additional a priori information
available.
Simplest and most useful model requires that each
8.15
Safe State
When a process requests an available resource, system
processes.
8.16
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.
8.17
8.18
requests a resource.
8.19
8.20
8.21
Bankers Algorithm
Multiple instances.
Each process must a priori claim maximum use.
When a process requests a resource it may
have to wait.
8.22
Max[i,j]
8.23
Allocation [i,j].
Silberschatz, Galvin and Gagne 2002
Safety Algorithm
1. Let Work and Finish be vectors of length m and n,
respectively. Initialize:
Work = Available
Finish [i] = false for i - 1,3, , n.
8.24
8.25
200 322
P2
302 902
P3
211 222
P4
002 433
Available
332
8.26
Need
ABC
P0 7 4 3
P1 1 2 2
P2 6 0 0
P3 0 1 1
P4 4 3 1
Example (Cont.)
The content of the matrix. Need is defined to be Max
Allocation.
P0
Need
ABC
743
P1
122
P2
600
P3
011
P4
431
The system is in a safe state since the sequence < P1, P3, P4,
8.27
Allocation NeedAvailable
ABC
A B CA B C
P0
010743
P1
3 0 20 2 0
P2
301600
P3
211011
P4
002431
230
Executing safety algorithm shows that sequence <P1, P3, P4, P0, P2>
8.28
Deadlock Detection
Allow system to enter deadlock state
Detection algorithm
Recovery scheme
8.29
in the graph.
8.30
Resource-Allocation Graph
8.31
8.32
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.
8.33
8.34
200202
P2
3 0 30 0 0
P3
211100
P4
002002
Sequence <P0, P2, P3, P1, P4> will result in Finish[i] = true for all
i.
8.35
Example (Cont.)
P2 requests an additional instance of type C.
Request
ABC
P0
000
P1
201
P2
001
P3
100
P4
002
State of system?
Can reclaim resources held by process P0, but insufficient
resources to fulfill other processes; requests.
Deadlock exists, consisting of processes P1, P2, P3, and P4.
8.36
Detection-Algorithm Usage
When, and how often, to invoke depends on:
How often a deadlock is likely to occur?
How many processes will need to be rolled back?
one for each disjoint cycle
If detection algorithm is invoked arbitrarily, there may be
8.37
cycle is eliminated.
longer to completion.
Resources the process has used.
Resources process needs to complete.
How many processes will need to be terminated.
Is process interactive or batch?
8.38
8.39
classes.
8.40
8.41