Dr.
Jayanta Pal
Assistant Professor
Department of Information Technology
Tripura University
Deadlock Detection
Allow system to enter deadlock state
Detection algorithm
Recovery scheme
Example
If there are three processes p1,p2 and p1 are acquiring
r1 resource and that r1 is needed by p2 which is
acquiring another resource r2 and that is needed by p1.
Here cycle occurs. It is called a deadlock.
Here in the above graph we found a cycle from P1 to P2 and again
to P1. So we can say that the system is in a deadlock state.
Deadlock Detection
The deadlock Detection Algorithm is of two types:
Wait-for-Graph Algorithm (Single Instance)
Banker’s Algorithm (Multiple Instance)
Wait-for-Graph Algorithm: It is a variant of the Resource
Allocation graph, processes as vertices in the graph.
If the Wait-for-Graph contains a cycle then we can say the
system is in a Deadlock state. We need to remove resources
while converting from Resource Allocation Graph to Wait-for-
Graph.
Resource Allocation Graph: Contains Processes and
Resources.
Wait-for-Graph: Contains only Processes after removing the
Resources while conversion from Resource Allocation Graph.
Algorithm
Step 1: Take the first process (Pi) from the resource
allocation graph and check the path in which it is acquiring
resource (Ri), and start a wait-for-graph with that particular
process.
Step 2: Make a path for the Wait-for-Graph in which there will
be no Resource included from the current process (Pi) to next
process (Pj), from that next process (Pj) find a resource (Rj)
that will be acquired by next Process (Pk) which is released
from Process (Pj).
Step 3: Repeat Step 2 for all the processes.
Step 4: After completion of all processes, if we find a closed-
loop cycle then the system is in a deadlock state, and
deadlock is detected.
Example
Consider a Resource Allocation Graph with
4 Processes P1, P2, P3, P4, and 4
Resources R1, R2, R3, R4. Find if there is a
deadlock in the Graph using the Wait for
Graph-based deadlock detection algorithm.
Step 1: First take Process P1 which is waiting for Resource R1,
resource R1 is acquired by Process P2.
Step 2: Now we can observe that there is a path from P1 to
P2 as P1 is waiting for R1 which is been acquired by P2. Now
the Graph would be after removing resource R1 looks like.
Step 3: From P2 we can observe a path from P2 to P3 as P2
is waiting for R4 which is acquired by P3. So make a path
from P2 to P3 after removing resource R4 looks like.
Step 4: From P3 we find a path to P4 as it is waiting for P3
which is acquired by P4. After removing R3 the graph looks
like this.
Step 5: Here we can find Process P4 is waiting for R2 which is
acquired by P1. So finally the Wait-for-Graph is as follows:
Resource-Allocation Graph and Wait-for Graph
Resource-Allocation Graph Corresponding wait-for graph
Resource Allocation Graph (RAG) Algorithm
Build a RAG − The first step is to build a Resource Allocation Graph (RAG)
that shows the allocation and request of resources in the system. Each
resource type is represented by a rectangle, and each process is
represented by a circle.
Check for cycles − Look for cycles in the RAG. If there is a cycle, it
indicates that the system is deadlocked.
Identify deadlocked processes − Identify the processes involved in the
cycle. These processes are deadlocked and waiting for resources held by
other processes.
Determine resource types − Determine the resource types involved in the
deadlock, as well as the resources held and requested by each process.
Take corrective action − Take corrective action to break the deadlock by
releasing resources, aborting processes, or preempting resources. Once
the deadlock is broken, the system can continue with normal operations.
Recheck for cycles − After corrective action has been taken, recheck the
RAG for cycles. If there are no more cycles, the system is no longer
deadlocked, and normal operations can resume.
Resource Allocation Graph (RAG) Algorithm
Advantages
Easy to understand and implement
Can handle multiple types of resources
Helps identify the processes involved in a deadlock
Disadvantages
Can be time-consuming for large systems
Can give false positives if there are multiple requests
for the same resource
Assumes that all resources are pre-allocated, which
may not be the case in some systems.
Wait-for Graph (WFG) Algorithm
Build a WFG − The first step is to build a Wait-for Graph (WFG) that shows
the waitfor relationships between processes. Each process is represented
by a circle, and an arrow is drawn from one process to another if the former
is waiting for a resource held by the latter.
Check for cycles − Look for cycles in the WFG. If there is a cycle, it
indicates that the system is deadlocked.
Identify deadlocked processes − Identify the processes involved in the
cycle. These processes are deadlocked and waiting for resources held by
other processes.
Determine resource types − Determine the resource types involved in the
deadlock, as well as the resources held and requested by each process.
Take corrective action − Take corrective action to break the deadlock by
releasing resources, aborting processes, or preempting resources. Once
the deadlock is broken, the system can continue with normal operations.
Recheck for cycles − After corrective action has been taken, recheck the
WFG for cycles. If there are no more cycles, the system is no longer
deadlocked, and normal operations can resume.
Wait-for Graph (WFG) Algorithm
Advantages
Can handle multiple types of resources
Useful for systems with a large number of processes
Provides a clear visualization of the deadlock
Disadvantages
Can be time-consuming for large systems
May give false positives if there are multiple requests
for the same resource
Assumes that all resources are pre-allocated, which
may not be the case in some systems.
Several Instances of a Resource Type
Available: A vector of length m indicates the number
of available resources of each type
Allocation: An n x m matrix defines the number of
resources of each type currently allocated to each
process
Request: An n x m matrix indicates the current
request of each process. If Request [i][j] = k, then
process Pi is requesting k more instances of resource
type Rj.
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
Algorithm requires an order of O(m x n2) operations to detect
whether the system is in deadlocked state
Example of Detection Algorithm
Five processes P0 through P4; three resource types
A (7 instances), B (2 instances), and C (6 instances)
Snapshot at time T0:
Allocation Request Available
ABC ABC ABC
P0 010 000 000
P1 200 202
P2 303 000
P3 211 100
P4 002 002
Sequence <P0, P2, P3, P1, P4> will result in Finish[i] = true for all i
1. In this, Work = [0, 0, 0] &
Finish = [false, false, false, false, false]
2. i=0 is selected as both Finish[0] = false and [0, 0, 0]<=[0, 0, 0].
3. Work =[0, 0, 0]+[0, 1, 0] =>[0, 1, 0] & Finish = [true, false, false, false, false].
4. i=2 is selected as both Finish[2] = false and [0, 0, 0]<=[0, 1, 0].
5. Work =[0, 1, 0]+[3, 0, 3] =>[3, 1, 3] & Finish = [true, false, true, false, false].
6. i=1 is selected as both Finish[1] = false and [2, 0, 2]<=[3, 1, 3].
7. Work =[3, 1, 3]+[2, 0, 0] =>[5, 1, 3] & Finish = [true, true, true, false, false].
8. i=3 is selected as both Finish[3] = false and [1, 0, 0]<=[5, 1, 3].
9. Work =[5, 1, 3]+[2, 1, 1] =>[7, 2, 4] & Finish = [true, true, true, true, false].
10. i=4 is selected as both Finish[4] = false and [0, 0, 2]<=[7, 2, 4].
11. Work =[7, 2, 4]+[0, 0, 2] =>[7, 2, 6] & Finish = [true, true, true, true, true].
12. Since Finish is a vector of all true it means there is no deadlock in this
example.
Advantages
Prevents deadlocks by ensuring that processes acquire all
required resources before execution
Can handle multiple resource types
Provides a safe and efficient resource allocation method
Disadvantages
May not be feasible for systems with a large number of
processes and resources
Assumes that resource requirements are known in
advance, which may not be the case in some systems
May result in low resource utilization if resources are
reserved but not used.
Example (Cont.)
P2 requests an additional instance of type C
Request
ABC
P0 000
P1 202
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
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 many cycles in the resource graph and
so we would not be able to tell which of the
many deadlocked processes “caused” the
deadlock.
Recovery from Deadlock: Process Termination
Abort all deadlocked processes
Abort one process at a time until the deadlock cycle is
eliminated
In which order should we choose to abort?
1. Priority of the process
2. How long process has computed, and how much longer
to completion
3. Resources the process has used
4. Resources process needs to complete
5. How many processes will need to be terminated
6. Is process interactive or batch?
Recovery from Deadlock: Resource Preemption
Selecting a victim – minimize cost
Rollback – return to some safe state, restart
process for that state
Starvation – same process may always be
picked as victim, include number of rollback in
cost factor