Deadlock is when two or more processes are unable to proceed because each is waiting for the other to release resources.

deadlock.png

<aside> 💡 Consider an example when two trains are coming toward each other on the same track and there is only one track, none of the trains can move once they are in front of each other. This is a practical example of deadlock.

</aside>

Conditions for a Deadlock:

Methods for handling deadlocks:

  1. Deadlock prevention or avoidance
  2. Deadlock recovery
  3. Deadlock ignorance

Deadlock prevention

The goal of deadlock prevention is to avoid fulfilling the conditions for a deadlock.

Deadlock avoidance

By using the strategy of “Avoidance”, we have to make an assumption of all the resources needed for the process before execution.

Deadlock recovery

Deadlock detection and recovery have two phases:

  1. The first phase examines the state of the process, whether the deadlock is in the system or not.
  2. If a deadlock is found, a deadlock recovery algorithm is executed.

Deadlock detection and recovery ensures data integrity, but degrades performance.

Deadlock recovery techniques

  1. Manual intervention: When a deadlock is detected, one option is to inform the operator and let them handle the situation manually.
  2. Automatic recovery: This approach enables the system to recover from deadlock automatically. It involves breaking the deadlock cycle by either aborting processes or preempting resources.
  3. Process termination: We can terminate all deadlocked processes or abort one process at a time. However, terminating all deadlocked processes can cause in partial computation loss. Alternatively, terminating one process at a time can cause overhead as a deadlock-detection algorithm must be invoked after each process termination.
  4. Resource preemption

Resource preemption involves choosing which resources and processes should be preempted to break the deadlock. If a resource is preempted from a process, the process cannot continue its normal execution as it lacks the required resource.

deadlock-detection-and-recovery-in-operating-system1.png

Rolling back the process to a safe state and restarting it is a common approach. Determining a safe state can be challenging, leading to the use of total rollback, where the process is aborted and restarted from scratch.

Deadlock ignorance

If a deadlock is very rare, then let it happen and reboot the system. This is the approach that both Windows and UNIX take.