What Is a Deadlock?
Learn what a deadlock is in an operating system, the four necessary conditions for its occurrence, and common strategies for prevention and detection.
In depth
A deadlock in an operating system occurs when two or more processes are permanently blocked, each waiting for a resource held by another process in the same set. This leads to a complete standstill, preventing any of the involved processes from completing their execution.
The Four Conditions for Deadlock
For a deadlock to occur, four specific conditions must be met simultaneously. If any one of these conditions can be prevented or broken, a deadlock will not form.
1. Mutual Exclusion: At least one resource must be held in a non-sharable mode, meaning only one process can use it at a time. If another process requests that resource, it must wait until the resource has been released. 2. Hold and Wait: A process must be holding at least one resource and waiting to acquire additional resources that are currently being held by other processes. 3. No Preemption: Resources cannot be forcibly taken away from a process. A resource can only be released voluntarily by the process that is holding it, after that process has completed its task with the resource. 4. Circular Wait: A set of processes {P0, P1, ..., Pn} must exist such that P0 is waiting for a resource held by P1, P1 is waiting for a resource held by P2, ..., Pn-1 is waiting for a resource held by Pn, and Pn is waiting for a resource held by P0. This forms a closed chain of dependencies.
Resource Allocation Graphs
Operating systems often model resource allocation and requests using a Resource Allocation Graph. In this graph:
- Circles represent processes.
- Squares represent resources (e.g., CPU cycles, memory, database locks).
- An arrow from a resource to a process indicates that the resource is currently allocated to that process.
- An arrow from a process to a resource indicates that the process is requesting and waiting for that resource.
A cycle in a resource allocation graph is a necessary and sufficient condition for a deadlock when each resource type has only one instance. If resource types can have multiple instances, a cycle indicates the *possibility* of a deadlock, but not a guarantee.
Deadlock Handling Strategies
Operating systems employ various strategies to manage deadlocks:
- Deadlock Prevention: This involves designing the system to ensure that at least one of the four necessary conditions for deadlock can never hold. For example, by forcing processes to request all their resources at once (breaking Hold and Wait) or by imposing a strict global ordering on resource requests (breaking Circular Wait).
- Deadlock Detection and Recovery: This approach allows deadlocks to occur. The system periodically runs an algorithm to detect cycles in the resource allocation graph. If a deadlock is detected, the system recovers by terminating one or more processes involved in the deadlock, thereby releasing their resources and breaking the cycle.
Key Takeaways
- Deadlocks occur when processes are permanently blocked, each waiting for resources held by others.
- Four conditions (Mutual Exclusion, Hold and Wait, No Preemption, Circular Wait) must all be present for a deadlock.
- Resource Allocation Graphs visualize process-resource relationships and can reveal cycles indicative of deadlocks.
- Deadlocks can be prevented by breaking any of the four conditions or detected and resolved through process termination.
Got a different question? SeaThru generates a fresh video for any topic where systems talk or data structures move.
Ask your own question →