E ExamMaster

AP Exams (Advanced Placement) · Operating Systems

Deadlocks

Coffman conditions, wait-for / resource-allocation graphs, Banker's algorithm and the minimum-units bound.

Save holds essay.txt and wants the printer. Backup holds the printer and wants the file. That cycle is deadlock. This lesson names the four Coffman conditions, draws the wait, then runs Banker's algorithm on twelve tape drives — Available 3, safe sequence P1, P0, P2 — and the N(M−1)+1 counting bound.

  • AP Exams (Advanced Placement)
  • Medium level
  • 5 concepts

1Deadlock is a cycle of waiting

maya's save thread holds a lock on essay.txt. A backup process holds the printer. Save needs the printer to print the file; backup needs essay.txt to copy it. Each waits for the other. Neither can run. That standstill is deadlock: a set of processes in which every member is Waiting for a resource another member holds, and no one can make progress.

Starvation is different: one process waits a long time while others keep running. In deadlock nobody in the set runs. The next concepts are the four conditions that must all hold, the picture of the wait, and the arithmetic that refuses an unsafe grant.

Figure. Each holds what the other wants. The two wait-edges form a cycle. That cycle is the deadlock.

The standstill

  1. HoldSave has the file. Backup has the printer.
  2. WaitSave wants the printer. Backup wants the file.
  3. No progressNeither can finish. That pair is deadlocked.

Coding lab. A wait-for cycle runs in the app, with checks on your output.

Save holds the file and wants the printer; backup holds the printer and wants the file. This is
  1. deadlock — a cycle of waits with no progress
  2. starvation — backup will run after a long time
  3. a race on unsaved_count

Each waits for the other. Nobody in the pair runs. Starvation still lets someone else run. A race is a lost increment, not a standstill.

2Coffman's four conditions

Coffman's four conditions are all necessary for deadlock. Mutual exclusion: at least one resource is not shareable — the file lock is held by one process. Hold and wait: a process keeps what it has while asking for more — save keeps the file while asking for the printer. No preemption: the kernel will not yank the file lock away. Circular wait: a cycle of waits, save → backup → save.

Break any one and deadlock cannot form. Make the printer sharable, force save to release the file before asking for the printer, allow preemption of the lock, or impose an order (always lock file before printer) so a cycle cannot appear. The mnemonic MHNC is the four names in that order.

Figure. P0 holds R1 and waits for R2; P1 holds R2 and waits for R1 — a closed wait chain and a deadlock.

How it works

  1. Mutual exclusionAt least one resource admits only one holder at a time — a printer, a mutex, a tape drive.
  2. Hold and waitA process keeps resources already allocated while blocking on a new request.
  3. No preemptionThe OS cannot grab a resource back; only the holding process may release it.
  4. Circular waitP0 waits on P1's resource, P1 waits on P2's, and the chain closes back on P0.
MHNC — break one to prevent deadlock
ConditionPrevention tactic
Mutual exclusionMake the resource sharable when safe (rare in practice)
Hold and waitRequire all resources to be requested at once before execution
No preemptionAllow the OS to preempt resources and roll back
Circular waitImpose a total ordering on resource types
An OS is changed so that when a process waits for a resource, any resources it already holds may be forcibly taken and rolled back. Which Coffman condition does that break?
  1. Mutual exclusion
  2. Hold and wait
  3. No preemption

Forcible take-back is preemption. Hold and wait would be broken by forcing a process to release everything before waiting, not by seizing resources mid-wait.

3The resource-allocation graph

The resource-allocation graph draws processes as circles and resource types as boxes. An assignment edge from a resource to a process means 'this instance is held'. A request edge from a process to a resource means 'this process is waiting'. save → printer and printer → backup and backup → file and file → save is a cycle.

When every resource type has a single instance, a cycle is necessary and sufficient for deadlock. When a type has several instances, a cycle is only a warning — a different instance might still free someone. The wait-for graph in the lab collapses each held resource away and keeps only process-to-process waits; a cycle there is the deadlock among those processes.

Reuse the directed cycle already drawn under Coffman deadlock conditions: process nodes hold request/assignment edges to single-instance resource boxes. A cycle there means deadlock for sure; with multiple instances a cycle is only necessary, not sufficient.

How it works

  1. Draw assignmentsAn arrow from resource R to process P means P currently holds one unit of R.
  2. Draw requestsAn arrow from P to R means P is blocked waiting for R.
  3. Single instanceIf every box has one dot inside, any cycle is a deadlock.
  4. Multiple instancesA cycle may still resolve if another process releases an instance — treat it as a warning, not proof.
A RAG contains a cycle, but one resource type in the cycle has three identical instances and only two are allocated. What follows?
  1. Deadlock is certain — any cycle proves it
  2. Deadlock is impossible — cycles never matter with multiple instances
  3. Deadlock is possible but not proved by the cycle alone

With multiple instances a cycle is only a hint. A third free instance could satisfy one request and break the ring, so you need a safe-state or reduction argument.

4Banker's algorithm and a safe state

Banker's algorithm is avoidance: grant a request only if the resulting state is safe. A state is safe when there exists an order in which every process can finish using what is free now plus what earlier finishers release. Track Allocation (what each process holds), Max (what it might still claim), and Need = Max − Allocation.

