If a system neither prevents nor avoids deadlocks, it must be able to find them when they occur and to recover.

Detection with Single Instances

If every resource type has only one instance, a wait-for graph is used. It is formed from the resource-allocation graph by removing the resource nodes and joining the edges: an edge Pi → Pj means that Pi is waiting for a resource held by Pj. A deadlock exists if and only if the wait-for graph contains a cycle. The system keeps the graph and periodically runs a cycle-detection algorithm, which takes O(n²) time for n processes.

Definition: Wait-for graph. A directed graph in which each node is a process and an edge from Pi to Pj means that Pi is waiting for a resource held by Pj; a cycle indicates deadlock.

Detection with Several Instances

When resource types have several instances, a detection algorithm similar to the Banker's safety algorithm is used, with Request in place of Need. Work = Available, and Finish[i] = true for any process holding no resources. Find i with Finish[i] false and Request[i] ≤ Work; then Work = Work + Allocation[i], Finish[i] = true. Repeat. Any process left with Finish[i] = false is deadlocked. The question of how often to run the algorithm depends on how often deadlocks occur: running it on every request is costly, so it is usually run at intervals or when CPU utilisation drops.

Recovery from Deadlock

Process termination: either abort all the deadlocked processes, which is quick but loses much work, or abort one at a time until the cycle is broken, running detection after each. The victim is chosen by factors such as priority, how long it has run, how much work remains, and the resources it holds. Resource preemption: take resources from some processes and give them to others. This raises three issues: selecting a victim to minimise cost, rollback, returning the victim to a safe earlier state, perhaps by restarting it, and starvation, since the same process might be chosen every time, which is avoided by counting rollbacks in the cost.

Table 3.7: Approaches to deadlock

Approach Idea Cost
Prevention Make a condition impossible Low utilisation
Avoidance Grant only safe requests Needs maximum needs in advance
Detection and recovery Find and break deadlocks Lost work, overhead
Ignore Assume deadlocks are rare Occasional manual restart

Example 3.7: Detecting with a wait-for graph

Problem: P1 waits for P2, P2 waits for P3, P3 waits for P4, and P4 waits for P2. P5 waits for P1. Which processes are deadlocked?

Solution:

  1. Edges: P1 → P2, P2 → P3, P3 → P4, P4 → P2, P5 → P1.
  2. The cycle is P2 → P3 → P4 → P2.
  3. P1 and P5 are not in the cycle, but they wait on it, so they cannot proceed until it is broken.

Result: P2, P3 and P4 are deadlocked, and P1 and P5 are blocked behind them.

#include <stdio.h>
int main(void) {
    int wf[6] = {-1, 2, 3, 4, 2, 1};
    /* wf[i]: Pi waits for P(wf[i]) */
    for (int s = 1; s <= 5; s++) {
        int p = wf[s], steps = 0;
        while (p != -1 && p != s && steps++ < 6) p = wf[p];
        if (p == s) printf("P%d is part of a deadlock cycle\n", s);
    }
    return 0;
}

Output

P2 is part of a deadlock cycle
P3 is part of a deadlock cycle
P4 is part of a deadlock cycle

Common mistakes

  • Running detection after every request, which costs too much time.
  • Aborting the same victim again and again, so that it starves.
  • Thinking that recovery is free. Aborted processes lose their work.

Key takeaways

  • With single instances, deadlock means a cycle in the wait-for graph.
  • With several instances, a Banker-like detection algorithm uses Request.
  • Recovery is by terminating processes or preempting resources.
  • Victim choice must avoid starvation.

Practice questions

  1. How is a wait-for graph formed?
  2. Name two methods of recovery from deadlock.
  3. What issues arise with resource preemption?

Answers to the practice questions

  1. From the resource-allocation graph by removing resource nodes and joining the edges between processes.
  2. Terminating processes and preempting resources.
  3. Selecting a victim, rollback and starvation.

Sources

  • Official BCA syllabus Operating Systems, course code 25BCA402DS03, Unit III