Hello! Welcome to the final lesson in our course on Synchronization and Deadlock.
In our previous lesson, we focused on deadlock avoidance by mastering the Banker's algorithm to solve GATE-style problems. You learned how to determine if a system is in a safe state and how to evaluate resource requests to prevent the system from ever entering an unsafe state.
Today, we shift our focus from prevention to cure. We will explore deadlock detection, specifically for systems with multiple instances of resources. This is a common scenario in GATE questions where simply finding a cycle in a graph isn't enough. Your goal for this lesson is to learn and apply the deadlock detection algorithm to confidently solve GATE problems on this topic.
Let's dive in.
The Deadlock Detection Algorithm
In a previous lesson, we discussed Resource Allocation Graphs (RAGs). You'll recall a key rule:
- For single-instance resources, a cycle in the RAG is a necessary and sufficient condition for a deadlock. If there's a cycle, there's a deadlock.
- For multi-instance resources, a cycle is a necessary but not sufficient condition. A cycle might exist, but the system might still be able to find a way to complete all processes.
So, how do we confirm a deadlock in a multi-instance system? We use a deadlock detection algorithm that is very similar to the Banker's safety algorithm you've already mastered.

The main difference is that instead of a Need matrix (based on a declared Max need), the detection algorithm works with the current Request matrix. It answers the question: "Given the resources processes are currently waiting for, is there a deadlock right now?"
The algorithm works as follows:
- Initialize a
Workvector equal to theAvailableresources. Also, create a boolean arrayFinishfor all processes, initialized tofalse. - Find a process
P_isuch thatFinish[i]isfalseand its currentRequest_iis less than or equal toWork. - If no such process exists, go to step 5.
- If such a process
P_iis found, updateWork = Work + Allocation_i, setFinish[i] = true, and go back to step 2. - After the loop terminates, if
Finish[i]isfalsefor any processP_i, it means that process is part of a deadlock. IfFinish[i]istruefor all processes, the system is not in a deadlock.
Applying the Algorithm: A Video Walkthrough
This method is best understood through an example. The following video demonstrates how to translate a multi-instance RAG into the necessary tables (Allocation, Request, Available) and then apply the detection algorithm.
L-4.3: Multi-Instance Resource Allocation Graph with Example | Operating System
This video from 'Gate Smashers' provides an excellent explanation of why a cycle in a multi-instance RAG doesn't guarantee a deadlock. It then walks through the systematic detection algorithm, which is exactly what you need for the GATE exam.
Please watch the video from the beginning to 19:51. Pay close attention to: The Core Idea (00:00 - 01:40): Understand why manual inspection is difficult and a systematic method is needed for multi-instance graphs. The Algorithm in Action (01:40 - 08:24): See how the Allocation, Request, and Available vectors are constructed from the graph and how the algorithm finds a safe sequence, proving there's no deadlock despite a cycle. A Complex GATE Problem (08:24 - 19:51): This is a full-fledged GATE-level problem. Follow the steps carefully as the presenter builds the tables and systematically finds a safe execution sequence.
As you can see, the process is nearly identical to the Banker's safety algorithm. The key is using the right inputs: Allocation, Request, and Available.
Practice Problem 1: Direct Application
Now it's your turn. Let's solve a typical GATE problem. Analyze the following system state and determine if a deadlock exists.
Your Task:
Apply the deadlock detection algorithm to the state shown in the image.
Workstarts asAvailable = [0, 0, 0].- Check if there is any process
P_iwhoseRequestcan be satisfied byWork. - If yes, release its resources (add
Allocation_itoWork) and repeat. - If you can find a sequence that allows all processes to finish, there is no deadlock. Otherwise, there is.
Which option is correct?
Test your understanding!
Let's walk through the solution step-by-step.
Initial State:
Allocation: As in the table.Request: As in the table.Available=[0, 0, 0]Work=[0, 0, 0]Finish=[F, F, F, F, F]
Iteration 1:
- We look for a process
P_iwhereRequest_i <= Work. - P0:
[0,0,0] <= [0,0,0]? Yes. - So, P0 can execute.
Work=Work+Allocation_0=[0,0,0]+[0,1,0]=[0,1,0]Finish=[T, F, F, F, F]
Iteration 2:
Work=[0,1,0]- We look for a new process.
- P1:
[2,0,2] <= [0,1,0]? No. - P2:
[0,0,1] <= [0,1,0]? No. - P3:
[1,0,0] <= [0,1,0]? No. - P4:
[0,0,2] <= [0,1,0]? No.
No other process can run. We are stuck.
Conclusion:
Processes P1, P2, P3, and P4 will never have their requests met. Finish array at the end will have false for P1, P2, P3, and P4. Therefore, the system is in a deadlock state. The correct option is (d).
Practice Problem 2: Conceptual Understanding (GATE 2022)
GATE often includes Multiple Select Questions (MSQs) that test your conceptual clarity. Let's analyze a question from GATE CSE 2022.
Operating System: GATE CSE 2022 | Question: 16
This page on GateOverflow discusses a real GATE 2022 question about deadlocks. The community answers provide excellent, detailed explanations for each statement, which will solidify your understanding of the nuances.
First, read the question itself. Then, read through the top-voted answers from users 'Kanwae Kan', 'Abhrajyoti00', and 'https_guru'. They do a great job of breaking down each statement.
Now, let's analyze each statement from the question together:
Statement A: "Circular wait is a necessary condition for the formation of deadlock."
- Analysis: This is TRUE. As you know, there are four necessary conditions for deadlock: Mutual Exclusion, Hold and Wait, No Preemption, and Circular Wait. All four must be present for a deadlock to occur.
Statement B: "In a system where each resource has more than one instance, a cycle in its wait-for graph indicates the presence of a deadlock."
- Analysis: This is FALSE. This is the core concept of today's lesson.
- Technically, a "wait-for graph" is a simplified RAG used for single-instance resources. It's not applicable to multi-instance systems.
- Even if we consider a cycle in the full RAG, it is not sufficient to prove deadlock in a multi-instance system. The first example in the
Gate Smashersvideo (LINK) showed a system with a cycle that was not deadlocked.
Statement C: "If the current allocation of resources to processes leads the system to unsafe state, then deadlock will necessarily occur."
- Analysis: This is FALSE. An unsafe state means that the OS cannot guarantee that deadlock won't happen. It means there is a possibility of deadlock. However, a process might release its resources earlier than its declared maximum need, allowing the system to recover and find an execution path. All deadlocked states are unsafe, but not all unsafe states lead to a deadlock.
Statement D: "In the resource-allocation graph of a system, if every edge is an assignment edge, then the system is not in deadlock state."
- Analysis: This is TRUE. An assignment edge (
R -> P) means a resource is held. A request edge (P -> R) means a process is waiting. If there are no request edges, no process is waiting for any resource. This breaks the "Hold and Wait" and "Circular Wait" conditions. Therefore, no deadlock can exist.
Conclusion
Congratulations on completing this course! You have covered the most critical GATE topics in process synchronization and deadlock, from foundational concepts to advanced problem-solving.
Key Takeaways from this Lesson:
- The deadlock detection algorithm for multi-instance systems is procedurally similar to the Banker's safety algorithm but uses the current
Requestmatrix. - A system is deadlocked if and only if the detection algorithm terminates with some processes having
Finish[i] = false. - A cycle in a multi-instance RAG is a necessary but not sufficient condition for deadlock. You must run the detection algorithm to be sure.
- Remember the crucial distinction: an unsafe state is a potential for deadlock, while a deadlocked state is an actual standstill.
You have built a strong foundation. The key to success in the GATE exam is now consistent practice. Revisit these problems, solve more from past papers, and focus on both speed and accuracy.
Best of luck with your GATE preparation