Sub topic 13
Recovery: terminating processes and preempting resources
Prerequisites and scope
This chapter assumes you can already detect a deadlock. You should know the wait-for graph for single-instance resources and the matrix-based detection algorithm for multiple instances. You should also know the four Coffman conditions: mutual exclusion, hold and wait, no preemption, and circular wait.
Detection only reports that a set of processes is deadlocked. It does not free them. Recovery is the step that breaks the deadlock so the system can continue. Recovery has to break at least one Coffman condition for the processes involved. The two standard ways are to terminate processes, which releases everything they hold, and to preempt resources, which takes specific resources away from their holders.
When recovery runs
A system that uses detection and recovery has to decide when to run the detection algorithm. The choice affects how much recovery work there is.
- Running detection on every blocked request finds a deadlock the moment it forms. It also identifies the process whose request closed the cycle. The cost is high, because the multi-instance algorithm takes on the order of m × n² operations for n processes and m resource types.
- Running detection at fixed intervals, or when CPU utilization drops below a threshold, costs less. The risk is that the resource graph may contain many cycles by the time detection runs. Then it is hard to say which process "caused" the deadlock.
Low CPU utilization is a useful trigger because a deadlock removes processes from the runnable set. Utilization falls even when the deadlocked processes are few.
Recovery by process termination
Termination breaks the deadlock by removing processes. The released resources go back to the Available vector, and the remaining processes can proceed. There are two variants.
Abort all deadlocked processes
This method clearly ends the deadlock, since every process in the cycle disappears. It is also expensive. The processes may have computed for a long time, and their partial results are lost. They may have to be restarted and the work redone.
Abort one process at a time
Here the system aborts one process, then runs detection again. It repeats until no deadlock remains. This wastes less work, but it has an overhead cost: detection runs after every abort.
For the one-at-a-time method, the system has to choose a victim. Typical selection factors are:
- The priority of the process.
- How long it has run, and how much longer it needs to finish.
- How many resources it holds, and what types they are.
- How many more resources it needs.
- How many processes would have to be terminated in total.
- Whether the process is interactive or batch.
The goal is to pick the victim whose termination costs the least. No single factor decides this. A process that holds many resources is a good victim because aborting it frees a lot. A process that is nearly finished is a poor victim because you lose almost all its work.
Termination has side effects that matter in practice. A process may be in the middle of updating a file or a database. Aborting it can leave that data inconsistent. A real system has to roll back or clean up after the aborted process.
Recovery by resource preemption
Preemption takes resources from some processes and gives them to others, until the cycle is broken. It negates the no-preemption condition for those resources. Three issues must be addressed.
Selecting a victim
As with termination, the system must decide which resources and which processes to preempt. The aim is to minimize cost. Cost factors include the number of resources a deadlocked process holds and the amount of time it has consumed.
Rollback
A process that loses a resource cannot continue normally. It is missing a resource it needs. The system must roll the process back to a safe state from which it can restart.
- The simplest rollback is a total rollback: abort the process and restart it from the beginning.
- A partial rollback returns the process only as far as needed to break the deadlock. This needs the system to keep state information about running processes, such as checkpoints. It is more efficient but more complex.
Starvation
If the victim is chosen mainly by cost, the same process may be picked again and again. That process never finishes. This is starvation, which is different from deadlock. The deadlocked set keeps getting resolved, but one process is always the loser.
A common fix is to include the number of rollbacks in the cost factor. A process that has already been a victim many times becomes more expensive to pick, so it is eventually left alone.
Comparing the two methods
| Aspect | Process termination | Resource preemption |
|---|---|---|
| Coffman condition broken | Removes the process, so its hold-and-wait and circular wait disappear | No preemption |
| Work lost | Whole process, or all deadlocked processes | Part of a process, if partial rollback is used |
| Bookkeeping needed | Little; the process is removed | Checkpoints or rollback state |
| Main risk | Inconsistent files or data | Starvation of repeatedly chosen victims |
| Suits resources that | Can be cleaned up when the process dies | Can be saved and restored, such as CPU registers or memory pages |
Preemption works well for resources whose state is easy to save and restore. Memory pages and CPU registers are examples. It works badly for a printer in the middle of a job, or a lock protecting a half-finished update.
Choosing a victim in code
The next program picks a victim among deadlocked processes using a simple cost rule. The rule combines the number of resources held, the progress made, and the number of earlier rollbacks. A higher score means a more expensive victim, and the cheapest one is chosen. The weights are arbitrary and only show the idea.
def victim_cost(proc):
# Work lost grows with elapsed time and with prior rollbacks.
# Resources held reduce the cost, because aborting frees more.
return proc["elapsed"] + 10 * proc["rollbacks"] - 2 * sum(proc["held"])
def choose_victim(deadlocked):
return min(deadlocked, key=lambda name: victim_cost(deadlocked[name]))
deadlocked = {
"P1": {"elapsed": 50, "rollbacks": 0, "held": [1, 1, 0]},
"P2": {"elapsed": 5, "rollbacks": 0, "held": [0, 1, 0]},
"P3": {"elapsed": 40, "rollbacks": 3, "held": [2, 0, 1]},
}
for name, proc in deadlocked.items():
print(name, victim_cost(proc))
print("Victim:", choose_victim(deadlocked))The costs are P1 = 46, P2 = 3, and P3 = 64. P2 is chosen because it has run only briefly. Note how P3's earlier rollbacks raise its cost. This protects it from repeated selection.
Worked example with the detection matrices
Suppose detection finds that P1 and P2 are deadlocked, with one resource type that has no free instances. P1 holds 2 units and needs 1 more. P2 holds 1 unit and needs 2 more. P3 holds 1 unit and has finished its requests, so it will release soon.
If the system terminates P1, then 2 units return to Available. P2 needs 2 more, and the available amount is 2, so P2 can now finish. One abort resolved the deadlock. If the system instead aborted P2, only 1 unit would return, and P1 needs 1 more, so P1 could also finish. Both victims work. The better choice depends on the cost rules above, such as which process has done less work.
This also shows how to check that recovery worked. After removing a victim, rerun the detection algorithm on the reduced system. Recovery succeeded only if every remaining process can now be marked finishable.
Common errors
- Treating detection as if it were recovery. Finding a cycle does nothing to the processes.
- Assuming one abort always suffices. With several independent cycles, one victim breaks only the cycles it belongs to, so detection must be rerun.
- Confusing starvation with deadlock. Starvation is possible during recovery even when each deadlock is correctly resolved.
- Forgetting that preemption needs rollback. Taking a resource without restoring the process to a consistent state is unsafe.
- Picking the process that "caused" the deadlock. When detection runs infrequently, many processes may have closed cycles, and the cause is not well defined.
Summary of key points
- Recovery follows detection and must break at least one Coffman condition for the deadlocked set.
- Termination is either all deadlocked processes at once, or one at a time with re-detection after each abort.
- Preemption needs victim selection, rollback, and protection against starvation.
- Including the rollback count in the cost factor limits starvation.
- Always rerun detection after recovery to confirm the system is free of deadlock.
Exercise
A system detects a deadlock among P1, P2, and P3 and recovers by preempting resources, always choosing the process with the lowest cost. P2 has the lowest cost every time, so it is repeatedly rolled back and never completes. Name this problem and give one way to fix it.
Show answer
This is starvation. The deadlocks are resolved, but P2 is always chosen as the victim. A fix is to add the number of times a process has been rolled back to its cost, so a repeatedly chosen process becomes more expensive and is eventually not selected.
Exercise
A single resource type has no free instances. P1 holds 3 units and needs 1 more. P2 holds 2 units and needs 2 more. Both are blocked. If the system aborts P1, can P2 then finish? Explain.
Show answer
Yes. Aborting P1 returns its 3 units, so Available becomes 3. P2 needs 2 more, and 2 ≤ 3, so P2 can be granted its request and finish.
Exercise
State two differences between recovery by process termination and recovery by resource preemption.
Show answer
Termination removes whole processes and loses all their work, while preemption takes only some resources and can lose less work if a partial rollback is used. Preemption needs rollback state such as checkpoints and risks starvation of repeated victims, while termination risks leaving files or data inconsistent.