You are on page 1of 37

Deadlocks

Deadlocks
Resource Allocation Graph
Conditions
for deadlock
Conditions
for deadlock
Conditions
for deadlock
Conditions
for deadlock
Conditions
for deadlock
Handling Deadlocks

• Detection and Recovery


• Avoidance
• Prevention
Deadlock Avoidance
Deadlock Avoidance
Deadlock Avoidance
Banker’s Example
Banker’s Example
Banker’s Example
Banker’s Example (for single instance of a resource)
Here, all four necessary
Multiple Instances of a resource conditions of deadlock
are true, but these are
not sufficient to say
that there is deadlock.

Because, P2 is holding
one instance of R1 but
it does not require any
other resource.

So after some time, P2


will terminate and
release R1, which can
be then given to P3.

Similar is true for P4.

Hence, deadlock is not


guaranteed here.
Multiple Instances of a resource

Here, clearly Hold and


wait condition is true
for P1, P2 , and P3.

Hence, deadlock is
guaranteed here.
Data Structures for the Banker’s Algorithm

Let n = number of processes, and m = number of resources types.


 Available: Vector of length m.
 If available [j] = k, then there are k instances of resource type Rj
available
 Max: n x m matrix.
 If Max [i,j] = k, then process Pi may request at most k instances of
resource type Rj
 Allocation: n x m matrix.
 If Allocation[i,j] = k then Pi is currently allocated k instances of Rj
 Need: n x m matrix.
 If Need[i,j] = k, then Pi may need at most k more instances of Rj to
complete its task.
 Need [i,j] = Max[i,j] – Allocation [i,j]
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. Otherwise, in an unsafe state.
example
• 5 processes -- P0 ….. P4;
• 3 resource types:
A (10 instances), B (5 instances), and C (7 instances)
• Snapshot at time T0:
Allocation Max Available(Remaining)
ABC ABC ABC
P0 0 1 0 753 332
P1 2 0 0 322
P2 3 0 2 902
P3 2 1 1 222
P4 0 0 2 433
Total= 7 2 5
ex
• The content of the matrix Need is defined to be Max – Allocation

Allocation Need Available


ABC ABC ABC
P0 0 1 0 743 332
P1 2 0 0 122
P2 3 0 2 600
P3 2 1 1 011
P4 0 0 2 431
P0 cannot be allocated because its Need is
Greater than Available(Work).

P1 can be allocated its Need= 1 2 2 because


Work= 3 3 2, ie, work>need. So it gets 1 2 2,
So now, alloc= 3 2 2 and Need= 0 0 0 and
work= 2 1 0.
Now, P1 does its execution and releases all its
resources back to work. So, work=work+1 2 2=
2 1 0+ 1 2 2= 3 3 2

Again, work=work+alloc= 3 3 2+2 0 0= 5 3 2

The system is in a safe state since the sequence < P1, P3, P4, P2,
P0> satisfies safety criteria
Given the above state -- can request for (3,3,0) by P4 be
granted?
Given the above state -- can request for (0,2,0) by P0 be
granted?
Deadlock Prevention
Deadlock Prevention
Deadlock Prevention
Ex: P1 (requires R1,R2, R3); gets resources R1, R2, R3
P2 (requires R2, R1, R4); cannot get R1 because it is with P1, and cannot get R2, R4 before R1, so
waits.
P3 (requires R3, R2), cannot get R2 because it is with P1, and cannot get R3 before R2 ,so waits.
Deadlock
Detection

• Keep Track of
current allocated
resources
Deadlock
Detection
Deadlock
Detection

4-2=2
Deadlock
Detection

4-2=2
Deadlock
Detection
Deadlock
Detection
Deadlock Recovery
Deadlock Recovery
Deadlock Recovery

You might also like