› Module 5: Deadlocks › 5.3 Deadlock Detection & Recovery
M O D U L E

Module 5: Deadlocks

Characterization, Coffman conditions, Banker's algorithm, and recovery

5.1 Deadlock Characterization

In a multiprogramming environment, multiple processes compete for a finite set of hardware and software resources. A deadlock occurs when two or more processes are permanently blocked because each process holds a resource that another process needs, and is waiting for a resource that the other process holds — meaning none of them can ever make progress.

Figure 5.1: Resource-Allocation Graph (RAG) Containing a Deadlock Cycle P1 P2 P3 R1 R2 R3 Cycle Detected: P1 → R1 → P2 → R2 → P3 → R3 → P1 (DEADLOCK!)

Figure 5.1: In a Resource-Allocation Graph with single-instance resource types, the presence of a directed cycle is both a necessary and sufficient condition for deadlock.

The Four Coffman Conditions

A deadlock can arise if and only if all four of the following conditions hold simultaneously in the system:

  1. Mutual Exclusion: At least one resource must be non-shareable. Only one process may use the resource at any given time.
  2. Hold and Wait: A process must be currently holding at least one resource and simultaneously waiting to acquire additional resources held by other processes.
  3. No Preemption: Resources cannot be forcibly seized from a process. They can only be released voluntarily by the holding process after completing its task.
  4. Circular Wait: A closed chain of processes ${P_0, P_1, dots, P_n}$ exists such that $P_0$ is waiting for a resource held by $P_1$, $P_1$ is waiting for $P_2$, and $P_n$ is waiting for a resource held by $P_0$.

5.2 Deadlock Prevention & Avoidance

Operating systems approach deadlock management using two proactive strategies: Prevention (constraining resource requests to mathematically eliminate at least one Coffman condition) and Avoidance (dynamically assessing resource allocation states to ensure the system never enters an unsafe state).

Figure 5.2: Banker's Algorithm Data Structures — Allocation, Max, and Need Matrices Process Allocation (A, B, C) Max Demand (A, B, C) Need = Max āˆ’ Alloc Available P0 0 1 0 7 5 3 7 4 3 3 3 2 P1 2 0 0 3 2 2 1 2 2 — P2 3 0 2 9 0 2 6 0 0 — P3 2 1 1 2 2 2 0 1 1 — P4 0 0 2 4 3 3 4 3 1 — Safe State Verified! Safe Sequence: <P1, P3, P4, P0, P2>

Figure 5.2: In Banker's Algorithm, a resource request is only granted if simulating the allocation keeps the system in a guaranteed Safe State.

Deadlock Prevention Techniques

  • Eliminate Hold and Wait: Require processes to request all needed resources at once before starting execution. If any resource is unavailable, none are allocated. Drawback: Severely reduces resource utilization.
  • Allow Preemption: If a process holding resources requests an unavailable resource, all of its currently held resources are preempted and returned to the free pool.
  • Eliminate Circular Wait: Impose a strict global total ordering on all resource types (e.g., $F( ext{Disk}) = 1, F( ext{Printer}) = 2, F( ext{Tape}) = 3$). A process can only request resources in strictly increasing numerical order. Mathematically makes circular cycles impossible!

Deadlock Avoidance: Banker's Algorithm

Banker's Algorithm requires each process to declare its maximum possible resource claims in advance. When a process requests resources, the OS temporarily allocates them in memory and runs the Safety Algorithm:

If there exists an execution sequence $langle P_1, P_2, dots, P_n angle$ where each process can finish using currently available resources plus resources freed by previously finished processes, the state is Safe, and the allocation is granted. If no safe sequence exists, the process must wait.

5.3 Deadlock Detection & Recovery

If a system employs neither deadlock prevention nor avoidance, deadlocks may occur at runtime. In this environment, the operating system must periodically execute a Deadlock Detection Algorithm and invoke a recovery mechanism to restore normal operation.

Figure 5.3: Wait-For Graph (WFG) Detection and Recovery Strategy Options P1 P2 P3 P4 Wait-For Cycle: P1 → P2 → P3 → P4 → P1 = DEADLOCK CONFIRMED! Recovery: Process Termination • Abort all deadlocked processes (drastic) • Abort one process at a time until cycle breaks Recovery: Resource Preemption • Select a victim process based on cost • Roll back to safe checkpoint & prevent starvation

Figure 5.3: When a cycle is discovered in the Wait-For Graph, the operating system recovers via selective process termination or resource preemption with rollback.

Recovery Strategies in Detail

  • Process Termination:
    • Abort all deadlocked processes: Guaranteed to break the deadlock immediately, but discards partial computations at great cost.
    • Abort one process at a time: Abort one victim, release its resources, re-run the cycle detection algorithm, and repeat until the deadlock is resolved.
  • Resource Preemption:
    • Victim Selection: Determine which process has consumed the least CPU time and holds the most critical resources.
    • Rollback: Roll the victim back to a previously saved checkpoint rather than killing it outright.
    • Starvation Prevention: Ensure a process is not repeatedly selected as a victim by factoring victim count into cost metrics.
MODULE COMPREHENSIVE QUIZ

Module 5 Final Quiz

Test your understanding across all 3 topics of this module with interview-grade questions, tricky edge cases, and code output predictions.

Take Module 5 Final Quiz →
Yukti Mascot