Operating SystemsUnit 313 min read
Process Deadlocks – Conditions, Detection, Prevention, Avoidance & Recovery
Unit 3 of Operating Systems: this note explains deadlock concepts, necessary conditions, resource‑allocation graphs, detection and recovery techniques, prevention & avoidance algorithms, and includes worked examples and comparison tables for quick revision.
Key points
- A deadlock occurs when a set of processes are each waiting for resources held by another in the same set.
- The four Coffman conditions are necessary and together sufficient for a deadlock to arise.
- Detection algorithms differ for single‑instance and multiple‑instance resources; the Banker's algorithm is used for avoidance.
- Recovery methods include process termination, resource preemption, and rollback; each has trade‑offs.
- Preventive strategies break at least one Coffman condition, while avoidance dynamically checks safe states before allocation.
1. Introduction to Deadlocks
A deadlock is a situation in a multiprogramming environment where a group of processes cannot proceed because each process is waiting for a resource that another process in the group holds. The system becomes permanently blocked unless external action is taken.
Deadlocks are distinct from starvation (or indefinite postponement). Starvation occurs when a process never gets the CPU or a resource because the scheduling policy continuously favors other processes, even though the system as a whole is still making progress.
1.1 Real‑world analogies
| Analogy | Processes | Resources | Result |
|---|---|---|---|
| Four cars at a four‑way stop, each waiting for the car on its right to go | Cars | Intersection space | Gridlock (deadlock) |
| Two threads each holding one lock and waiting for the other lock | Threads | Mutexes | Deadlock |
| A customer waiting forever for a service desk that always serves higher‑priority customers | Customer | Service desk | Starvation |
2. Coffman Conditions
For a deadlock to occur, all of the following conditions must hold simultaneously (Coffman et al., 1971):
| Condition | Description | How to break it |
|---|---|---|
| Mutual Exclusion | At least one resource is non‑shareable. | Make the resource shareable (if possible). |
| Hold and Wait | Processes hold allocated resources while requesting new ones. | Enforce “no hold‑and‑wait” by requiring all resources up‑front or by pre‑empting held resources. |
| No Preemption | Resources cannot be forcibly taken away from a process. | Allow preemption (e.g., rollback, priority inheritance). |
| Circular Wait | A circular chain of processes exists, each waiting for a resource held by the next. | Impose a linear ordering of resource types and require processes to request resources in that order. |
If any one condition is eliminated, deadlock cannot arise.
3. Resource‑Allocation Graph (RAG)
A Resource‑Allocation Graph is a directed bipartite graph where
- are process vertices.
- are resource‑type vertices.
Edges are of two kinds:
- Request edge (process is requesting an instance of ).
- Assignment edge (resource instance of is allocated to ).
3.1 Single‑Instance Resources
If each resource type has exactly one instance, a cycle in the RAG is both necessary and sufficient for deadlock.
Example
P0 --> R1 --> P1 --> R2 --> P0
The cycle indicates that P0 holds R1 and waits for R2, while P1 holds R2 and waits for R1 → deadlock.
3.2 Multiple‑Instance Resources
When resources have multiple instances, a cycle is necessary but not sufficient. The graph must be examined with the available vector and allocation matrix to determine a deadlocked set.
4. Deadlock Detection
Detection is performed after the system has entered a possibly unsafe state. Two major cases:
4.1 Single‑Instance Resources (Cycle Detection)
Algorithm: Depth‑First Search (DFS) on the RAG. If a cycle is found, the processes in the cycle are deadlocked.
Complexity: .
4.2 Multiple‑Instance Resources (Banker‑style Detection)
Data structures:
- Available vector – number of free instances of each resource type.
- Max matrix – maximum demand of each process.
- Allocation matrix – current allocation.
- Need matrix .
Detection Algorithm (similar to safety algorithm):
- Initialize Work = and Finish[i] = false for all processes.
- Find a process such that Finish[i] = false and .
- If found, set and Finish[i] = true; repeat step 2.
- If no such process exists, all processes with Finish = false are deadlocked.
Worked Example
Consider three resource types with total instances .
Current state:
| Process | Allocation (A,B,C) | Max (A,B,C) |
|---|---|---|
| P0 | (0, 1, 0) | (3, 2, 2) |
| P1 | (2, 0, 0) | (6, 1, 3) |
| P2 | (3, 0, 3) | (3, 0, 3) |
| P3 | (2, 1, 1) | (4, 2, 2) |
Step 1: Compute Available = Total – ΣAllocation = (6‑7, 5‑2, 7‑4) = . Since Available for A is negative, we made a mistake; adjust totals to (10,5,7) → Available = (10‑7, 5‑2, 7‑4) = (3,3,3).
Step 2: Need = Max – Allocation
- P0 Need = (3,1,2)
- P1 Need = (4,1,3)
- P2 Need = (0,0,0)
- P3 Need = (2,1,1)
Step 3: Find a process whose Need ≤ Work (3,3,3).
- P2 Need = (0,0,0) ≤ Work → finish P2, Work = Work + Allocation(P2) = (3,3,3) + (3,0,3) = (6,3,6).
Now check remaining:
- P0 Need (3,1,2) ≤ (6,3,6) → finish P0, Work = (6,3,6)+(0,1,0) = (6,4,6).
- P1 Need (4,1,3) ≤ (6,4,6) → finish P1, Work = (6,4,6)+(2,0,0) = (8,4,6).
- P3 Need (2,1,1) ≤ (8,4,6) → finish P3.
All processes can finish → no deadlock.
If at any stage no process satisfies the condition, the unfinished processes constitute a deadlocked set.
5. Deadlock Recovery
When a deadlock is detected, the system must take action to break it.
| Recovery Method | How it works | Advantages | Disadvantages |
|---|---|---|---|
| Process Termination (abort one or more deadlocked processes) | Choose victim(s) and kill them, releasing their resources. | Simple, immediate. | May lose valuable work; may need to restart aborted processes. |
| Resource Preemption | Temporarily take resources from some processes and give them to others. Often combined with rollback. | Can avoid aborting whole processes. | Requires state saving; may cause cascading rollbacks; overhead. |
| Rollback | Restore one or more processes to a previously safe checkpoint, then re‑execute. | Preserves most work; systematic. | Needs checkpointing mechanism; may lead to livelock if not careful. |
| Killing Processes in Order of Minimum Cost | Compute cost (e.g., CPU time, I/O) and abort the least costly. | Minimizes overall loss. | Cost estimation may be inaccurate; extra computation. |
5.1 Choosing a Victim
Common heuristics:
- Minimum number of resources held.
- Shortest remaining execution time.
- Lowest priority.
- Least recent CPU usage.
6. Deadlock Prevention
Prevention guarantees that at least one Coffman condition can never hold.
| Condition to Break | Typical Technique | Example |
|---|---|---|
| Mutual Exclusion | Use spooling, shareable resources, or convert to logical sharing. | Print queue spooling. |
| Hold and Wait | All‑or‑none request: a process must request all needed resources before it begins execution. | Batch job requesting all files before start. |
| No Preemption | Allow preemption: if a process holding resources requests a new one and it is denied, preempt some of its current resources. | Preempt a lock when a higher‑priority process needs it. |
| Circular Wait | Impose a total ordering of resource types and require processes to request resources in increasing order. | If resources are ordered A < B < C, a process may request A then B then C only. |
Prevention often reduces system concurrency and throughput, because it restricts how resources can be requested.
7. Deadlock Avoidance
Avoidance algorithms make dynamic decisions based on the future request pattern. The classic method is the Banker’s Algorithm (for multiple instances).
7.1 Banker’s Algorithm Overview
- Safety Check before granting a request: simulate allocation and run the safety algorithm (same as detection).
- If the resulting state is safe, grant the request; otherwise, the process must wait.
7.2 Example of a Safe vs. Unsafe Request
Using the previous example’s state (Available = (3,3,3)), suppose P1 now requests an additional (1,0,2).
- Tentative Allocation: New Available = (2,3,1).
- New Need for P1 = (3,1,1).
Run safety algorithm:
- P2 can finish (Need = 0) → Work = (2,3,1)+(3,0,3) = (5,3,4).
- P0 Need (3,1,2) ≤ Work → finish → Work = (5,3,4)+(0,1,0) = (5,4,4).
- P1 Need (3,1,1) ≤ Work → finish → Work = (5,4,4)+(2,0,0) = (7,4,4).
- P3 Need (2,1,1) ≤ Work → finish.
All can finish → safe; request granted.
If instead P1 requested (3,0,3), new Available = (0,3,0) and Need(P1) = (3,1,0). No process other than P2 can finish (P2 Need = 0). After P2 finishes, Work = (0,3,0)+(3,0,3) = (3,3,3). Now P0 Need (3,1,2) ≤ Work? No, because Need C=2 > Work C=3? Actually 2 ≤ 3, so yes. Continue... eventually all may finish; but if a request leaves no process able to proceed, the state is unsafe and the request is denied.
7.3 Limitations
- Requires a priori knowledge of maximum demand for each process.
- Overhead of safety checks for every request.
- Not suitable for systems with highly dynamic or unknown resource needs.
8. Comparison of Detection, Prevention, and Avoidance
| Aspect | Detection | Prevention | Avoidance |
|---|---|---|---|
| When applied | After deadlock occurs | Guarantees deadlock never occurs | Dynamically before allocation |
| Overhead | Periodic scans; may be costly | May restrict resource usage, reducing concurrency | Safety check on every request |
| Complexity | O(n^2) for multiple instances | Simple rules; low runtime cost | O(m·n^2) where m = resources, n = processes |
| System throughput | May drop sharply during recovery | May be lower due to restrictive policies | Generally higher than prevention, lower than unrestricted |
| Implementation difficulty | Moderate (graph algorithms) | Easy (policy enforcement) | High (need max demand, bookkeeping) |
| Typical use | General‑purpose OS (e.g., Linux) | Real‑time or safety‑critical systems | Database transaction managers, some embedded OS |
9. Practical Considerations in Modern OS
- Linux: Uses Ostrich algorithm – assumes deadlocks are rare; provides limited detection via
/proc/locksand tools likedeadlockinglibc. - Windows: Provides deadlock detection for certain kernel objects (e.g., mutexes) and a resource‑ordering guideline for driver developers.
- Database systems: Employ two‑phase locking (2PL) and wait‑die or wound‑wait schemes to avoid deadlocks.
- Distributed systems: Use wait‑for graphs and probe messages (e.g., Chandy‑Misra‑Haas algorithm) for detection.
10. Worked Trace: Round‑Robin vs. Priority Scheduling (Related to Past Exam)
Although not a deadlock topic, understanding scheduling helps answer exam questions that combine concepts.
| Process | Arrival | Burst | Priority (lower = higher) |
|---|---|---|---|
| P0 | 0 | 8 | 2 |
| P1 | 2 | 4 | 1 |
| P2 | 4 | 9 | 3 |
| P3 | 6 | 5 | 2 |
10.1 Round‑Robin (Quantum = 4)
| Time | Running Process | Remaining Burst |
|---|---|---|
| 0‑4 | P0 | 4 |
| 4‑8 | P1 (arrived at 2) | 0 (completes) |
| 8‑12 | P0 | 0 (completes) |
| 12‑16 | P2 (arrived at 4) | 5 |
| 16‑20 | P3 (arrived at 6) | 1 |
| 20‑24 | P2 | 1 |
| 24‑25 | P3 | 0 (completes) |
| 25‑26 | P2 | 0 (completes) |
Waiting times:
- P0: (Start 0) + (wait 4) = 4 → WT = 4
- P1: start at 4, arrival 2 → WT = 2
- P2: first run at 12, arrival 4 → WT = 8 (plus later wait 4) = 12? Actually total waiting = (12‑4) + (20‑16) = 8 + 4 = 12.
- P3: first run at 16, arrival 6 → WT = 10 (plus later wait 4) = 14? Wait: after first quantum (16‑20) remaining 1, then runs at 24‑25, so extra wait = 4. Total WT = (16‑6) + (24‑20) = 10 + 4 = 14.
Turnaround times (completion – arrival):
- P0: 12‑0 = 12
- P1: 8‑2 = 6
- P2: 26‑4 = 22
- P3: 25‑6 = 19
Average WT = (4+2+12+14)/4 = 8.0
Average TT = (12+6+22+19)/4 = 14.75
10.2 Non‑preemptive Priority (lower number = higher priority)
Order of execution: P1 (priority 1), P0 & P3 (priority 2, FCFS), P2 (priority 3).
| Process | Start | Completion | WT = Start‑Arrival | TT = Completion‑Arrival |
|---|---|---|---|---|
| P1 | 2 | 6 | 0 | 4 |
| P0 | 6 | 14 | 6‑0 = 6 | 14 |
| P3 | 14 | 19 | 14‑6 = 8 | 13 |
| P2 | 19 | 28 | 19‑4 = 15 | 24 |
Average WT = (0+6+8+15)/4 = 7.25
Average TT = (4+14+13+24)/4 = 13.75
These numbers illustrate how different policies affect waiting and turnaround times – a typical exam computation.
11. Summary Checklist
- Know the four Coffman conditions and how to break each.
- Be able to draw and interpret a Resource‑Allocation Graph for both single‑ and multiple‑instance resources.
- Perform deadlock detection using cycle detection (single instance) and the safety‑algorithm style (multiple instances).
- Understand recovery options and their trade‑offs.
- Distinguish between prevention (policy‑based) and avoidance (dynamic safety check).
- Apply the Banker’s algorithm to a given state and decide whether a request is safe.
Exam tip
- Past papers often ask you to differentiate deadlock vs. starvation; memorize the key distinction (progress vs. indefinite waiting).
- For graph‑based questions, draw the RAG clearly, label request/assignment edges, and immediately check for cycles.
- When a multiple‑instance detection problem is given, write down Available, Allocation, Max, and compute Need; then run the safety algorithm step‑by‑step – marking processes that can finish.
- In Banker’s questions, the examiner expects you to show the tentative allocation, recompute Available and Need, and then run the safety test. Highlight the safe sequence if one exists.
- For recovery questions, list at least two methods, discuss their pros/cons, and give a short example (e.g., aborting the process holding the most resources).
- Time‑management tip: allocate ~5 minutes to sketch the graph, ~10 minutes to run the detection algorithm, and the remaining time to write concise explanations and a short conclusion.
Based on the TU BSc CSIT syllabus for Operating Systems (CSC264), unit 3.
Discussion
Loading…