Sub topic 5
Four Ways to Handle Deadlock
This chapter assumes you understand processes, threads, locks or semaphores, directed-graph cycles, and basic vector and matrix comparisons. The goal is to choose a system policy before or after processes compete for resources.
A deadlock occurs when a group of processes waits forever for resources held by other processes in the same group. Handling deadlock means deciding whether to prevent it, avoid it, detect it after it occurs, or accept it as an uncommon risk.
The four strategies
| Strategy | Main idea | Cost or limitation |
|---|---|---|
| Prevention | Make at least one deadlock condition impossible | Can reduce concurrency or require restrictive rules |
| Avoidance | Grant a request only if the resulting state remains safe | Requires maximum-claim information and runtime checks |
| Detection and recovery | Allow deadlocks, find them, then break them | Work is wasted and recovery can be expensive |
| Ignoring | Take no special action | A deadlock may remain until users or administrators intervene |
Prevention
Deadlock prevention changes the rules of resource use so that at least one of the four Coffman conditions cannot hold. The conditions are mutual exclusion, hold and wait, no preemption, and circular wait.
For example, a system can require a process to request every resource before it starts. The process cannot hold one resource while waiting for another, so the hold-and-wait condition is broken. This rule can waste resources because a process may reserve resources long before it needs them.
Another option is to impose a global order on resource types. A process must acquire resources only in increasing order, such as disk before printer before network device. A circular wait cannot form because every waiting edge follows the same order.
Prevention usually needs little information about future requests. Its disadvantage is reduced flexibility. A rule that prevents deadlock may cause longer waits, lower resource utilization, or unnecessary preemption.
Avoidance
Avoidance examines each request before granting it. The operating system temporarily considers the grant and asks whether the resulting state is safe.
A safe state has at least one order in which every process can obtain its remaining resources, finish, and release what it holds. An unsafe state has no such guaranteed order. Unsafe does not always mean that deadlock already exists, but it means the system can no longer guarantee deadlock freedom.
The Banker's algorithm is the standard multiple-instance avoidance method. It requires each process to declare a maximum claim. The system grants a request only when:
- The request does not exceed the process's remaining declared need.
- The resources are currently available.
- The tentative grant leaves the system in a safe state.
Avoidance can preserve more concurrency than strict prevention, but it adds checking overhead and depends on trustworthy maximum claims. It is unsuitable when processes cannot state their future needs.
Detection and recovery
A system may allow requests freely and periodically check for deadlock. For one instance of each resource type, a cycle in the wait-for graph identifies deadlock. With multiple instances, the system uses a matrix-based detection algorithm.
After detection, recovery must break the cycle. The system can terminate one or more processes, or preempt resources from selected processes. Termination is simple but loses work. Preemption may preserve more work, but it needs rollback and can repeatedly choose the same victim, causing starvation.
Ignoring deadlock
Some general-purpose systems ignore deadlock for particular resources when deadlocks are rare and prevention or detection costs more than occasional manual recovery. This choice does not make deadlock impossible. It accepts the risk as an engineering trade-off.
Exercise
A system cannot obtain maximum resource claims, but it can enforce a fixed order for acquiring locks. Which strategy is most suitable?
Show answer
Prevention. The fixed order breaks the circular-wait condition without requiring future-claim information.