Contents

Dead Lock Detection

1. Deadlock: Basics and Conditions

Definition

A deadlock occurs when two or more processes cannot proceed because each holds a resource and waits for another, creating a cyclic dependency.

Four Necessary Conditions

ConditionDescriptionExample
Mutual ExclusionResources are non-sharable.Printer, file locks.
Hold and WaitProcess holds resources while waiting for others.P1 holds R1, waits for R2.
No PreemptionResources cannot be forcibly taken.CPU time, memory.
Circular WaitProcesses form a cycle of waiting.P1→P2→P3→P1.

2. Resource Allocation Graphs (RAGs)

Purpose

Visualize resource allocation states to detect deadlocks. Components:

  • Processes: ○ (e.g., P1)
  • Resources: □ (single instance: □‧; multi-instance: □‧‧‧)
  • Edges:
    • Request Edge: Process → Resource.
    • Assignment Edge: Resource → Process.

Example: Single-Instance Deadlock

Processes: P1, P2
Resources: R1‧, R2‧
Edges:
  R1 → P1 (P1 holds R1)
  R2 → P2 (P2 holds R2)
  P1 → R2 (P1 waits for R2)
  P2 → R1 (P2 waits for R1)
Cycle: P1 → R2 → P2 → R1 → P1 → ... → **Deadlock**.

3. Types of Waiting

TypeDescriptionExample
Direct WaitingProcess waits for a held resource.P1 → R1 (held by P2).
Indirect WaitingChain of waiting processes.P1 → P2 → P3 → P1.
Indefinite WaitingProcess starved due to unfair policy.Low-priority process.

4. Single-Instance vs. Multi-Instance RAGs

Single-Instance RAGs

  • Resources have one instance (e.g., printer).
  • Cycle in RAG = Deadlock.
  • Detection: Use cycle-checking in the graph.

Multi-Instance RAGs

  • Resources have multiple instances (e.g., RAM blocks).
  • Cycle ≠ Deadlock (if free instances exist).
  • Detection: Use the Banker’s Algorithm.

5. Deadlock Detection

1. Single-Instance: Cycle Detection via Wait-For Table

Step 1: Create a Wait-For Table

ProcessWaits For
P1P2
P2P1

Step 2: Detect Cycles

  • P1 waits for P2, and P2 waits for P1 → Cycle = Deadlock.

2. Multi-Instance: Banker’s Algorithm (Table-Based)

Step 1: Define Resource Allocation Tables

ProcessAllocation (R1)Max (R1)Need (R1)Available (R1)
P11322
P2242
  • Need = Max – Allocation.
  • Total Instances = Allocated + Available = 1 + 2 + 2 = 5.

Step 2: Safety Check via Table Filling

StepProcessWork (Available)Finish
1P12 → 2 + 1 = 3Yes
2P23 → 3 + 2 = 5Yes
  • Safe Sequence: P1 → P2. No Deadlock.

Example: Deadlock Scenario

ProcessAllocationMaxNeedAvailable
P12311
P2121
  • Need (P1)=1, Need (P2)=1, Available=1.
  • Neither process can be satisfied → Deadlock.

6. Detection Workflow Comparison

MethodSingle-Instance RAGsMulti-Instance RAGs
ApproachCycle detection in Wait-For TableBanker’s Algorithm (tables)
ComplexityO(n) for cycle checkO(n² × m) for n processes, m resources
ToolsAdjacency matrix/graphAllocation, Max, Need tables

7. Visual Examples

Single-Instance Deadlock (Wait-For Table)

Processes: P1, P2
Wait-For Table:
P1 → P2
P2 → P1
Cycle Detected → Deadlock.

Multi-Instance Safe State (Banker’s Tables)

Total Resources: R1=5
Allocation Table:
P1: 1, P2: 2 → Allocated = 3
Available: 2
Need Table:
P1: 2, P2: 2
Safety Sequence: P1 (needs 2 ≤ 2) → P2 (needs 2 ≤ 3).

8. Conclusion

  • Single-Instance Deadlocks are detected via cycle-checking in Wait-For tables.
  • Multi-Instance Deadlocks require the Banker’s Algorithm with resource tables.
  • Use RAGs to visualize and tables to algorithmically resolve deadlocks.