A deadlock is a situation where a set of processes are blocked because each process is holding a resource and waiting for a resource held by another process in the set. None of the processes can proceed.
Classic Example: Two processes P1 and P2, two resources R1 and R2:
All four conditions must hold simultaneously for a deadlock to occur (Coffman conditions):
| Condition | Description |
|---|---|
| Mutual Exclusion | At least one resource must be held in a non-shareable mode. Only one process can use the resource at a time. |
| Hold and Wait | A process must be holding at least one resource and waiting to acquire additional resources held by other processes. |
| No Preemption | Resources cannot be forcibly taken from a process. A process must voluntarily release resources. |
| Circular Wait | A circular chain of processes exists where each process holds a resource needed by the next process in the chain. |
A Resource Allocation Graph (RAG) is used to detect deadlocks:
Prevent deadlock by ensuring at least one of the four necessary conditions cannot hold:
The Banker's Algorithm (Dijkstra, 1965) is a deadlock avoidance algorithm. Before granting a resource request, it checks if the resulting state is safe.
A safe state is one where there exists a safe sequence - an ordering of processes such that each process can obtain all its needed resources, execute, and release them, allowing the next process to proceed.
Data structures:
Detection: Allow deadlocks to occur, then detect them using a wait-for graph (simplified RAG). Periodically run a detection algorithm to find cycles.
Recovery:
Master Operating System with these hand-picked resources
Explore 500+ free tutorials across 20+ languages and frameworks.
Fresh tutorials, interview guides, and coding practice in your inbox.