Skip to main content

Welcome to a Grasp lesson

Create a free account to keep studying and create your own study path.

Lesson 18

Deadlock Detection with Multiple-Instance Resources

Recommended resources

Hello! Let's dive into our next lesson on deadlock.

In our last session, we mastered the Banker's Algorithm, a powerful proactive strategy for deadlock avoidance. It ensures a system never enters an unsafe state. However, this strategy relies on knowing maximum resource needs in advance, which isn't always practical.

Today, we shift from prevention to cure. We'll explore a reactive strategy: deadlock detection. We'll focus on systems with multiple instances of resource types, a common scenario in GATE problems. Your goal for this lesson is to learn how to apply the deadlock detection algorithm to determine if a system state is deadlocked.

This algorithm is a close cousin of the Banker's Safety Algorithm you just learned, which will make it very familiar. We will focus on the algorithm's mechanics and the subtle but crucial differences between avoidance and detection.

The Problem with Cycles in Multi-Instance Systems

In the previous module, you learned that for systems with only single-instance resources, a cycle in the Resource Allocation Graph (RAG) is a necessary and sufficient condition for deadlock. If you find a cycle, you've found a deadlock.

However, for systems with multiple instances of a resource type, this rule changes.

A cycle in a multi-instance RAG is a necessary but not a sufficient condition for deadlock.

This means that while a deadlock implies a cycle, a cycle does not guarantee a deadlock. Why? Because a process outside the cycle might finish, release its resources, and allow one of the processes within the cycle to acquire its needed resource, thus breaking the deadlock.

To understand this crucial distinction, please read the introductory rules from the following article.

Resource Allocation Graph | Deadlock Detection

This article from GateVidyalay clearly outlines the rules for deadlock detection using Resource Allocation Graphs, highlighting the key difference between single-instance and multi-instance systems.

Read the sections titled 'Deadlock Detection', 'Rule-01', and 'Rule-02'. Focus on understanding why the presence of a cycle is definitive for single-instance resources but only indicates a potential deadlock for multi-instance resources.

Since we can't rely on just finding a cycle, we need a more robust algorithm.


The Deadlock Detection Algorithm for Multiple Instances

The algorithm to detect a deadlock in a multi-instance system is a variation of the Banker's Safety Algorithm. It determines if the current state is deadlocked by checking if there is a sequence in which all processes can finish.

Let's formalize the algorithm and its data structures (for 'n' processes and 'm' resource types):

  • Available: A vector of length 'm' indicating available resources.
  • Allocation: An n x m matrix showing currently allocated resources.
  • Request: An n x m matrix showing the current requests of each process. Note that we use Request here, not Need as in the Banker's Algorithm.

The algorithm works as follows:

  1. Initialization:

    • Let Work be a vector of length m, initialized to Available.
    • Let Finish be a boolean vector of length n. Initialize Finish[i] to false if Allocation[i] is not all zeros. If Allocation[i] is all zeros (the process holds no resources), set Finish[i] to true, as it cannot be part of a deadlock.
  2. Find a Process: Find a process Pi for which both of the following are true:

    • Finish[i] == false
    • Request[i] <= Work (vector comparison)
  3. If no such process exists: Go to Step 5.

  4. Simulate Execution: If such a process Pi is found:

    • Work = Work + Allocation[i] (Assume Pi finishes and releases its resources).
    • Finish[i] = true.
    • Go back to Step 2.
  5. Conclusion: After the algorithm completes, check the Finish vector.

    • If Finish[i] == false for any process Pi, the system is in a deadlocked state. The set of processes for which Finish is false are the deadlocked processes.
    • If Finish[i] == true for all i, the system is not in a deadlock.

Key Differences from Banker's Safety Algorithm

The algorithm looks very similar, but the context and goal are different. The following resource provides a concise explanation of these differences.

Operating Systems: Deadlocks

This article from the University of Illinois Chicago directly compares the deadlock detection algorithm to the Banker's algorithm, which will help solidify your understanding.

Read the section '7.6.2 Several Instances of a Resource Type'. Pay close attention to the two subtle differences it points out regarding the initialization of the Finish array and the final conclusion.

In short:

  • Banker's (Avoidance) uses the Need matrix to see if a safe future exists.
  • Detection uses the Request matrix to see if the current set of requests can be resolved.

A Worked Example

