Deadlock strategies: prevent, avoid, detect, recover

Match each deadlock policy to the moment it acts, then run a detector on current requests and recover by ending a victim. Deadlock depth and concurrency diagnosis · Operating systems.

Suppose a system lets processes request resources freely and never checks ahead. Some may eventually wait on each other indefinitely. Can the system find the stuck ones afterwards, and what can it do once it has? Before the detector, it helps to place it among the other policies.

Four policies, four moments

A deadlock needs four conditions to hold together: mutual exclusion (a unit is held by one process at a time), hold and wait (a process keeps what it has while it waits for more), no preemption (a held unit cannot be taken away) and circular wait (a ring of processes each waits for the next). Take away any one and no deadlock can form.

Prevention acts before any request: it changes the rules so one condition cannot hold. A global lock order, with every process taking locks in increasing order, breaks circular wait, as the resource graph lesson showed. Avoidance acts on each request: it refuses a grant that would lead to an unsafe state, as the Banker's algorithm does. An unsafe state is not yet a deadlock; it is one the system declines to enter. Detection acts after the fact: it lets the deadlock happen, then looks for it. Recovery is what follows a detection: it breaks the deadlock at a cost.

Prevention restricts resource use, while Banker-style avoidance checks requests against declared maximums. Detection and recovery permit risky grants and accept that a deadlock may persist until it is detected and repaired.

What a detector looks at

A detector has no declared maximums to lean on. It reads three things: each process's Allocation (what it holds), each process's current Request (what it is waiting for right now) and Available. It copies Available into Work and passes over the processes. A process whose Request is no more than Work in every entry is assumed to get it, finish and hand back everything it holds, so Work grows by its Allocation, and the passes repeat.

The pass assumes a consistent snapshot, no external release or preemption, and that a process whose current request is met can finish without asking for more. This last assumption is optimistic, not a prediction of its later requests. A process that holds nothing is set aside first. It cannot hold the others in a deadlock, because nobody can be waiting for a unit it does not hold. It can still be blocked indefinitely behind a deadlocked group.

The first example has three resource types: disks, tapes and plotters. Available is [0, 0, 1]. A holds a disk and waits for a tape. B holds a tape and waits for a plotter. C holds a plotter and waits for a disk. E holds nothing and waits for a tape. In a resource-allocation graph, the request and allocation edges for A, B and C form a cycle. B is not blocked by C: the spare plotter can satisfy B.

A detection pass that finds no deadlock

Read the steps as text

  1. Available is [0, 0, 1], listed as disks, tapes and plotters. Each row shows the units a process holds and what it is asking for now.
  2. Work starts as a copy of Available: [0, 0, 1]. Process E holds nothing, so it is set aside.
  3. Process A is waiting for [0, 1, 0], but it is short on tapes. Process B is waiting for [0, 0, 1], and Work covers it. Assume it finishes and hands back [0, 1, 0]: Work becomes [0, 1, 1].
  4. Process A is waiting for [0, 1, 0], and Work covers it. Assume it finishes and hands back [1, 0, 0]: Work becomes [1, 1, 1].
  5. Process C is waiting for [1, 0, 0], and Work covers it. Assume it finishes and hands back [0, 0, 1]: Work becomes [1, 1, 2].
  6. B, A and C can finish in that order. There is no deadlock.

Plotters have a spare unit, so B's request is covered at once. B finishes and hands back a tape, which covers A's request. A hands back a disk, which covers C's. The simulated finish order is B, A, C. E was set aside because it holds nothing; the pass does not claim E actually finished.

One unit each: the wait-for ring

Some systems keep exactly one unit of every resource, such as one lock per database table. Then every wait points at a single holder, so the tables can be dropped from the picture. If Job 1 holds the orders table and waits for the billing table that Job 2 holds, Job 1 is in effect waiting for Job 2. Here three jobs hold one table each and then ask for the next.

One unit each, so a ring of waits is the deadlock

Read the steps as text

  1. The graph has 3 jobs and 3 resources, with 3 instances in all. Nothing is held yet.
  2. Job 1 requests the orders table and gets it: the orders table was free. Job 2 requests the billing table and gets it: the billing table was free. Job 3 requests the audit table and gets it: the audit table was free.
  3. Job 1 requests the billing table, held by Job 2, and waits. Job 2 requests the audit table, held by Job 3, and waits.
  4. Job 3 requests the orders table, held by Job 1, and waits. Deadlock: Job 1, Job 2 and Job 3 cannot finish unaided.
  5. Each resource becomes an edge from the job waiting for it to the job holding it. Job 1 waits for Job 2, Job 2 waits for Job 3 and Job 3 waits for Job 1: a cycle of waits, so Job 1, Job 2 and Job 3 are deadlocked.

