OS Lecture-8 (Deadlock)
OS Lecture-8 (Deadlock)
Deadlocks
Introduction
Deadlock in Real Life
• Mutual exclusion:
– only one process at a time can use a resource.
• 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, …, Pn} 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, …, Pn–1 is waiting for a resource that is
held by Pn, and P0 is waiting for a resource that is held by P0.
Mutual exclusion
• A resource can be held by only one process at a time.
• Pi requests instance of Rj
Pi
Rj
• Pi is holding an instance of Rj
Pi
Rj
Example of a Resource Allocation Graph
Allocation Need
R1 R2 R3 R1 R2 R3
P1 0 1 0 1 0 0
P2 1 1 0 0 0 1
P3 0 0 1 0 0 0
No cycle No deadlock
Resource Allocation Graph with a Deadlock
Allocation Need
R1 R2 R3 R1 R2 R3
P1 0 1 0 1 0 0
P2 1 0 0 0 0 1
P3 0 0 1 0 1 0
Avail = [ 0 0 0 ]
Allocation Need
R1 R2 R1 R2
P1 0 1 1 0
P2 1 0 0 0
P3 1 0 0 1
P4 0 1 0 0
Avail = [ 0 0 0 ]
3. Ignore the problem and pretend that deadlocks never occur in the
system; used by most operating systems, including UNIX.
Deadlock Prevention
Deadlock Prevention
Disallow at least one condition that is necessary for deadlock to occur.
• System is in safe state if there exists a sequence <P1, P2, …, Pn> of 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:
─ If Pi resource needs are not immediately available, then Pi can
wait until all Pj have finished.
• 3 drives remaining
Allocate one
instance of
R2 to P2
• Finish: Boolean value, either true or false. If finish[i] = true for all i
return safe else unsafe.
Safety Algorithm
1. Let Available and Finish be vectors of length m and n,
respectively. Initialize:
Work = Available and 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.
Resource-Request Algorithm for Process Pi
Let Requesti be the request array for process Pi. Requesti [j] = k means
process Pi wants k instances of resource type Rj.
1. If Requesti ≤ Needi go to step 2. Otherwise, raise error condition, since
process has exceeded its maximum claim
2. If Requesti ≤ Available, go to step 3.
Otherwise Pi must wait, since resources are not available
3. Pretend to allocate requested resources to Pi by modifying the state as
follows:
Available = Available – Requesti;
Allocationi = Allocationi + Requesti;
Needi = Needi – Requesti;
Allocation Max
ABC ABC
P0 010 753
P1 200 322
P2 302 902
P3 211 222
P4 002 433
Need0 = 7, 4, 3 0 1 2 3 4
Finish[0] is false and Need0 > Work Finish = false true false false false
So P0 must wait
Step 2 Step 2
For i = 2 For i = 4
Need2 = 6, 0, 0 Need4 = 4, 3, 1
Finish[2] is false and Need2 > Work Finish[4] is false and Need4 ≤ Work
So P2 must wait So P4 must be kept in safe sequence
Step 2 Step 3
For i = 3
Work = Work + Allocation4
Need3 = 0, 1, 1
A B C
Finish[3] is false and Need3 ≤ Work
Work = 7 4 5
So P3 must be kept in safe sequence
0 1 2 3 4
Step 3 Finish = false true false true true
Work = Work + Allocation3
A B C
Work = 7 4 3 Step 2
For i = 0
0 1 2 3 4 Need0 = 7, 4, 3
Finish = false true false true false Finish[0] is false and Need0 ≤ Work
So P0 must be kept in safe sequence
Step 3 Step 3
Work = Work + Allocation0 Work = Work + Allocation2
A B C A B C
Work = 7 5 5 Work = 10 5 7
0 1 2 3 4 0 1 2 3 4
Finish = true true false true true Finish = true true true true true
Step 2 Step 4
For i = 2
Need2 = 6, 0, 0 Finish[i] = true for 0 ≤ i < n
Finish[2] is false and Need2 ≤ Work Hence the system is in safe state
So P2 must be kept in safe sequence
• Executing safety algorithm shows that sequence < P1, P3, P4, P0, P2 > satisfies
safety requirement.
• Can request for (3,3,0) by P4 be granted?
• Can request for (0,2,0) by P0 be granted?
Deadlock Prevention Vs. Deadlock Avoidance
Deadlock Detection
and Recovery
Deadlock Detection and Recovery
• System lets the deadlocks occur.