First published on CodeKairo, where the figures in this note are animated step by step.

In short: A deadlock is a state in which a set of processes are all blocked, each holding at least one resource and waiting for a resource held by another process in the set, so none of them can ever continue. It can occur only when four conditions hold together: mutual exclusion, hold and wait, no preemption and circular wait. Operating systems handle it by prevention, avoidance, detection and recovery, or by ignoring it.

A deadlock is a standstill: a set of processes in which every process is waiting for something that only another process in the set can provide. Process P1 holds the printer and wants the scanner; P2 holds the scanner and wants the printer. Neither will release what it has, so both wait forever. Deadlocks come from ordinary, correct-looking code that grabs locks or devices in different orders, which is why every operating systems interview asks about them and many ask you to run the Banker's algorithm on paper.

The system model

Resources come in types (CPU cycles, memory space, files, locks, printers), and each type has one or more identical instances. A process uses a resource in three steps: request it (waiting if it is not available), use it, and release it. Deadlock is a circle of processes stuck at the request step.

The four necessary conditions

Coffman and his colleagues showed in 1971 that a deadlock can arise only if four conditions hold at the same time:

  1. Mutual exclusion. At least one resource is non-shareable: only one process can use it at a time.
  2. Hold and wait. A process holds at least one resource while waiting for others.
  3. No preemption. A resource cannot be taken from a process; it is released only voluntarily.
  4. Circular wait. There is a cycle P0 → P1 → … → Pn → P0 in which each process waits for a resource held by the next.

These are necessary conditions: remove any one and deadlock is impossible.

The resource allocation graph

A resource allocation graph (RAG) shows the state at one moment. Processes are circles, resource types are rectangles with one dot per instance, a request edge P → R means P is waiting for R, and an assignment edge R → P means an instance of R is held by P.

  • No cycle: no deadlock.
  • A cycle, and every resource in it has one instance: deadlock.
  • A cycle with multi-instance resources: possibly a deadlock, possibly not.

When every resource has a single instance, the graph can be collapsed into a wait-for graph with only processes: an edge Pi → Pj means Pi waits for a resource Pj holds. Then a deadlock exists if and only if the wait-for graph has a cycle.

Figure: A cycle of single-instance resources is a deadlock

  1. P1 holds R2 and waits for R1; P2 holds R1 and waits for R2. Following the edges gives the cycle P1 → R1 → P2 → R2 → P1, and each resource has one instance, so both processes wait for ever.
  2. With single instances, drop the resource boxes: P waits for Q whenever P requests something Q holds. The wait-for graph has the cycle P1 → P2 → P1, and a cycle there means deadlock, no more and no less.

▶ Watch this figure animated, step by step

With several instances per resource, a process outside the cycle can finish and free an instance, which breaks the cycle:

Figure: A cycle without a deadlock: resources with two instances

  1. The cycle P1 → R1 → P3 → R2 → P1 is there, but R1 and R2 have two instances each, and P2 and P4 hold the others while waiting for nothing. No instance is free, so the detection algorithm looks for a process whose request can be met.
  2. P2: it requests nothing, so it can run to the end and release its R1.
  3. P1: its request for R1 can be met, so it can run to the end and release its R2.
  4. P3: its request for R2 can be met, so it can run to the end and release its R1.
  5. P4: it requests nothing, so it can run to the end and release its R2. Every process finished, so there is no deadlock: with several instances a cycle is necessary but not sufficient.

Handling deadlocks

Strategy Idea Cost
Prevention Make one of the four conditions impossible Lower resource utilization, awkward programming rules
Avoidance Grant a request only if the system stays safe Needs every process's maximum claim in advance
Detection and recovery Let deadlocks happen, find them, break them Detection overhead, lost work on recovery
Ignore it Assume deadlocks are rare (the "ostrich algorithm") Occasional manual restarts

General-purpose systems such as Linux and Windows take the last approach for user processes and leave deadlock handling to applications and databases.

Deadlock prevention

Prevention attacks the conditions one at a time:

Condition How to break it Drawback
Mutual exclusion Make resources shareable: read-only files, spooling for printers Most resources, such as locks, are inherently exclusive
Hold and wait Request every resource at once before starting, or release everything before asking for more Poor utilization; a process needing many popular resources can starve
No preemption If a request cannot be granted, release what you hold, or let the OS take resources from a waiting process Works only for resources whose state can be saved, such as CPU registers and memory
Circular wait Number the resource types and always request them in increasing order Every programmer must follow the order

Ordering is the method used most in practice, and it is provably enough: a cycle would need some process to hold a higher-numbered resource while requesting a lower one, which the rule forbids.

Figure: Two threads, two locks: opposite orders against one order

  1. Thread 1 locks A and thread 2 locks B, each the first lock its code asks for. Nothing has gone wrong yet: both locks were free.
  2. Thread 1 now asks for B, held by thread 2, and thread 2 asks for A, held by thread 1. Each waits for the other: a cycle of two, and neither can ever continue.
  3. Now both lock A before B. Thread 1 gets A; thread 2 asks for A and waits — but it holds nothing, so no one can be waiting for it and no cycle can close.
  4. Thread 1 takes B, works and unlocks both; thread 2 then gets A and B in turn and finishes too. Locking in one global order removes circular wait, so this pair can never deadlock.

