# Deadlocks

> Learn the four Coffman conditions, resource allocation graphs, and the strategies of prevention, avoidance with the Banker's algorithm, detection and recovery....

Canonical: https://www.internhack.in/roadmaps/tracks/cs-fundamentals/deadlocks
Last updated: 2026-11-28

Learn the four Coffman conditions, resource allocation graphs, and the strategies of prevention, avoidance with the Banker's algorithm, detection and recovery.

Intermediate · about 4 hours · difficulty 3/5

Learn first: [Concurrency and Synchronization](https://www.internhack.in/roadmaps/tracks/cs-fundamentals/concurrency-synchronization)

A **deadlock** is a set of processes each waiting for a resource held by another, so none can proceed. It is a favourite interview topic because it links theory (conditions, algorithms) with practical coding habits.

### The four necessary conditions (all must hold)

- **Mutual exclusion**: a resource can be held by only one process.
- **Hold and wait**: a process holds one resource while waiting for another.
- **No preemption**: resources cannot be forcibly taken away.
- **Circular wait**: a cycle of processes each waiting on the next.

Break any one and deadlock cannot happen.

### Handling strategies

- **Prevention**: design so a condition never holds. The most practical is **lock ordering** (always acquire locks in a global order), which removes circular wait. Requesting all resources at once removes hold and wait but wastes resources.
- **Avoidance**: grant a request only if the system stays in a **safe state**. The **Banker's algorithm** uses Max, Allocation, Need and Available matrices to find a safe sequence. Practise at least three numericals.
- **Detection and recovery**: allow deadlocks, detect cycles in a **wait-for graph** (a graph cycle problem you already know), then kill or roll back a victim.
- **Ignore it** (the ostrich approach): what most general-purpose OSes do for user resources.

### Related terms

- **Starvation**: a process waits indefinitely but the system still progresses. Not the same as deadlock.
- **Livelock**: processes keep changing state in response to each other without progress.
- A **resource allocation graph** with single-instance resources is deadlocked if and only if it has a cycle.

Good enough to move on: you can list the four conditions, solve a Banker's safe sequence question, and explain how lock ordering prevents deadlock in real code.

## Key concepts

- **Four Coffman conditions**
- **Resource allocation graphs**
- **Deadlock prevention and lock ordering**
- **Banker's algorithm and safe state**
- **Detection and recovery**
- **Starvation vs livelock vs deadlock**

## Mini project

Write a Banker's algorithm program that reads Allocation, Max and Available, prints a safe sequence or reports an unsafe state, and checks whether a new request can be granted. Then create a real two-lock deadlock in code and fix it with lock ordering.

## Check yourself

Which of the four deadlock conditions does consistent lock ordering break, and why is that the most common fix in practice?

## Free resources

- [OSTEP: Common Concurrency Problems](https://pages.cs.wisc.edu/~remzi/OSTEP/) (OSTEP)
- [GeeksforGeeks: Banker's Algorithm](https://www.geeksforgeeks.org/bankers-algorithm-in-operating-system-2/) (GeeksforGeeks)

Previous: [Concurrency and Synchronization](https://www.internhack.in/roadmaps/tracks/cs-fundamentals/concurrency-synchronization) · Next: [Memory Management and Virtual Memory](https://www.internhack.in/roadmaps/tracks/cs-fundamentals/memory-management)

## More in Operating Systems

- [Processes and Threads](https://www.internhack.in/roadmaps/tracks/cs-fundamentals/processes-and-threads)
- [CPU Scheduling](https://www.internhack.in/roadmaps/tracks/cs-fundamentals/cpu-scheduling)
- [Concurrency and Synchronization](https://www.internhack.in/roadmaps/tracks/cs-fundamentals/concurrency-synchronization)
- [Deadlocks](https://www.internhack.in/roadmaps/tracks/cs-fundamentals/deadlocks)
- [Memory Management and Virtual Memory](https://www.internhack.in/roadmaps/tracks/cs-fundamentals/memory-management)
- [File Systems, Storage and I/O](https://www.internhack.in/roadmaps/tracks/cs-fundamentals/file-systems-and-io)

## Related

- [Roadmaps](https://www.internhack.in/roadmaps)
- [CS Fundamentals and Placement Prep Roadmap](https://www.internhack.in/roadmaps/tracks/cs-fundamentals)
