A deadlock is a state in which a set of processes are all blocked, each waiting for a resource held by another process in the set, so none can ever continue.

System Model

A system has resources of several types, such as CPU cycles, memory, printers and files, and each type may have several identical instances. A process uses a resource in three steps: request it, waiting if it is not available, use it, and release it. Requests and releases are made through system calls or semaphore operations.

Definition: Deadlock. A state in which each process of a set is waiting for an event, such as the release of a resource, that can only be caused by another process of the same set, so that none of them can proceed.

Necessary Conditions

A deadlock can occur only if all four of the following hold at the same time (Coffman's conditions). Mutual exclusion: at least one resource is held in a non-shareable mode. Hold and wait: a process holds at least one resource while waiting for others. No preemption: a resource cannot be taken from a process; it is released only voluntarily. Circular wait: there is a chain of processes P0, P1, …, Pn in which each waits for a resource held by the next, and Pn waits for one held by P0.

Resource-Allocation Graph

Deadlocks can be shown with a resource-allocation graph (RAG). Processes are drawn as circles and resource types as rectangles, with a dot for each instance. A request edge P → R means that P has requested R. An assignment edge R → P means that an instance of R is held by P. If the graph has no cycle, there is no deadlock. If there is a cycle and each resource has one instance, there is a deadlock. If resources have several instances, a cycle means that a deadlock is possible but not certain.

Methods of Handling Deadlocks

There are four approaches: prevention, ensuring that one of the four conditions can never hold; avoidance, using advance information to keep the system in a safe state; detection and recovery, allowing deadlocks and then breaking them; and ignoring the problem, as most general-purpose systems such as Windows and Linux largely do, since deadlocks are rare and prevention is costly.

Table 3.5: Coffman's conditions

Condition Meaning Example
Mutual exclusion Resource not shareable Printer
Hold and wait Hold one, wait for another Holds disk, wants printer
No preemption Cannot be taken away Printer mid-job
Circular wait Cycle of waits P1 waits P2, P2 waits P1

Example 3.5: Reading a resource-allocation graph

Problem: R1 and R2 each have one instance. R1 is assigned to P1, P1 requests R2, R2 is assigned to P2, and P2 requests R1. Is there a deadlock?

Solution:

  1. Edges: R1 → P1, P1 → R2, R2 → P2, P2 → R1.
  2. These form a cycle: P1 → R2 → P2 → R1 → P1.
  3. Each resource has a single instance, so the cycle means deadlock.

Result: P1 and P2 are deadlocked.

#include <stdio.h>
int main(void) {
    /* wait-for graph: wf[i] = process that i waits for, -1 if none */
    int wf[] = {1, 2, 0, -1};
    for (int s = 0; s < 4; s++) {
        int p = s, steps = 0;
        while (p != -1 && steps < 4) {
            p = wf[p];
            steps++;
            if (p == s) break;
        }
        printf("P%d %s\n", s, p == s ? "is in a deadlock cycle"
                                    : "is not deadlocked");
    }
    return 0;
}

Output

P0 is in a deadlock cycle
P1 is in a deadlock cycle
P2 is in a deadlock cycle
P3 is not deadlocked

Common mistakes

  • Thinking that any one of the four conditions alone causes deadlock. All four must hold.
  • Concluding that a cycle always means deadlock when resources have several instances.
  • Confusing deadlock with starvation. In starvation, the system still makes progress.

Key takeaways

  • A process requests, uses and releases a resource.
  • Deadlock needs mutual exclusion, hold and wait, no preemption and circular wait together.
  • In a RAG with single instances, a cycle means deadlock.
  • Deadlocks are handled by prevention, avoidance, detection and recovery, or ignoring.

Practice questions

  1. Name the four necessary conditions for deadlock.
  2. What does a cycle in a RAG mean when every resource has one instance?
  3. What is a request edge?

Answers to the practice questions

  1. Mutual exclusion, hold and wait, no preemption and circular wait.
  2. A deadlock exists.
  3. An edge P → R meaning that process P has requested resource R.

Sources

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