Now let's see this algorithm in action. The best way to learn it is to watch it being applied to a complex RAG, translating the graph into the required tables and stepping through the logic.

L-4.3: Multi-Instance Resource Allocation Graph with Example | Operating System

This video from 'Gate Smashers' provides an excellent, detailed walkthrough of a GATE-level problem. It starts with a multi-instance RAG and systematically applies the detection algorithm.

Watch the video from 01:40 to 19:51. Focus on these steps: Table Creation (01:40 - 13:20): Observe how the visual RAG is translated into the Allocation and Request matrices. This is a critical first step in many GATE problems. Algorithm Execution (13:20 - 19:51): Follow the iterative process of comparing process requests with the Work vector and updating Work as processes are completed. Notice how a safe execution sequence is derived.

As you saw, by systematically checking if any process can run to completion, we can determine if the system is stuck or not, even if there's a cycle in the RAG.

Test your understanding!

Consider the following system snapshot. Is the system in a deadlock? If not, provide an order in which the processes can finish.

  • Available = [0, 0, 0]
  • Resources: A, B, C
Process Allocation (A,B,C) Request (A,B,C)
P0 (0, 1, 0) (0, 0, 0)
P1 (2, 0, 0) (2, 0, 2)
P2 (3, 0, 3) (0, 0, 1)
P3 (2, 1, 1) (1, 0, 0)
P4 (0, 0, 2) (0, 0, 2)

This problem is adapted from the example in the "Deadlock Detection Algorithm Example" video.

Show answer
  1. Initialization:

    • Work = Available = [0, 0, 0]
    • Finish = [false, false, false, false, false]
  2. Iteration 1:

    • We look for a process Pi where Request[i] <= Work.
    • P0's Request (0,0,0) <= Work(0,0,0). Yes!
    • Execute P0. It finishes and releases its resources.
    • New Work = Work + P0's Allocation = [0,0,0] + [0,1,0] = [0,1,0].
    • Finish = [true, false, false, false, false].
    • Sequence: <P0>
  3. Iteration 2:

    • Now, Work = [0,1,0]. We check the remaining processes.
    • P1's Request (2,0,2) > Work(0,1,0). No.
    • P2's Request (0,0,1) > Work(0,1,0). No (needs C).
    • P3's Request (1,0,0) > Work(0,1,0). No (needs A).
    • P4's Request (0,0,2) > Work(0,1,0). No (needs C).
    • No process can proceed. The algorithm terminates here.
  4. Conclusion:

    • The Finish vector is [true, false, false, false, false].
    • Since Finish is false for P1, P2, P3, and P4, the system is in a deadlocked state. The deadlocked processes are P1, P2, P3, and P4.

Identifying a Deadlocked State

In the exercise above, the algorithm terminated with some processes unfinished, indicating a deadlock. Let's watch a short video that explicitly demonstrates such a scenario, where an additional request pushes a system into a deadlocked state.

Deadlock Detection Algorithm Example - Deadlocks - Operating Systems (KIIT DU)

This video shows a clear example of the detection algorithm identifying a deadlock. It starts from a non-deadlocked state and then modifies a request to show how the system gets stuck.

Watch the segment from 03:17 to 05:31. Notice how after one process (P0) completes, no other process can have its request satisfied by the available resources, leading to the conclusion of a deadlock.

This confirms the final step of the algorithm: if any process remains with Finish = false after the loop terminates, a deadlock exists.


Conclusion

You have now learned how to detect deadlocks in systems with multiple resource instances, a crucial skill for GATE questions.

Key Takeaways:

  • In a multi-instance resource system, a cycle in the RAG is necessary but not sufficient for a deadlock.
  • The deadlock detection algorithm is a variation of the Banker's Safety algorithm. It uses the current Request matrix (not Need) and the Available resources to see if all processes can finish.
  • The algorithm works by simulating the sequential execution of processes whose current requests can be met, and adding their allocated resources back to a Work pool.
  • If the algorithm completes and any process has its Finish flag as false, the system is in a deadlock, and those processes are the ones that are deadlocked.

Preview of the Next Lesson:
You now have the theoretical foundation and have seen the deadlock detection algorithm applied. In our next lesson, we will focus purely on practice. We will tackle a series of GATE-level problems on deadlock detection to sharpen your problem-solving speed and accuracy, preparing you for the exam environment.