Professional Documents
Culture Documents
Deadlock Avoidance
Deadlock Detection and Recovery (from Deadlock)
Chapter Objectives
R1 R2
Trafic1 needs resources R1 & R2 Trafic3 needs resources R3 & R4 to cross
R4 R3
Traffic 3
R1 R2
R4 R3
Traffic 3 ( T3 )
Deadlock (Hold and wait)
T1 holds resource R1, waiting for R2 Circular wait of users (processes)
User T1 waiting for T2 to complete
T2 holds resource R2, waiting for R3
User T2 waiting for T3 to complete
T3 holds resource R3, waiting for R4 User T3 waiting for T4 to complete
T4 holds resource R4, waiting for R1 User T4 waiting for T1 to complete
Traffic Crossing (deadlock Prevention)
(Yellow users, Red resources) T
r
a
f
f
i
c
2
Traffic 1
R1 R2
R4 R3
Pi
Pi requests an instance of Rj
Rj
Pi is holding an instance of Rj Pi
Rj
Example of a Resource Allocation
Graph
Resource Allocation Graph With A
Deadlock
P1 P2 P3
necessary conditions
Direct
allocation)
Deadlock Prevention (Cont.)
To break any necessary condition to prevent deadlock
is not so easy;
1. Mutual exclusion : Non-shareable resource can not be used jointly to
break mutual-exclusion
2. Hold and wait : If processes release resources or abort, to break hold-and-
wait condition, then wastage or under utilization of earlier execution
3. Non-preemption: Processes normally do not release resources midway,
voluntarily, if they have to wait for currently non available resource
4. Circular wait: If each resource is given distinct no to avoid circular wait and
each process requests for a specific resource by its Id, then the process
will lose generality, and cause resource under utilization.
No deadlock threat
Still needed
Maximum Need Allocated claim
P0 10 6 4
P1 4 2 2
P2 9 2 7
Current state: Total resources = 12 Available = 2
In case of Prevention D/L handling technique only one process can execute at a time.
Safe: if next allocation is done to process P1 (leaves 1 resource, need of P1 can be fulfilled that will return 4)
Unsafe: if next allocation is done to process P0 or P2 (needs of none can be fulfilled)
Basic Facts
If a system is in safe state no threat of
deadlock.
{
finish [ n ] = FALSE ; // assume nth process can not finish
resources = resources – Allocated [ n ]; // Presently free resources
}
Max. Need
Cont: safe-allocation procedure (second Allocated
part)
P0 10 6
while ( ! done )
P1 4 2
{
P2 9 2
done = TRUE ; // assume all processes are done / checked
for (n=0; n < P ; n++) // check processes that can return, if finished
{ // resource reclaiming part follows
if ( ! finish [n] ) && Needed [n] <= resources )
{
finish [n] = TRUE; // nth process can complete
resources = resources + Allocated [ n ]; // return resources
done = FALSE ; // needs further checking after reclaim
}
}
}
}
(1 0 2) P1 322 302 020 230 Allocation to P1
P (Need)SAFE: If all processes can complete with reclaimed resources 2 3 0
Reclaiming (Need < = resources)
P1 (0 2 0) 322 322 --- 532 230 + 302
P3 (0 1 1) 222 222 --- 743 532 + 211
P4 (4 3 1) 433 433 --- 745
P0 (7 4 3) 753 753 --- 755
P2 (6 0 0) 902 902 --- 10 5 7 755 + 302
Order processes finish
Tracing algorithm shows that sequence < P1, P3, P4, P0, P2> satisfies safety criteria for the request (1,0,2) by P1 to be
granted.
Exercise:
A total of 3 types of resources (A, B and C) are available on a system,
such that there are A (5 instances), B (7 instances), and C (10
instances). That is, the Available Resource Vector (A,B,C) is (5,7,10). The
status of different resources for each process is shown as under:
Processes Max. Need Allocated Need Available
Total: ABC ABC ABC 5 7 10
P0 537 100 437 4 7 10
P1 223 002 221 478
P2 029 023 006 455
P3 222 112 110 343
P4 334 020 314 323
Do the following requests, to allocate resources will result into Safe state, if
so, in which sequence the processes will complete, otherwise, which
processes will enter in deadlock state? (consider any two of the following,
individually not one after the other)
1.P1 requests for resources (0 2 1) Safe.
2.P4 requests for resources (2 1 0) Unsafe. (P0 and P4 can not finish)
3.P0 requests for resources (2 2 0) Unsafe. (All processes will deadlock)
Solution Processes Max. Allocated Need Available
Total: Need ABC ABC 5 7 10
P0 537 100 437 4 7 10
P1 223 002 221 478
P2 029 023 006 455
P3 222 112 110 343
P4 334 020 314 323
The request by process P1 for resources (0 2 1) < Available resources (3 2 3), so after supposed
allocation to P1, the allocated will be (0 2 3), the Need will be (2,0,0) and Available resources
will be (3 0 2) Y/N
Process Can finish ? Allocated Need Available after reclaiming
Need <= Available ABC ABC Allocated + Available
P0 437 < 302 N 100 437
P1 200 < 302 Y 023 023 +302 = 325
P2 006 < 325 N 002 006
P3 110 < 325 Y 112 112 + 325 =437
314 < 437 Y 020 020 + 437 = 457
437 < 457 Y 100 100 + 457 = 557
P4 Y
006 < 557 023 0 2 3 + 5 5 7 = 5 7 10
P0
P2
The processes will finish in the sequence: < P1, P3, P4, P0, P2 >, This allocation results in Safe state.
Whereas, the request by process P4 for resources (3 0 3) < Available resources (3 2 3). After supposed
allocation to P4, the allocated will be (3 2 3), the need will be ( 0 1 1) and available resources will be (0 2
0). With this much available resources, need of none of the processes can be met. Therefore, this is an
Un-Safe state, so this allocation shall be reverted
Deadlock Detection and Recovery
(adopted on systems with remote chances of deadlock)
Allows processes to enter into deadlock state
No need to pre state needed resources
Saves time otherwise consumed on running safety algorithms on
allocating each resource (record resource-process allocation)
Keep record of Available (Work), Requests and Allocations
Detection algorithm
Execute automatically after regular intervals or interactively:
When system or a group of processes look “BUSY, DOING
NOTHING”
When ready queue is nearly empty and blocked queues are heavily
populated
When idle process is taking most of the execution time
Recovery scheme
detecting deadlock, for recovery adopt conservative approach
Single Instance of Each Resource Type
Maintain wait-for graph
Nodes are processes.
Pi Pj if Pi is waiting for Pj.