Deadlock Recovery in Operating System

Last Updated : 5 Sep, 2025

Deadlock recovery is the process of resolving a deadlock after it has already occurred. Unlike deadlock prevention (which avoids deadlocks in advance) or deadlock avoidance (which carefully allocates resources to avoid unsafe states), recovery accepts that deadlocks may occur and focuses on fixing them.

Goals of Deadlock Recovery

  1. Free resources locked by processes stuck in the deadlock.
  2. Ensure system consistency without corruption or partial updates.
  3. Minimize loss of work by carefully choosing which processes to terminate or roll back.
  4. Restore normal operation so other processes can continue execution.

Deadlock Recovery Techniques

There are several ways an operating system can recover from a deadlock:

1. Process Termination

The OS can break the deadlock by terminating one or more processes.

Kill All Deadlocked Processes

  • Simple but drastic: all processes involved in the deadlock are terminated.
  • Wasteful since a lot of work may be lost.

Kill One Process at a Time (Victim Selection)

  • The OS terminates one process at a time until the cycle is broken.
  • Factors considered for selecting the victim:

Process priority

  • How long the process has been running
  • How many resources it is holding
  • How much more work it needs to finish
  • Whether it’s interactive or batch (interactive processes are less likely to be killed)

2. Resource Preemption

Instead of killing processes, the OS may take away resources from one process and give them to another to break the cycle.

Challenges in resource preemption:

  • Which resources to take? Choosing the right resource is tricky.
  • Victim selection: Pick a process whose rollback cost is lowest.
  • Starvation risk: A process might repeatedly lose its resources (to avoid this, the OS tracks how many times a process has been chosen as a victim).

3. Process Rollback

The OS can roll back one or more processes to a previous safe state before the deadlock happened.

  • Rollback requires the system to maintain checkpoints of process states.
  • Once rolled back, the process can be restarted later when resources are available.
  • Useful in systems where killing processes is not acceptable (like banking or critical systems).

4. Combination of Methods

In practice, the OS may use a mix of termination, preemption, and rollback depending on the system design and type of processes running.

Factors Affecting Recovery Choice

When deciding how to recover, the OS considers:

  • Process Priority – High-priority processes are less likely to be terminated.
  • Execution Time – Processes close to completion are less likely to be killed.
  • Resource Holding – Processes holding fewer resources may be preferred victims.
  • Process Type – Interactive processes are usually spared; batch processes may be terminated.
  • Rollback Cost – Processes with lower restart cost are better candidates.

Example Scenario

Suppose three processes (P1, P2, P3) are deadlocked:

  • P1 is waiting for a printer held by P2.
  • P2 is waiting for memory held by P3.
  • P3 is waiting for the printer held by P1.

To recover:

  • The OS may terminate P3 (the least important), releasing memory.
  • Now P2 can finish and release the printer.
  • P1 can then proceed, resolving the deadlock.
Comment