The tape-drive walk already on this card uses twelve identical drives and three processes with Max/Allocation (10/5), (4/2), (9/2). Available is 12 − (5+2+2) = 3. Need is 5, 2, 7. P1's need 2 fits in 3, so P1 can run and release 2, Available becomes 5, then P0, then P2. The safe sequence is P1, P0, P2. Unsafe is not already deadlocked — it only means some future request could trap you.

Figure. Safe if some order exists where each process's Need fits in Available plus prior releases — Banker's walks that order.

How it works

  1. Compute NeedNeed[i] = Max[i] − Allocation[i] for each process.
  2. Find a runnable processPick any process whose Need[i] is less than or equal to Available.
  3. Simulate finishAssume it runs to completion and releases Allocation[i]; add those units back to Available.
  4. Repeat or failIf every process can be scheduled this way, the state is safe; if you get stuck, it is unsafe.

Safe state check

Twelve identical tape drives, three processes: Max/allocation (10/5), (4/2), (9/2). Is the state safe?

  • Available = 12 − (5+2+2)3 drives
  • Need = Max − Allocation → 5, 2, 7P1 need 2 ≤ 3
  • Run P1, release 2 → Available5
  • Run P0 (need 5), then P2 (need 7)safe sequence P1, P0, P2

Pro tip. Always compute Need first, then greedily pick any process whose Need fits in Available — the safe sequence need not be unique.

Banker's algorithm refuses a request because the resulting state would be unsafe. Does that mean the processes are in deadlock right now?
  1. Yes — unsafe and deadlocked mean the same thing
  2. No — unsafe means no safe finish order exists if the grant were made; deadlock may not have occurred yet
  3. Only if the resource-allocation graph already has a cycle

Safe/unsafe is forward-looking. The algorithm denies a request that would remove every safe completion order, even when every process is still running.

5Minimum units that guarantee freedom

A counting question asks: N processes, each needing at most M units of one resource, what is the smallest total that guarantees no deadlock? Give every process M−1 units and they can all sit one short. That uses N(M−1) units. One extra unit lets someone finish. The bound is N(M−1)+1.

Three processes, two units each: N=3, M=2, minimum 3×1+1 = 4. With 3 units each process can hold 1 and nobody can take the second. With 4, one spare remains and someone finishes.

Figure. Worst case: each process holds Max−1. One spare unit above that sum lets someone finish and release — so four identical units free three processes that each need two.

How it works

  1. Worst-case holdEach of N processes could hold Max_i − 1 units and still want one more.
  2. Sum the holdsAdd (Max_i − 1) across all processes — that is the worst simultaneous occupation.
  3. Add one spareOne extra unit guarantees someone can finish and release, so no circular wait can persist.

Three processes, two units each

Each of three processes needs at most two units of one resource type. What is the minimum total units that guarantees no deadlock?

  • Sum of (Max_i − 1) = 3 × (2 − 1)3
  • Need strictly more than 3at least 4 units
  • Check: with 4 units, worst hold is 3 and one spare remainssome process can always finish

Pro tip. Memorise the one-liner: N processes each needing M → minimum is N(M−1)+1 identical units.

Four processes each need at most three units of one resource. What is the minimum total that guarantees deadlock freedom?
  1. 9 units
  2. 12 units
  3. 13 units

N(M−1)+1 = 4×2+1 = 9. Twelve would also work but is not minimal; thirteen overshoots the bound.

Notes

  • Deadlock is a cycle of processes each waiting for a resource held by another.
  • Coffman's four conditions are all necessary; break any one to prevent deadlock.
  • A cycle in a single-instance RAG proves deadlock; with multiple instances it does not.
  • Banker's algorithm grants a request only if the resulting state is safe.
  • N processes, max M each, need at least N(M−1)+1 identical units to guarantee freedom.

Formulas

  • Need[i] = Max[i] − Allocation[i].
  • Available = total − sum(Allocation).
  • Minimum units to guarantee no deadlock: N(M-1)+1.

Exam traps & shortcuts

  • Unsafe ≠ deadlocked. It means a future request might not have a safe sequence.
  • With one instance per type, a wait-for cycle is the deadlock.

Reference tables

Max/Allocation (10/5), (4/2), (9/2). Available = 3. Need = 5, 2, 7.

Twelve tape drives
ProcessMaxAllocationNeed
P01055
P1422
P2927

Recap

A cycle of waits is deadlock. Break one Coffman condition. Unsafe ≠ deadlocked.

Deadlock
Every member of the set waits for another member. Nobody runs.
Coffman
MHNC — mutual exclusion, hold-and-wait, no preemption, circular wait.
RAG
Single-instance cycle proves deadlock. Multi-instance cycle is a warning.
Banker
Need = Max − Allocation. Finish anyone who fits, release, repeat.
Bound
N processes, max M each → N(M−1)+1 units guarantee freedom.

Practise Deadlocks

Reading is free and needs no account. Practice, mocks and progress live in the app.

  • A 4-question practice set that ends the chapter
  • 5 quick checks with worked explanations
  • Timed mocks scored with the real marking scheme
  • Readiness tracked per topic, kept on your device
Continue with Google — freeNo card, no trial. Works offline once installed.