
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Deadlock Detection Algorithm in Operating System
Introduction
Deadlock is a situation that occurs in a computer system when two or more processes are blocked and waiting for each other to release resources, resulting in a stalemate.

It's a serious issue in operating systems as it can cause the entire system to freeze or crash. Therefore, detecting and resolving deadlock is crucial for the smooth operation of any computer system.
Deadlock detection algorithms are used to identify the presence of deadlocks in computer systems. These algorithms examine the system's processes and resources to determine if there is a circular wait situation that could lead to a deadlock. If a deadlock is detected, the algorithm can take steps to resolve it and prevent it from occurring again in the future. There are several popular deadlock detection algorithms.Here we will explore necessary conditions of deadlock, purpose of the deadlock detection algorithm,mainly each of these algorithms in detail and the situations in which they are most effective.
Purpose of deadlock detection algorithm
The purpose of a deadlock detection algorithm is to identify and resolve deadlocks in a computer system.
It does so by identifying the occurrence of a deadlock, determining the processes and resources involved, taking corrective action to break the deadlock, and restoring normal system operations.
The algorithm plays a crucial role in ensuring the stability and reliability of the system by preventing deadlocks from causing the system to freeze or crash.
Deadlock Detection Algorithms
1. 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.
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.
Example
Consider a system with two processes, P1 and P2, and two resources, R1 and R2.
Process |
R1 |
R2 |
---|---|---|
P1 |
1 |
0 |
P2 |
0 |
1 |
The RAG for this system can be represented as follows:------
P1 -> R1
P2 -> R2
R1 -> P2
R2 -> P1
There is a cycle between P1 and P2, indicating a potential deadlock. To confirm whether there is a deadlock, we can use the cycle-detection algorithm on the RAG. The algorithm will detect the cycle and identify a potential deadlock between P1 and P2. We can then take appropriate actions to resolve the deadlock and prevent it from occurring in the future.
2. 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.
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.
Example
Three processes, P1, P2, and P3, and two resources, R1 and R2.
Process |
R1 |
R2 |
---|---|---|
P1 |
1 |
0 |
P2 |
0 |
1 |
P3 |
1 |
1 |
The wait-for graph (WFG) for this system can be represented as follows ?
P1 -> P3
P3 -> P2
P2 -> P3
There is a cycle between P2 and P3, indicating a potential deadlock. To confirm whether there is a deadlock, we can use the cycle-detection algorithm on the WFG. The algorithm will detect the cycle and identify a potential deadlock between P2 and P3. We can then take appropriate actions to resolve the deadlock and prevent it from occurring in the future.
3. Banker's Algorithm
It can be used as a deadlock detection algorithm. In fact, it is one of the most well-known algorithms for deadlock detection in operating systems.
It uses 3 data structures -
Available ?
Vector of length m
It indicates how many available resources of each type are there.
Allocation ?
Matrix of size n*m
A[i,j] indicates how many j th resource type allocated to i th process.
Request ?
Matrix of size n*m
Indicates request of each process.
Request[i,j] tells the number of instances Pi process is requested of jth resource type.
Algorithm
Step 1 ?
Let Work(vector) length = m
Finish(vector) length = n
Initialize Work= Available.
For i=0, 1, ?., n-1, if Allocation_i = 0, then Finish[i] = true; otherwise, Finish[i]= false.
Step 2 ?
Find an index i such that both
Finish[i] == false
Request_i <= Work
If no such i exists go to step 4.
Step 3 ?
Work= Work + Allocation_i
-
Finish[i]= true
Go to Step 2.
Step 4 ?
If Finish[i]== false for some i, 0<=i<n, then the system is in a deadlocked state. Moreover, if Finish[i]==false the process Pi is deadlocked.
Otherwise no deadlock.
Example
Processes |
Allocation (ABC) |
Request (ABC) |
Available (ABC) |
---|---|---|---|
P0 |
010 |
000 |
000 |
P1 |
200 |
202 |
|
P3 |
303 |
000 |
|
P4 |
211 |
100 |
|
P5 |
002 |
002 |
Solution
-
Step 1 ?
Work =[0,0,0] &
Finish = [false, false, false, false, false].
-
Step 2 ?
i=0 is selected as both Finish[0]=false and [0,0,0] <=[0,0,0].
-
Step 3 ?
Work = [0,0,0]+[0,1,0]=>[0,1,0] &
Finish = [true, false, false, false, false].
-
Step 4 ?
i=2 is selected as both Finish[2] = false and [0,0,0]<=[0,1,0].
-
Step 5 ?
Work = [0,1,0]+[3,0,3] = > [3,1,3] &
Finish = [true, false, false, false, false].
-
Step 6 ?
i=1 is selected as both Finish[1] = false and [2,0,2] <=[3,1,3].
-
Step 7 ?
Work=[3,1,3]+[2,0,0] => [5,1,3] &
Finish = [true, true, true, false, false].
-
Step 8 ?
i=3 is selected as both Finish[3] =false and [1,0,0] <= [5,1,3].
-
Step 9 ?
Work = [5,1,3] + [2,1,1] =>[7,2,4] &
Finish = [true, true, true, true, false].
-
Step 10 ?
i=4 is selected as both Finish[4] = false and [0,0,2] <=[7,2,4].
-
Step 11 ?
Work = [7,2,4] + [0,0,2] =>[7,2,6] &
Finish = [true, true, true, true, true].
Now we can see Finish (a vector) contains all true values, 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.
Conclusion
Deadlock detection algorithms are important tools for maintaining the stability and reliability of computer systems. Different implementation strategies can be used. Each algorithm has its own strengths and weaknesses, and the choice of which algorithm to use will depend on the specific needs of the system being examined. Overall, the deadlock detection algorithm plays a crucial role in ensuring the reliability and performance of operating systems in complex computing environments.