Deadlock avoidance

Avoidance needs extra information: each process declares in advance the maximum number of instances of each resource type it may ever need. The OS then grants a request only if the resulting state is safe.

A state is safe if there is a safe sequence: an order of all processes such that each one's remaining need can be met by the currently available resources plus everything released by the processes before it. Then the OS can always finish every process in that order, so no deadlock can happen.

An unsafe state is not a deadlock; it only means the OS can no longer guarantee to avoid one. Avoidance stays out of unsafe states altogether.

For single-instance resources, avoidance can use the RAG with claim edges (dashed P → R edges for future requests): a request is granted only if turning its claim edge into an assignment edge creates no cycle. For multiple instances, it uses the Banker's algorithm.

The Banker's algorithm

Named after a banker who never lends cash in a way that leaves him unable to satisfy all his customers' credit limits. For n processes and m resource types it keeps:

  • Available[m]: free instances of each type.
  • Max[n][m]: each process's declared maximum.
  • Allocation[n][m]: what each process holds now.
  • Need[n][m] = Max − Allocation: what each process may still request.

Safety algorithm.

  1. Set Work = Available and Finish[i] = false for every process.
  2. Find a process i with Finish[i] = false and Need[i] ≤ Work (in every resource type). If none exists, go to step 4.
  3. Pretend it runs to completion: Work = Work + Allocation[i], Finish[i] = true. Go to step 2.
  4. The state is safe if every Finish[i] is true.

Resource-request algorithm, when process i asks for Request[i]:

  1. If Request[i] > Need[i], it is an error: the process asked for more than it declared.
  2. If Request[i] > Available, the process must wait: the resources are not free.
  3. Otherwise pretend to grant it (Available −= Request, Allocation[i] += Request, Need[i] −= Request) and run the safety algorithm. If the new state is safe, grant the request; if not, restore the old state and make the process wait.

The safety check costs O(m × n²) per request.

Worked example: Banker's algorithm

Five processes and three resource types with 8 instances of A, 6 of B and 7 of C.

Process Allocation (A B C) Max (A B C) Need = Max − Allocation
P0 1 1 0 4 3 2 3 2 2
P1 2 0 1 3 2 2 1 2 1
P2 1 2 1 3 4 3 2 2 2
P3 0 1 2 1 3 4 1 2 2
P4 2 0 0 5 2 3 3 2 3
Total allocated 6 4 4

Available = total − allocated = (8 6 7) − (6 4 4) = (2 2 3).

Is the state safe? Each step picks the lowest-numbered unfinished process whose Need fits in Work:

Figure: Banker's algorithm: finding a safe sequence — Available = (2 2 3), total = (8 6 7)

  1. Need = Max − Allocation, row by row, and Work starts as Available = total (8 6 7) − allocated = (2 2 3). The algorithm repeatedly looks for an unfinished process whose whole Need fits in Work.
  2. P0 needs 3 of A but Work has 2. P1's Need (1 2 1) fits in Work (2 2 3): it can finish and release its Allocation, so Work becomes (4 2 4).
  3. P0's Need (3 2 2) fits in Work (4 2 4): it can finish and release its Allocation, so Work becomes (5 3 4).
  4. P2's Need (2 2 2) fits in Work (5 3 4): it can finish and release its Allocation, so Work becomes (6 5 5).
  5. P3's Need (1 2 2) fits in Work (6 5 5): it can finish and release its Allocation, so Work becomes (6 6 7).
  6. P4's Need (3 2 3) fits in Work (6 6 7): it can finish and release its Allocation, so Work becomes (8 6 7). Every process finished: the state is safe, and Work is back to the total.

Every process finishes, so the state is safe with the sequence P1, P0, P2, P3, P4. The final Work equals the total (8 6 7), a useful check. Other safe sequences exist too, such as P1, P3, P0, P2, P4; any one proves safety.

Request 1: P4 asks for (1 0 1). It is within P4's Need (3 2 3) and Available (2 2 3), and after pretending to grant it the state is still safe, so it is granted.

Request 2: P0 asks for (1 2 0), starting again from the original state. The resources are physically free, but after pretending to grant it Available would be (1 0 3) and no process's Need fits. The state would be unsafe, so the request is denied and P0 waits, even though the resources were there.

Figure: Banker's algorithm: two requests, one granted and one refused — Available = (2 2 3) before either request

  1. P4 asks for (1 0 1): within its Need and within Available. Pretend to grant it — Available becomes (1 2 2), P4's Need (2 2 2) — and rerun the safety check: P1, P0, P2, P3, P4 all finish, so the request is granted.
  2. P0 asks for (1 2 0), also within its Need and Available. Pretend to grant it: Available becomes (1 0 3). Now no Need fits — P0 wants 2 of A with 1 free, and every other process wants B, which has none — so the state would be unsafe and P0 must wait.

