Chapter 7: Deadlocks

CS330 - Operating Systems

System Deadlocks, Prevention, Avoidance, Detection & Recovery

7.1 System Model

A deadlock is a situation where a set of processes are blocked because each process is holding a resource and waiting for another resource acquired by some other process.

Resource Types

Process Resource Utilization

// Normal process resource usage sequence: 1. Request - Process requests resource 2. Use - Process uses resource 3. Release - Process releases resource

⚠️ Deadlock Example:

Process P1 holds Resource R1 and waits for Resource R2

Process P2 holds Resource R2 and waits for Resource R1

Result: Both processes wait forever!

7.2 Deadlock Characterization

Four Necessary Conditions for Deadlock

Deadlock can arise if ALL FOUR conditions hold simultaneously:

1Mutual Exclusion

At least one resource must be held in a non-sharable mode. Only one process at a time can use the resource.

2Hold and Wait

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

3No Preemption

Resources cannot be preempted; a resource can be released only voluntarily by the process holding it.

4Circular Wait

A set {P0, P1, ..., Pn} of waiting processes must exist such that P0 waits for P1, P1 waits for P2, ..., and Pn waits for P0.

💡 Important:

All four conditions must be present for a deadlock to occur. If we can prevent ANY ONE of these conditions, we can prevent deadlock!

7.3 Resource-Allocation Graph (RAG)

Resource Allocation Graph Components

A directed graph used to describe deadlocks

P1
R1
P2

Graph Elements:

  • Vertices:
    • Process nodes (circles): P = {P1, P2, ..., Pn}
    • Resource nodes (rectangles): R = {R1, R2, ..., Rm}
  • Edges:
    • Request edge: Pi → Rj (process requests resource)
    • Assignment edge: Rj → Pi (resource assigned to process)

RAG Analysis Rules:

  • If graph has no cyclesNo deadlock
  • If graph has a cycle:
    • If only one instance per resource type → Deadlock
    • If several instances per resource type → Possibility of deadlock

📊 Resource Allocation Graph Examples

[Insert RAG diagrams showing deadlock and no-deadlock scenarios]

7.4 Methods for Handling Deadlocks

1️⃣ Deadlock Prevention

Ensure that at least one of the four necessary conditions cannot hold.

  • Structural approach
  • Prevents deadlocks by design
  • May lead to low device utilization

2️⃣ Deadlock Avoidance

Requires advance knowledge of resource needs. System decides if request should wait.

  • Dynamic approach
  • Uses Banker's Algorithm
  • Maintains safe state

3️⃣ Deadlock Detection

Allow deadlocks to occur, then detect and recover.

  • Periodic checking
  • Detection algorithm
  • Recovery procedures

4️⃣ Ignore the Problem

Ostrich Algorithm - pretend deadlocks never occur.

  • Used by most OS
  • Manual intervention
  • Cost-effective for rare deadlocks

7.5 Deadlock Prevention

Restrain the ways resource requests can be made to ensure at least one condition cannot occur:

Preventing Each Condition:

1. Mutual Exclusion

Prevention: Not practical for most resources. Some resources are inherently non-sharable (e.g., printer).

Solution: Use spooling for some resources (printer queues).

2. Hold and Wait

Prevention: Guarantee that when a process requests a resource, it doesn't hold any other resources.

Solutions:

  • Request all resources at beginning (low resource utilization)
  • Release all resources before requesting new ones

3. No Preemption

Prevention: If a process holding resources requests another that cannot be immediately allocated, all currently held resources are released.

Limitation: Applied only to resources whose state can be saved (CPU, memory).

4. Circular Wait

Prevention: Impose a total ordering of all resource types.

Solution: Require processes to request resources in increasing order of enumeration.

// Resource ordering example: R1 = tape drive (order = 1) R2 = disk drive (order = 2) R3 = printer (order = 3) // Process must request in order: R1 → R2 → R3 // Cannot request R1 after holding R2

7.6 Deadlock Avoidance

Safe State

A state is safe if the system can allocate resources to each process in some order and still avoid deadlock.

A system is in a safe state if there exists a safe sequence of all processes.

Safe State

No Deadlock Possible

Unsafe State

May Lead to Deadlock

Deadlock State

System Stuck

Resource-Allocation Graph Algorithm

For single instance of each resource type:

Banker's Algorithm

For Multiple Instances of Resources

Named after banking system that ensures bank never allocates all cash.

Data Structures (n processes, m resource types):

Available[m]
3
3
2

Available resources

Max[n][m]
7
5
3
3
2
2
9
0
2

Maximum demand

Allocation[n][m]
0
1
0
2
0
0
3
0
2

Currently allocated

Need[n][m]
7
4
3
1
2
2
6
0
0

Need = Max - Allocation

Safety Algorithm

// Safety Algorithm to find safe sequence 1. Let Work = Available and Finish[i] = false for all i 2. Find an i such that: Finish[i] == false AND Need[i] ≤ Work If no such i exists, go to step 4 3. Work = Work + Allocation[i] Finish[i] = true Go to step 2 4. If Finish[i] == true for all i, then system is in safe state

Example Safe Sequence:

P1 P3 P4 P2 P0

System is in safe state with this execution order

7.7 Deadlock Detection

Detection Algorithm

Allow system to enter deadlock state, then detect and recover.