Once the tables are replaced by job-to-job edges, the ring of three arrows is the whole answer: 3 jobs are stuck, and no scan is needed. This shortcut holds only because every resource has one unit. With multiple units per resource type, a resource-allocation graph cycle is necessary but not sufficient for deadlock; the Work scan tests the current requests.

When nothing fits

Now take away the spare plotter, drop E and add a fourth process, D. Available is [0, 0, 0]. A, B and C hold and wait as before, and D holds a disk and waits for a plotter. Every Request asks for a unit that only a waiting process holds, so nothing fits an empty Work. All four are deadlocked.

One recovery choice is to end a process to break the ring. Here the system picks a victim and terminates it. This is different from the check. In the pass, a finishing process only hands units to Work on paper. Terminating a victim really returns its units to Available and discards its work. The detector then runs again on the new ledger.

Nothing fits, so end a victim and look again

Read the steps as text

  1. Available is [0, 0, 0], listed as disks, tapes and plotters. Each row shows the units a process holds and what it is asking for now.
  2. Work starts as a copy of Available: [0, 0, 0].
  3. Process A is short on tapes, process B on plotters, process C on disks and process D on plotters.
  4. Processes A, B, C and D cannot finish with Work [0, 0, 0]. They are deadlocked.
  5. Terminate process D as the victim. The [1, 0, 0] it holds goes back to Available, now [1, 0, 0]. Its work is lost.
  6. Work starts as a copy of Available: [1, 0, 0].
  7. Process A is short on tapes and process B on plotters. Process C is waiting for [1, 0, 0], and Work covers it. Assume it finishes and hands back [0, 0, 1]: Work becomes [1, 0, 1].
  8. Process A is waiting for [0, 1, 0], but it is short on tapes. Process B is waiting for [0, 0, 1], and Work covers it. Assume it finishes and hands back [0, 1, 0]: Work becomes [1, 1, 1].
  9. Process A is waiting for [0, 1, 0], and Work covers it. Assume it finishes and hands back [1, 0, 0]: Work becomes [2, 1, 1].
  10. Every remaining process can finish, in the order C, B and A. There is no deadlock.

Ending D returns a disk. C's request for a disk now fits, C finishes and hands back its plotter, and B and then A follow. Suppose D is the cheapest to lose; the system chooses it for that reason: ending any one of the four would clear this ledger. One victim is not always enough. If the victim held only units that no waiting process asks for, the others stay stuck, and the pass is repeated with the next victim.

What recovery costs

Which victim to end is a policy choice. Common choices weigh how much work would be lost, how long the process has run, how many resources it holds and how many times it has already been ended. Rolling a process back to a saved checkpoint loses less work, but only if the checkpoint restores a consistent state. Taking a unit away from a process without ending it works for a resource that can be handed over safely, such as a page of memory, and not for a held lock.

The detector can run on a timer or when a request has waited too long. Running it often catches a deadlock sooner and costs more processor time on every run. Each run tests only the current requests under the finish assumption; a later request can change the result of a later run.

Recap

  • Prevention breaks a condition in advance, avoidance refuses a risky grant, detection finds a deadlock that has happened, and recovery ends or rolls back to break it.
  • A detector uses current Requests and Work, sets aside processes that hold nothing, and reports the processes left over as deadlocked; with one unit of every resource, a cycle of waits is already the answer.
  • Ending a victim returns its units for real, so the detector runs again; which victim to end is a cost decision.
Go deeper

With several units of a resource the ring in the allocation graph is necessary but not enough, which is why this detector counts Work instead of looking for a cycle. A state the Banker's algorithm calls unsafe may still avoid deadlock if no process ever asks for its declared maximum, which is why avoidance is cautious and detection tests the present snapshot under its stated finish assumption.

A detector over n processes and m resource types repeats a scan of up to n rows for up to n finishes, about n² · m steps. This costs the same order as the Banker's check, but it uses what processes are waiting for now and needs no declared maximums.

Check your understanding with the quiz, then read about starvation, livelock and priority inversion, where processes keep running yet still make no progress.