Chapter 8

Chapter 8: Deadlocks

Overview of Deadlocks

  • In a multiprogramming environment, several processes may compete for a finite number of resources.

  • A process requests resources and, if they are unavailable, enters a waiting state.

  • Occasionally, a waiting process cannot change state because its requested resources are held by other waiting processes.

  • This scenario is referred to as a deadlock.

System Model

  • The system comprises a finite number of resources distributed among competing processes.

  • Resources are partitioned into several types, each containing multiple identical instances. For example, instances of CPU.

Examples of Resource Types
  • Resource types can include but are not limited to:

    • CPUs

    • Printers

    • Disk drives

    • Memory blocks

Deadlock Process Utilization Sequence

  • A process may utilize a resource in a specific sequence:

    1. Request: If the request cannot be granted immediately (e.g., due to use by another process), the requesting process must wait.

    2. Use: Once available, the process can operate on the resource (e.g., print).

    3. Release: Finally, the process releases the resource after use.

Deadlock Characterization

  • Four necessary conditions must simultaneously hold for a deadlock to occur:

    1. Mutual Exclusion:

    • At least one resource must be held in a non-sharable mode; only one process can use a resource at a time. If another process requests that resource, it must be delayed until the resource is released.

    1. Hold and Wait:

    • A process must be holding at least one resource and waiting to acquire additional resources currently held by other processes.

    1. No Preemption:

    • Resources cannot be forcibly taken from processes; they must be released voluntarily after the process has completed its task.

    1. Circular Wait:

    • There exists a set of processes {P0, P1, …, Pn} such that P0 waits for a resource held by P1, P1 waits for a resource held by P2, …, and Pn waits for a resource held by P0.

  • If all four conditions hold, processes never complete execution, and system resources become tied up, preventing the initiation of other jobs.

Deadlock Graphs and Cycles

  • Resource-Allocation Graph:

    • The vertices are divided into two types:

    • Processes (P): {P1, P2, …, Pn}

    • Resources (R): {R1, R2, …, Rm}

    • Request Edge: A directed edge from process Pi to resource type Rj indicates that Pi requests Rj and is currently waiting for it.

    • Assignment Edge: A directed edge from resource type Rj to process Pi signifies that Rj is allocated to Pi.

  • If the graph contains cycles, a deadlock may exist.

Characteristics of the Graph
  • If the graph contains no cycles, there is no deadlock.

  • If the graph contains a cycle, deadlock MAY exist.

  • Example of cycles in resource allocation leading to deadlocks includes specific sequences such as:

    • P1 -> R1 -> P2 -> R3 -> P3 -> R2 -> P1

    • P2 -> R3 -> P3 -> R2 -> P2.

  • Basic Facts:

    • No cycles in graph ⇒ No deadlock.

    • Cycles present ⇒ Deadlock possible, especially if there is only one instance per resource type.

Handling Deadlocks

There are three main strategies for managing deadlocks:

  1. Deadlock Prevention and Avoidance:

    • Implement protocols to ensure that the system never enters a deadlock state.

  2. Deadlock Detection and Recovery:

    • Allow the system to enter a deadlocked state, then detect it and recover.

  3. Ignore the Problem:

    • Pretend deadlocks do not occur.

Deadlock Prevention Methods
  • Deadlock Prevention involves ensuring that at least one of the four necessary conditions does not hold:

    1. Mutual Exclusion:

      • Not avoidable for non-shared resources (e.g., printers), as they must be accessed exclusively.

    2. Hold and Wait:

      • Require processes to request all needed resources upfront before execution or allow them to request resources only when they hold none.

      • Disadvantages: Poor resource utilization and possibility of starvation.

    3. No Preemption:

      • A process holding resources that requests more can have its current resources preempted if it must wait.

      • Suitable mainly for resources that can be saved and restored easily (e.g., CPU registers).

      • Not applicable to non-preemptible resources like printers.

    4. Circular Wait:

      • Impose a total order on resource types, requiring processes to request resources in increasing order.

Deadlock Avoidance
  • Deadlock can also be avoided by requiring knowledge of the maximum resources a process will request during its lifetime.

  • A Safe State is defined as one where a deadlock can be avoided. A state is considered safe if there is a sequence of resource allocations that allows all processes to complete without entering a deadlock.

  • Not all unsafe states lead to deadlocks, but they may lead to conditions where a deadlock could occur.

Banker's Algorithm Example
  1. Consider 5 processes (P0 to P4) needing resources from 3 types (A, B, C):

    • Resource availability and allocation matrices determine if the system is in a safe state or not, using a sequence such as to ensure deadlock avoidance.

  2. The algorithm checks resource requests against current availability and pending needs to maintain a safe environment.

Deadlock Detection with Single Instances

  • Use Wait-For Graph to illustrate that deadlock exists if the graph contains a cycle.

  • An edge from Pi to Pj indicates that Pi is waiting for Pj to release a resource needed by Pi.

Recovery from Deadlock
  • Upon detection of a deadlock, the system can:

    1. Inform an Operator: Manual resolution.

    2. Automatic Recovery: Could involve aborting processes or preempting resources from some processes to resolve the deadlock.

    • Options include:

      • Abort all deadlocked processes.

      • Abort one process at a time until the cycle is broken.

      • Preempt resources which may necessitate a status rollback and consideration of starvation issues.