Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Deadlocks

Overview

A deadlock is a situation where two or more processes are permanently blocked, each waiting for a resource held by another. None can proceed, and none will ever release their resources. Deadlocks are one of the most serious concurrency bugs.

The Deadlock Example

Thread A:                      Thread B:
lock(mutex1)  ✓                lock(mutex2)  ✓
lock(mutex2)  BLOCKED ←→      lock(mutex1)  BLOCKED

Both threads wait forever.

Four Necessary Conditions (Coffman Conditions)

All four must hold simultaneously for deadlock:

ConditionDescriptionExample
Mutual ExclusionOnly one process can use a resource at a timeMutex, printer
Hold and WaitProcess holds resource while waiting for anotherHolding mutex1, waiting for mutex2
No PreemptionResources cannot be forcibly taken awayCan’t force a thread to release mutex
Circular WaitCircular chain of processes waiting for each otherA waits for B, B waits for A

Resource Allocation Graph

graph LR
    P1[Process 1] -->|requests| R2[Resource 2]
    R1[Resource 1] -->|assigned to| P1
    P2[Process 2] -->|requests| R1
    R2 -->|assigned to| P2
    
    style P1 fill:#ff9999
    style P2 fill:#ff9999

Cycle in graph = potential deadlock (with single-instance resources, cycle = deadlock).

Strategies for Handling Deadlocks

StrategyDescriptionCost
PreventionEliminate one of the four conditionsRestrict programming
AvoidanceDynamically check before granting (Banker’s)Runtime overhead
Detection + RecoveryLet deadlocks happen, detect and fixRecovery cost
Ignore (Ostrich)Pretend deadlocks don’t happenRisk of deadlock

Chapter Contents

Interview Quick Facts

  1. Deadlock requires ALL four conditions: mutual exclusion, hold-and-wait, no preemption, circular wait
  2. Prevention: break any one condition
  3. Avoidance: Banker’s algorithm checks if granting a request leads to unsafe state
  4. Detection: find cycles in wait-for graph
  5. Recovery: kill processes or preempt resources

Diagram: Deadlock Strategies

graph TD
    A[Deadlock Handling] --> B[Prevention<br>Break one condition]
    A --> C[Avoidance<br>Banker's algorithm]
    A --> D[Detection + Recovery<br>Wait-for graph]
    A --> E[Ignore<br>Ostrich algorithm]
    
    B --> B1[Break mutual exclusion<br>Spooling]
    B --> B2[Break hold-and-wait<br>Request all at once]
    B --> B3[Break no preemption<br>Force release]
    B --> B4[Break circular wait<br>Order resources]

Cross-References

Cross References