A deadlock in operating systems refers to a situation where a set of processes is unable to proceed because each process is waiting for another to release a resource, and none of them can proceed. This creates a circular wait, where each process is in a state of waiting for something that another process holds. Deadlocks can occur in systems that have limited resources, particularly when multiple processes request and hold resources in an overlapping or conflicting manner.
Understanding and managing deadlocks is a crucial part of designing and operating multi-threaded or multi-process systems, as it can cause the system to become unresponsive or inefficient.
A deadlock can occur in an environment where all the following four conditions hold simultaneously. These conditions are known as the Coffman conditions:
Mutual Exclusion:
Hold and Wait:
No Preemption:
Circular Wait:
These four conditions must all be present for a deadlock to occur. If any one of these conditions is violated, deadlock cannot happen.
Imagine two processes and two resources (e.g., a printer and a scanner):
Both processes are now in a deadlock situation:
Deadlock prevention involves taking steps to ensure that at least one of the Coffman conditions does not hold. By breaking one or more of these conditions, we can prevent deadlocks from occurring.
Prevent Mutual Exclusion:
Prevent Hold and Wait:
Prevent No Preemption:
Prevent Circular Wait:
Deadlock avoidance involves dynamically checking whether granting a resource request will lead to a potential deadlock. This can be done by analyzing the current state of resource allocation and ensuring that each resource request will not lead the system into an unsafe state.
One of the most well-known methods for deadlock avoidance is the Banker's Algorithm, which ensures that the system only grants resource requests if they will not result in a deadlock. The Banker's Algorithm is similar to a "safety check" that examines whether there is a safe sequence of processes that can eventually complete without entering a deadlock state.
The Banker's Algorithm works based on the following concepts:
In the Banker's Algorithm:
When deadlock prevention and avoidance strategies are not feasible or not used, the system must detect deadlocks and recover from them. This involves periodically checking the system for deadlocks and taking actions to resolve them.
The system can detect deadlocks by using a wait-for graph, where:
A cycle in this graph indicates the presence of a deadlock. If no cycles are detected, the system is not in a deadlock state.
Once a deadlock is detected, the system must take steps to recover from it. The two main approaches to recovery are:
Process Termination:
Resource Preemption:
Deadlocks are not limited to single-machine systems. In distributed systems, deadlocks can also occur, but they are more difficult to detect and handle due to the lack of a central coordinator. Distributed deadlock detection involves maintaining information about resource allocation and waiting states across multiple machines, which can be challenging.
Some distributed systems use distributed algorithms to detect and resolve deadlocks by having processes communicate with each other to determine the existence of a cycle or other indicators of deadlock.
Deadlocks represent one of the most challenging problems in concurrent computing, as they can cause a system to come to a halt and reduce its performance. The four Coffman conditions — mutual exclusion, hold and wait, no preemption, and circular wait — must all hold for deadlock to occur.
While deadlocks can be prevented, avoided, or detected, the approach depends on the specific application and system requirements. Deadlock prevention aims to eliminate one of the Coffman conditions, deadlock avoidance uses algorithms like the Banker's Algorithm to ensure a safe system state, and deadlock detection involves periodically checking for cycles in the wait-for graph. Once a deadlock is detected, the system must recover by either terminating processes or preempting resources to break the cycle and allow the system to continue functioning.
Open this section to load past papers