Professional Documents
Culture Documents
Deadlock
Deadlock
Deadlocks
Deadlock Characterization
Deadlock can arise if four conditions hold
simultaneously.
Mutual exclusion
Hold and wait
No preemption
Circular wait
Traffic Deadlock
System Model
Resource types R1, R2, . . ., Rm
CPU cycles, memory space, I/O devices
Resource-Allocation Graph
A set of vertices V and a set of edges E.
V is partitioned into two types:
P = {P1, P2, , Pn}, the set consisting of all the processes
in the system.
R = {R1, R2, , Rm}, the set consisting of all resource
types in the system.
request edge P1 Rj
assignment edge Rj Pi
7
Pi requests instance of Rj
Pi is holding an instance of Rj
Rj
Pi
Rj
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.
10
11
12
Deadlock Prevention
A set of methods for ensuring that at least one of
the necessary conditions cannot hold.
Restrain the ways request can be made.
Mutual Exclusion not required for sharable
resources; must hold for non-sharable resources.
Read only files and printers.
Hold and Wait
Require process to request and be allocated all its
resources before it begins execution, or allow process
to request resources only when the process has none.
Low resource utilization; starvation possible.
14
Protocol
If process P1, has requested disk drive, later on it can only request
printer and not tape drive.
F (printer) > F(disk drive), but F(tape drive ) < F(disk drive)
Proof - By contradiction
F (R0) < F(R1) < ..<F(Rn) < F(R0).
F(R0) < F(R0), is not possible.
16
Deadlock Avoidance
Requires that the system has some additional a priori
Information available.
Safe State
When a process requests an available resource, system must decide if
immediate allocation leaves the system in a safe state.
System is in safe state if there exists a safe sequence of all processes.
Sequence <P1, P2, , Pn> is safe if 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.
If Pi resource needs are not immediately available, then 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, and so on.
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.
19
20
Holding
5
2
2
21
22
23
Bankers Algorithm
Multiple instances.
Each process must a priori claim maximum use.
25
Example (Cont.)
The content of the matrix. Need is defined to be
Max Allocation.
Process
Allocation
Max
ABCD
ABCD
P1
3011
4111
P2
0100
0212
P3
1110
4210
P4
1101
1101
P5
0000
2110
Is the current state safe?
If P5 requests for (1,0,1,0), can this be granted?
28
Safety Algorithm
1. Let Work and Finish be vectors of length m
and n, respectively. Initialize:
Work = Available
Finish [i] = false for i = 1,2,3, , n.
P0
P1
P2
P3
P4
Need
ABC
743
122
600
011
431
Executing safety algorithm shows that sequence <P1, P3, P4, P0, P2>
29
satisfies
safety requirement.
Deadlock Detection
Allow system to enter deadlock state
Detection algorithm
Recovery scheme
32
Resource-Allocation Graph
34
35
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.
3. Work = Work + Allocationi
Finish[i] = true
go to step 2.
4. If Finish[i] == false, for some i, 1 i n, then the system is in deadlock
state. Moreover, if Finish[i] == false, then Pi is deadlocked.
36
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?
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.
38
Detection Algorithm
Request
1. Let Work and Finish be vectors of length m and n, respectively Initialize:
ABC
(a) Work = Available
P0
000
(b) For i = 1,2, , n, if Allocationi 0, then
P1
201
Finish[i] = false;otherwise, Finish[i] = true.
P2
001
2. Find an index i such that both:
P3
100
(a) Finish[i] == false
(b) Requesti Work
P4
002
If no such i exists, go to step 4.
3. Work = Work + Allocationi
Finish[i] = true
go to step 2.
39
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
41
42