OS - Deadlock-Detection
OS - Deadlock-Detection
Jayanta Pal
Assistant Professor
Department of Information Technology
Tripura University
Deadlock Detection
Detection algorithm
Recovery scheme
Example
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
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.)
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