Two more requests show the first two checks: P4 asking for (3 0 0) must wait, because only 2 of A are available; P1 asking for (2 0 0) is an error, because its Need for A is only 1.

This program runs the same checks and prints the same answers:

C++

#include <cstdio> #include <string> #include <vector> using namespace std; using Matrix = vector<vector<int>>; const Matrix MAX = {{4, 3, 2}, {3, 2, 2}, {3, 4, 3}, {1, 3, 4}, {5, 2, 3}}; const Matrix ALLOCATION = {{1, 1, 0}, {2, 0, 1}, {1, 2, 1}, {0, 1, 2}, {2, 0, 0}}; const vector<int> AVAILABLE = {2, 2, 3}; const int N = 5, M = 3; bool fits(const vector<int>& a, const vector<int>& b) { for (int j = 0; j < M; j++) if (a[j] > b[j]) return false; return true; } vector<int> need(const Matrix& alloc, int i) { vector<int> row(M); for (int j = 0; j < M; j++) row[j] = MAX[i][j] - alloc[i][j]; return row; } string join(const vector<int>& v) { string s; for (int j = 0; j < (int)v.size(); j++) s += (j ? " " : "") + to_string(v[j]); return s; } // Fills order with a safe sequence; returns false if the state is unsafe. bool safeSequence(const Matrix& alloc, vector<int> work, string& order) { vector<bool> finished(N, false); for (int done = 0; done < N; done++) { int pick = -1; // lowest-numbered unfinished process whose need fits in work for (int i = 0; i < N && pick == -1; i++) if (!finished[i] && fits(need(alloc, i), work)) pick = i; if (pick == -1) return false; for (int j = 0; j < M; j++) work[j] += alloc[pick][j]; // it finishes and releases finished[pick] = true; order += (done ? " P" : "P") + to_string(pick); } return true; } void request(int p, const vector<int>& req) { string label = "P" + to_string(p) + " requests " + join(req) + ": "; if (!fits(req, need(ALLOCATION, p))) { printf("%serror, more than its declared maximum\n", label.c_str()); return; } if (!fits(req, AVAILABLE)) { printf("%smust wait, not enough available\n", label.c_str()); return; } Matrix alloc = ALLOCATION; vector<int> avail = AVAILABLE; for (int j = 0; j < M; j++) { alloc[p][j] += req[j]; avail[j] -= req[j]; } string order; if (safeSequence(alloc, avail, order)) printf("%sgranted, safe sequence %s\n", label.c_str(), order.c_str()); else printf("%sdenied, the state would be unsafe\n", label.c_str()); } int main() { string rows; for (int i = 0; i < N; i++) rows += (i ? " | P" : "P") + to_string(i) + " " + join(need(ALLOCATION, i)); printf("Need: %s\n", rows.c_str()); string order; if (safeSequence(ALLOCATION, AVAILABLE, order)) printf("Initial state is safe: %s\n", order.c_str()); else printf("Initial state is unsafe\n"); request(4, {1, 0, 1}); request(0, {1, 2, 0}); request(4, {3, 0, 0}); request(1, {2, 0, 0}); return 0; } 

Java

import java.util.*; public class Main { static final int[][] MAX = {{4, 3, 2}, {3, 2, 2}, {3, 4, 3}, {1, 3, 4}, {5, 2, 3}}; static final int[][] ALLOCATION = {{1, 1, 0}, {2, 0, 1}, {1, 2, 1}, {0, 1, 2}, {2, 0, 0}}; static final int[] AVAILABLE = {2, 2, 3}; static final int N = MAX.length, M = AVAILABLE.length; static boolean fits(int[] a, int[] b) { for (int j = 0; j < M; j++) if (a[j] > b[j]) return false; return true; } static int[] need(int[][] alloc, int i) { int[] row = new int[M]; for (int j = 0; j < M; j++) row[j] = MAX[i][j] - alloc[i][j]; return row; } // Returns a safe sequence, or null if the state is unsafe. static List<String> safeSequence(int[][] alloc, int[] avail) { int[] work = avail.clone(); boolean[] finished = new boolean[N]; List<String> order = new ArrayList<>(); while (order.size() < N) { int pick = -1; // lowest-numbered unfinished process whose need fits in work for (int i = 0; i < N && pick == -1; i++) if (!finished[i] && fits(need(alloc, i), work)) pick = i; if (pick == -1) return null; for (int j = 0; j < M; j++) work[j] += alloc[pick][j]; // it finishes and releases finished[pick] = true; order.add("P" + pick); } return order; } static String join(int[] v) { StringBuilder sb = new StringBuilder(); for (int j = 0; j < v.length; j++) sb.append(j > 0 ? " " : "").append(v[j]); return sb.toString(); } static void request(int p, int[] req) { String label = "P" + p + " requests " + join(req) + ": "; if (!fits(req, need(ALLOCATION, p))) { System.out.println(label + "error, more than its declared maximum"); ret...
Source:https://dev.to/codekairo/deadlock-in-os-conditions-and-bankers-algorithm-12na