Single Instance of Each Resource Type:

  • Maintain wait-for graph
  • Nodes are processes
  • Pi → Pj if Pi is waiting for Pj
  • Periodically check for cycles
🔄 Cycle in graph = Deadlock detected!

Multiple Instances Detection Algorithm

// Similar to Banker's algorithm but for detection 1. Let Work = Available For i = 0 to n-1: if Allocation[i] ≠ 0 Finish[i] = false else Finish[i] = true 2. Find index i such that: Finish[i] == false AND Request[i] ≤ Work 3. Work = Work + Allocation[i] Finish[i] = true Go to step 2 4. If Finish[i] == false for some i, then system is in deadlock state Processes with Finish[i] == false are deadlocked

When to Invoke Detection Algorithm?

7.8 Recovery from Deadlock

Recovery Methods

Option 1: Process Termination

  • Abort all deadlocked processes
    • Clear deadlock immediately
    • Expensive - all work lost
  • Abort one process at a time
    • Check for deadlock after each abort
    • Less expensive but more overhead

Option 2: Resource Preemption

  • Selecting a victim - Which resources and processes to preempt?
  • Rollback - Return to safe state, restart process
  • Starvation - Ensure same process not always victim

Factors in Selecting Victim

Factor Consideration
Priority Lower priority process selected first
Time How long process has computed and remaining time
Resources Resources used and resources needed to complete
Rollback Cost Number of processes that need termination
Interactive/Batch Interactive processes may have higher priority

7.9 Practical Examples

Example 1: Banker's Algorithm Application

Given: 5 processes (P0-P4), 3 resource types (A=10, B=5, C=7)

Process Allocation
(A B C)
Max
(A B C)
Need
(A B C)
P0 0 1 0 7 5 3 7 4 3
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

Available: A=3, B=3, C=2

Safe Sequence: <P1, P3, P4, P2, P0>

System State: SAFE ✓

Example 2: Resource Request

If P1 requests (1,0,2):

  1. Check: Request ≤ Need? (1,0,2) ≤ (1,2,2) ✓
  2. Check: Request ≤ Available? (1,0,2) ≤ (3,3,2) ✓
  3. Pretend allocation:
    • Available = (3,3,2) - (1,0,2) = (2,3,0)
    • Allocation[P1] = (2,0,0) + (1,0,2) = (3,0,2)
    • Need[P1] = (1,2,2) - (1,0,2) = (0,2,0)
  4. Run safety algorithm → Find safe sequence
  5. If safe, grant request; else deny

7.10 Practice Problems and Exam Questions

Multiple Choice Questions

Q1. Which of the following is NOT a necessary condition for deadlock?

  • a) Mutual exclusion
  • b) Hold and wait
  • c) Preemption allowed
  • c) Preemption allowed

Q2. In Resource Allocation Graph, a cycle indicates:

  • a) Always a deadlock
  • b) Deadlock if single instance, possible deadlock if multiple
  • c) Never a deadlock
  • d) System is safe

Q3. Banker's algorithm is used for:

  • a) Deadlock prevention
  • b) Deadlock avoidance
  • c) Deadlock detection
  • d) Deadlock recovery

Q4. A safe state means:

  • a) System is in deadlock
  • b) System will enter deadlock
  • c) System can avoid deadlock
  • d) System has no processes

Q5. Which method prevents circular wait?

  • a) Resource ordering
  • b) Resource sharing
  • c) Process termination
  • d) Resource preemption

Short Answer Questions

Q6. Explain the difference between deadlock prevention and deadlock avoidance.

Answer:

Prevention: Structurally makes deadlock impossible by ensuring at least one necessary condition cannot occur.

Avoidance: Dynamically examines resource allocation state to ensure system never enters unsafe state.

Q7. Given 3 processes with following needs. Is the system in safe state?

Process Allocated Max
P0 1 4
P1 2 3
P2 2 9

Available = 2

Solution:

Need: P0=3, P1=1, P2=7

Can satisfy P1 (Need=1 ≤ Available=2)

After P1: Available = 2 + 2 = 4

Can satisfy P0 (Need=3 ≤ Available=4)

After P0: Available = 4 + 1 = 5

Cannot satisfy P2 (Need=7 > Available=5)

System is NOT in safe state

7.11 Key Terms and Definitions

Term Definition
Deadlock Processes blocked forever waiting for each other
Livelock Processes continuously change state but make no progress
Starvation Process waits indefinitely while others proceed
Safe State System can allocate resources without entering deadlock
Unsafe State May lead to deadlock (not necessarily deadlocked)
RAG Resource Allocation Graph for visualizing resource allocation
Banker's Algorithm Deadlock avoidance algorithm for multiple resource instances
Wait-for Graph Variant of RAG for deadlock detection
Circular Wait Cyclic chain of processes waiting for resources
Resource Ordering Prevention technique assigning order to resources
Rollback Return process to previous safe state
Victim Selection Choosing process to terminate or preempt

7.12 Diagrams from Slides

📊 Resource Allocation Graph

[Insert RAG diagram with processes and resources]

📊 Deadlock vs No Deadlock RAG

[Insert comparison of RAGs with and without cycles]

📊 Safe, Unsafe, and Deadlock States

[Insert state transition diagram]

📊 Banker's Algorithm Example

[Insert step-by-step Banker's algorithm execution]

Note: Please provide the actual diagrams from your course materials to replace these placeholders.