Sub topic 13
Recovery from Deadlock: Terminating Processes and Preempting Resources
Assume you know processes, resource instances, directed graphs, and the deadlock-detection algorithms for single-instance and multiple-instance resources. This chapter focuses on what an operating system does after it detects a deadlock.
Why recovery is necessary
A detected deadlock does not disappear by itself. Each process in the deadlock waits for a resource held by another process in the same cycle or waiting set. The operating system must break at least one waiting relationship.
Recovery has two main approaches:
- Terminate one or more processes.
- Preempt, or forcibly take back, resources from selected processes.
Both approaches can cause lost work. The operating system therefore chooses a victim carefully.
Process termination
The simplest recovery method is to terminate processes and release all resources they hold.
Abort every deadlocked process
The operating system can terminate every process in the detected deadlock. This immediately releases all their resources and guarantees that the deadlock is removed.
This method is simple, but expensive. It discards all work performed by every terminated process. It may also damage user data if processes were updating files or other shared state.
Abort one process at a time
A less destructive method is to terminate one process, run the detection algorithm again, and repeat only if the deadlock remains.
The operating system can choose a victim using factors such as:
- Process priority.
- Amount of work already completed.
- Resources currently held.
- Resources still required.
- Cost of restarting the process.
- Whether the process is interactive or a background task.
- Number of other processes that become unblocked.
The goal is to remove the deadlock at the lowest total cost. Terminating one process may release enough resources to let the other processes finish.
Resource preemption
Instead of terminating a process, the operating system may take a resource from one process and give it to another. The interrupted process is later resumed.
Preemption requires three decisions:
- Select a victim. Choose a process and resource whose removal has a low cost.
- Roll back the victim. Restore the victim to a previous safe checkpoint, or otherwise make its state consistent with losing the resource.
- Prevent starvation. Ensure that the same process is not always selected as the victim.
Preemption works well for resources that can be saved and restored, such as memory pages or computation state. It is unsafe or difficult for resources such as a printer that has already produced part of a document. The operating system must not duplicate or corrupt externally visible effects.
Choosing between the methods
| Method | Main benefit | Main cost |
|---|---|---|
| Terminate all victims | Guaranteed and simple recovery | Loses all work in the deadlocked group |
| Terminate one victim | Limits lost work | Requires repeated detection and victim selection |
| Preempt a resource | Preserves the process | Needs rollback and can cause starvation |
A practical system may combine both methods. It can first attempt resource preemption, then terminate a process if preemption is impossible or too costly.
Worked decision
Suppose processes P1 and P2 are deadlocked. P1 holds a resource needed by P2, while P2 holds a resource needed by P1. If P1 has performed little work and can restart cheaply, terminating P1 is a reasonable choice. Its resources are released, allowing P2 to finish. If P1 has performed a long computation and has a checkpoint, preempting a resource and rolling P1 back may cost less.
Exercise
Why does terminating only one process often remove a deadlock?
Show answer
Terminating that process releases every resource it holds. Another deadlocked process may then obtain the needed resource, finish, and release more resources, breaking the circular wait.