๐งฉ Topic: Deadlock โ Characterization
๐ง What is a Deadlock?
- A deadlock is a situation where a group of two or more processes are blocked forever, waiting for each other to release resources.
- It leads to permanent blocking and the system becomes unresponsive for those processes.
โ ๏ธ Real-Life Analogy
Two people are holding one chopstick each and waiting for the other chopstick to eat. Neither can eat, and neither releases their chopstick. This is a deadlock.
๐งฉ Deadlock Conditions (Coffmanโs Conditions)
Deadlock occurs only if all four of the following conditions hold simultaneously:
| Condition | Description |
|---|---|
| 1. Mutual Exclusion | At least one resource is non-shareable (only one process can use it at a time) |
| 2. Hold and Wait | A process holding a resource is waiting for more resources held by others |
| 3. No Preemption | Resources cannot be forcibly taken away; they must be released voluntarily |
| 4. Circular Wait | A set of processes are waiting for each other in a circular chain |
๐ Example
Consider three processes: P1, P2, P3 And three resources: R1, R2, R3
P1 โ holding R1, waiting for R2
P2 โ holding R2, waiting for R3
P3 โ holding R3, waiting for R1
๐ A circular wait occurs: P1 โ P2 โ P3 โ P1 โ All four conditions are satisfied โ Deadlock
๐ Resource Allocation Graph (RAG)
- A graphical method to visualize resource allocation and process waiting.
Components:
- Process: Represented by a circle (e.g., P1, P2)
- Resource: Represented by a square (e.g., R1, R2)
- Request edge (โ): From process to resource
- Assignment edge (โ): From resource to process
Deadlock Detection:
-
Cycle in RAG:
-
If only one instance per resource โ cycle = deadlock
- If multiple instances โ cycle may or may not = deadlock
๐ง Key Points to Remember
| Term | Meaning |
|---|---|
| Deadlock | A condition where no process can proceed because each is waiting for others |
| Coffman Conditions | 4 conditions that must be present for deadlock to occur |
| Circular Wait | The hallmark of deadlock; visualized using RAG |
โ Summary
- A deadlock happens when processes are stuck waiting for resources held by each other.
- Four conditions must hold together: Mutual Exclusion, Hold and Wait, No Preemption, Circular Wait.
- Resource Allocation Graph (RAG) helps detect potential deadlocks.
๐งฉ Topic: Deadlock Prevention
๐ง What is Deadlock Prevention?
- Deadlock prevention is a strategy to design the system in such a way that at least one of the four necessary conditions for deadlock never holds.
- The goal is to break at least one of the Coffman conditions.
๐ Recall the 4 Coffman Conditions:
- Mutual Exclusion
- Hold and Wait
- No Preemption
- Circular Wait
Deadlock prevention means breaking any one or more of the above.
๐งฉ How to Prevent Deadlock
๐น 1. Mutual Exclusion
- Explanation: Some resources (like printers) are not shareable.
- Prevention: This condition cannot be eliminated for non-shareable resources.
- โ Applicable only to resources that can be safely shared (like read-only files).
๐น 2. Hold and Wait
- Explanation: A process holding a resource is waiting for another.
-
Prevention:
-
Require processes to request all resources at once before execution begins.
- Or, allow a process to hold no resources while waiting for new ones.
โ Eliminates hold and wait โ Causes low resource utilization and possible starvation
๐น 3. No Preemption
- Explanation: Processes donโt give up resources voluntarily.
-
Prevention:
-
If a process requests a resource and it's not available, preempt all currently held resources, and restart the process later with all resources.
โ Ensures resources can be forcibly taken โ Complex to implement โ May lead to data inconsistency or overhead
๐น 4. Circular Wait
- Explanation: A circular chain of waiting processes exists.
-
Prevention:
-
Impose a strict global ordering of resource types (e.g., R1 < R2 < R3)
- A process can only request resources in increasing order of enumeration.
โ Simple and effective โ May require redesign of resource request logic
๐ Summary Table: Deadlock Prevention Methods
| Coffman Condition | How to Prevent It | Issues |
|---|---|---|
| Mutual Exclusion | Use sharable resources if possible | Not always possible |
| Hold and Wait | Request all resources at once | Low resource utilization |
| No Preemption | Forcefully take resources back | Complex and risky |
| Circular Wait | Impose resource ordering | Rigid resource allocation |
โ Key Points
- Deadlock prevention is proactive โ avoids deadlock before it happens.
- Requires system-level design changes to eliminate one of the four conditions.
- Not always efficient โ can lead to starvation, low CPU and resource usage.
๐งฉ Topic: Deadlock Avoidance
๐ง What is Deadlock Avoidance?
- Deadlock avoidance means the operating system takes decisions dynamically to ensure the system never enters an unsafe state.
-
It requires prior knowledge of:
-
Maximum number of resources a process may need.
- Number of available resources at any time.
โ๏ธ Safe vs Unsafe State
| State | Meaning |
|---|---|
| Safe State | A sequence exists in which all processes can complete without deadlock |
| Unsafe State | No such sequence is guaranteed โ may lead to deadlock |
โ Avoidance ensures the system always stays in a safe state.
๐ Banker's Algorithm โ Most Common Deadlock Avoidance Technique
๐งพ Assumptions:
-
System knows in advance:
-
Maximum demand of each process
- Currently allocated resources
- Available resources
๐งฎ Data Structures:
Let:
n= number of processesm= number of resource types
| Matrix/Vector | Description |
|---|---|
Available[m] |
Number of available instances of each resource |
Max[n][m] |
Maximum demand of each process |
Allocation[n][m] |
Currently allocated resources to each process |
Need[n][m] |
Remaining need = Max โ Allocation |
โ Bankerโs Algorithm Steps
- Check if request โค need of that process
-
Check if request โค available
-
If not, process must wait
-
Pretend to allocate resources:
-
Update Available, Allocation, Need
-
Check if system is in a safe state:
-
Try to find a sequence where all processes can complete
โ๏ธ If safe, grant the request โ If unsafe, deny request
๐ Example
Let:
- Available = [3, 3, 2]
- Max Matrix:
A B C
P0 7 5 3
P1 3 2 2
P2 9 0 2
P3 2 2 2
P4 4 3 3
- Allocation Matrix:
A B C
P0 0 1 0
P1 2 0 0
P2 3 0 2
P3 2 1 1
P4 0 0 2
๐ก Use Need = Max โ Allocation Run Banker's Algorithm to find if the system is in a safe state.
โ ๏ธ Limitations of Deadlock Avoidance
| Drawback | Description |
|---|---|
| Advance Knowledge Needed | OS must know the maximum resource needs |
| Complexity | Frequent safety checks can be slow |
| Low Resource Utilization | May deny requests that wouldnโt cause deadlock |
| Not Scalable | Not suitable for systems with many processes |
โ Summary
- Deadlock avoidance works by checking the system state before allocating resources.
- Uses Bankerโs Algorithm to ensure system stays in a safe state.
- Safer than prevention, but needs advance information and more computation.
๐งฉ Topic: Deadlock Detection
๐ง What is Deadlock Detection?
- Deadlock Detection is the technique used when the system does not prevent or avoid deadlocks, but instead checks periodically whether a deadlock has occurred.
- If detected, some recovery mechanism is used to break the deadlock.
๐ When is Deadlock Detection Used?
-
In systems where:
-
Resources are requested dynamically
- Performance is more important than prevention
- Deadlocks are rare and temporary
๐งฉ Conditions for Detection
Deadlock can occur if all 4 Coffman Conditions are allowed:
- Mutual Exclusion
- Hold and Wait
- No Preemption
- Circular Wait
So, deadlock detection algorithms assume all conditions may hold and check the current state for circular wait.
๐ Detection Algorithm (Single Instance per Resource Type)
- Uses a Resource Allocation Graph (RAG).
- If there is a cycle in the graph, deadlock exists.
๐ข Works well when only one instance of each resource exists.
๐ Detection Algorithm (Multiple Instances per Resource Type)
Similar to Bankerโs Algorithm, but instead of checking for a safe state before allocation, it checks if current state is already deadlocked.
๐งฎ Data Structures Used
| Name | Meaning |
|---|---|
Available[m] |
Available instances of each resource type |
Allocation[n][m] |
Allocated resources for each process |
Request[n][m] |
Currently requested resources |
Finish[n] |
Boolean array to track process completion |
โ Detection Algorithm Steps
-
Initialize:
-
Finish[i] = falsefor all i where Allocation[i] โ 0 -
Work = Available -
Find a process i such that:
-
Finish[i] == false -
Request[i] โค Work -
If found:
-
Work = Work + Allocation[i] Finish[i] = true-
Repeat Step 2
-
If no such process is found:
-
All processes where
Finish[i] == falseare deadlocked
๐ก Example
Let:
Available = [3, 3, 2]Allocation,Requestmatrices given for 5 processes.
Apply the steps above to determine which processes are deadlocked.
โ ๏ธ Complexity
- Time complexity: O(nยฒ ร m) (n = number of processes, m = resource types)
๐งฏ What to Do After Detection? (Recovery Options)
-
Abort one or more processes
-
Simple but may cause loss of work
-
Resource Preemption
-
Take resources away from some processes and restart them
- Risk of starvation
๐ Comparison: Prevention vs Avoidance vs Detection
| Strategy | Deadlock Happens? | Advance Knowledge | Suitable For |
|---|---|---|---|
| Prevention | โ No | โ Not needed | Critical systems |
| Avoidance | โ No | โ Yes | Predictable systems |
| Detection | โ Yes | โ No | Flexible, but needs recovery |
โ Summary
- Deadlock Detection checks for cycles or blocked processes in the system.
- Uses Resource Allocation Graph (RAG) or Banker-like detection algorithm.
- Detection must be followed by recovery (abort or preempt).
- Best for systems where deadlocks are rare but possible.