UNIHIVE

Study Hub UNIPORT CSC 394

CSC 394 · Operating Systems

Deadlocks

The four necessary conditions, resource-allocation graphs, prevention, avoidance with the Banker's algorithm (worked), detection and recovery.

Our notes

2 min read

What a deadlock is

A set of processes is deadlocked when each is waiting for a resource held by another in the set, so none can continue. Example: P1 holds the printer and wants the scanner; P2 holds the scanner and wants the printer.

Four necessary conditions (Coffman)

All four must hold at once:

  1. Mutual exclusion: a resource can be used by only one process at a time.
  2. Hold and wait: a process holds resources while waiting for more.
  3. No preemption: resources cannot be taken away; they are released voluntarily.
  4. Circular wait: P1 waits for P2, P2 for P3, …, Pn for P1.

Resource-allocation graph

Circles are processes, boxes are resource types (dots = instances). Request edge P → R; assignment edge R → P. No cycle → no deadlock. A cycle means deadlock if every resource has one instance; with several instances a cycle means deadlock is possible.

Handling deadlocks

  1. Prevention: break one condition: allow sharing where possible; request all resources at the start (no hold and wait); allow preemption; number resource types and request them only in increasing order (no circular wait).
  2. Avoidance: check each request and grant it only if the system stays in a safe state (Banker's algorithm).
  3. Detection and recovery: let deadlocks happen, detect them, then recover by ending processes or taking resources back (rollback).
  4. Ignore (the "ostrich algorithm"): used by most desktop OSs because deadlocks are rare.

Banker's algorithm (worked)

5 processes, 3 resource types A, B, C with totals A = 10, B = 5, C = 7.

Allocation (A B C)Max (A B C)Need = Max − Allocation
P00 1 07 5 37 4 3
P12 0 03 2 21 2 2
P23 0 29 0 26 0 0
P32 1 12 2 20 1 1
P40 0 24 3 34 3 1

Allocated: A 7, B 2, C 5 → Available = (3, 3, 2).
Safety check (find a process whose Need ≤ Work, let it finish, add its Allocation to Work):

  • Work (3,3,2): P1 needs (1,2,2) ✓ → Work = (3,3,2) + (2,0,0) = (5,3,2).
  • P3 needs (0,1,1) ✓ → Work = (7,4,3).
  • P4 needs (4,3,1) ✓ → Work = (7,4,5).
  • P0 needs (7,4,3) ✓ → Work = (7,5,5).
  • P2 needs (6,0,0) ✓ → Work = (10,5,7).

All finish: safe sequence ⟨P1, P3, P4, P0, P2⟩.
A request: P1 asks for (1,0,2). It is ≤ its Need (1,2,2) and ≤ Available (3,3,2). Pretend to grant: Available = (2,3,0), P1 Allocation (3,0,2), Need (0,2,0). Check: P1 (0,2,0) ✓ → (5,3,2); P3 ✓ → (7,4,3); P4 ✓ → (7,4,5); P0 ✓ → (7,5,5); P2 ✓ → (10,5,7). Still safe → grant it.

Detection and recovery

With single-instance resources, look for cycles in a wait-for graph. Recovery: abort all deadlocked processes, or one at a time (cheapest first) until the cycle breaks; or preempt resources and roll a process back.

Exams usually ask: define deadlock; the four conditions; draw and read a resource-allocation graph; prevention methods; run the Banker's algorithm (Need matrix, safe sequence, grant a request); deadlock vs starvation.

Common mistake: in the Banker's algorithm, forgetting to add the finished process's Allocation (not its Need or Max) back to Work.

Written by the UniHive team in our own words. Spotted a mistake? Tell us on WhatsApp.

Read

Watch

We link to free material and never copy it. Every link was opened by our team before it went up; video data is a rough guide at 360p.

All CSC 394 topics

Report

Sign in to report this. It is free.

Is this your work (a lecturer, author or publisher)? Ask us to take it down.