Deadlock Prevention, Avoidance, Detection & Recovery in Operating System
Once we understand how a deadlock is created, the next question is: How should an operating system deal with it? The system can stop deadlock from becoming possible, carefully avoid dangerous resource allocations, allow deadlock and detect it later, or recover after deadlock has already occurred.
These approaches are called Deadlock Prevention, Deadlock Avoidance, Deadlock Detection and Deadlock Recovery. They sound similar, but they work at completely different stages. This chapter explains each method, safe and unsafe states, Banker's Algorithm, detection algorithms, process termination, resource preemption, rollback and victim selection in simple language.
How Can an Operating System Handle Deadlock?
There is no single deadlock-handling method that is perfect for every system. Different approaches make different trade-offs between safety, performance, resource utilization and implementation cost.
Deadlock Prevention
Change resource-allocation rules so that at least one of the four necessary deadlock conditions cannot hold.
Idea: Make deadlock structurally impossible.Deadlock Avoidance
Examine each new request and grant it only when the resulting allocation leaves the system in a safe state.
Idea: Do not enter a dangerous state.Deadlock Detection
Do not try to prevent every dangerous allocation. Instead, periodically check whether processes have actually become deadlocked.
Idea: Allow first, inspect later.Deadlock Recovery
Once deadlock is detected, take action that breaks the circular dependency.
Idea: Restore progress after failure.Deadlock Prevention in Operating System
Deadlock Prevention works by ensuring that at least one of the four necessary conditions for deadlock can never occur.
Recall the four conditions:
Mutual Exclusion · Hold and Wait · No Preemption · Circular Wait
Deadlock prevention is a method in which the resource-allocation rules are designed so that at least one necessary condition for deadlock is always false.
Make resources shareable whenever possible.
Do not allow a process to hold resources while waiting for additional resources.
When practical, force a process to give up some resources if its new request cannot immediately be satisfied.
Define a fixed global ordering for resource types and require processes to request resources only in that order.
1. Preventing Mutual Exclusion
Mutual exclusion exists when only one process can use a resource instance at a time.
One prevention idea is to make resources shareable.
However, this is only possible for resources that are naturally safe to share.
Therefore, breaking mutual exclusion is useful only for suitable shareable resources.
2. Preventing Hold and Wait
Hold and Wait occurs when a process already owns one or more resources while waiting for additional resources.
A system can prevent this in two common ways.
Method A: Request Everything Together
A process declares and requests all required resources before it begins the resource-dependent work.
Method B: Release Before Requesting More
A process must release what it already holds before requesting another unavailable resource.
Problem With This Method
A process may reserve resources long before it actually needs them. Those resources remain unavailable to other processes.
Resource utilization can therefore become poor.
A process that repeatedly fails to obtain all required resources may also experience long delays or starvation.
3. Breaking the No Preemption Condition
Another strategy is to make selected resources preemptable.
Suppose Process P1 holds R1 and requests R2. R2 is unavailable.
Instead of letting P1 keep R1 while it waits, the system may require P1 to release eligible resources and try again later.
4. Preventing Circular Wait With Resource Ordering
Circular Wait can be prevented by assigning each resource type a fixed number or order.
Processes must then request resources only in increasing order.
Order 1
Order 2
Order 3
Order 4
A process may request:
R1 → R2 → R4
but it should not hold R4 and then request R2.
Because every process moves in the same resource-order direction, a circular resource dependency cannot form.
Advantages and Limitations of Deadlock Prevention
Advantages
Deadlock is prevented by design.
The logic can be easier to reason about than allowing deadlock and repairing it later.
Limitations
Prevention rules can reduce resource utilization, concurrency and flexibility.
Some conditions, especially mutual exclusion and no preemption, cannot be removed for every resource.
Deadlock Avoidance in Operating System
Deadlock Avoidance takes a different approach.
Instead of permanently breaking one deadlock condition, the operating system examines resource requests before granting them.
The request is granted only if the resulting system state remains safe.
Deadlock avoidance is a dynamic resource-allocation method that grants requests only when the allocation keeps the system in a safe state.
What Information Is Needed?
Avoidance normally requires the system to know something about the maximum possible resource demand of each process in advance.
That information allows the operating system to ask:
“If I grant this request now, can all processes still finish in some order?”
Safe State, Unsafe State and Deadlocked State
At least one order exists in which every process can obtain the resources it still needs, finish, and release its resources.
The operating system cannot guarantee that all processes can finish using the remaining resources and declared maximum demands.
Processes are already permanently blocked by resource dependencies.
What Is a Safe Sequence?
A safe sequence is an order of processes in which every process can receive its remaining required resources, complete, and release what it holds.
If at least one safe sequence exists, the state is considered safe.
Resource Allocation Graph Algorithm for Deadlock Avoidance
When each resource type has only one instance, a Resource Allocation Graph can be extended for deadlock avoidance.
In addition to normal request and assignment edges, the system may represent a claim edge.
A claim edge means:
“This process may request this resource later.”
Before converting a request into an actual assignment, the operating system checks whether the new graph would create an unsafe cycle.
Banker's Algorithm in Operating System
Banker's Algorithm is a deadlock-avoidance algorithm for systems with multiple instances of resource types.
The name comes from the idea of a banker who lends money only when the banker can still satisfy all customers eventually.
Banker's Algorithm Solved Example
Assume the system has five processes: P0, P1, P2, P3 and P4.
There are three resource types: A, B and C.
Current Available vector:
Available = (3, 3, 2)
Allocation Matrix
| Process | A | B | C |
|---|---|---|---|
| P0 | 0 | 1 | 0 |
| P1 | 2 | 0 | 0 |
| P2 | 3 | 0 | 2 |
| P3 | 2 | 1 | 1 |
| P4 | 0 | 0 | 2 |
Maximum Demand Matrix
| Process | A | B | C |
|---|---|---|---|
| P0 | 7 | 5 | 3 |
| P1 | 3 | 2 | 2 |
| P2 | 9 | 0 | 2 |
| P3 | 2 | 2 | 2 |
| P4 | 4 | 3 | 3 |
Step 1: Calculate Need Matrix
Use:
Need = Max − Allocation
| Process | Need A | Need B | Need C |
|---|---|---|---|
| P0 | 7 | 4 | 3 |
| P1 | 1 | 2 | 2 |
| P2 | 6 | 0 | 0 |
| P3 | 0 | 1 | 1 |
| P4 | 4 | 3 | 1 |
Safety Algorithm Step by Step
The safety algorithm checks whether the current allocation state is safe.
Initially: Work = (3, 3, 2).
Work = Work + Allocation
Finding the Safe Sequence
P1 Can Finish First
P1 Need: (1,2,2)
Current Work: (3,3,2)
Since:
(1,2,2) ≤ (3,3,2)
P1 can finish.
P1 returns its Allocation: (2,0,0).
New Work:
(3,3,2) + (2,0,0) = (5,3,2)
P3 Can Finish Next
P3 Need: (0,1,1)
It fits within Work: (5,3,2).
P3 releases Allocation: (2,1,1).
New Work: (7,4,3).
P4 Can Now Finish
P4 Need: (4,3,1)
This is less than or equal to: (7,4,3).
P4 releases: (0,0,2).
New Work: (7,4,5).
P0 Can Finish
P0 Need: (7,4,3)
Work: (7,4,5)
So P0 can finish.
New Work after P0 releases its allocation: (7,5,5).
P2 Can Finish Last
P2 Need: (6,0,0)
Work: (7,5,5)
So P2 can also complete.
Resource-Request Algorithm in Banker's Algorithm
The safety algorithm tells us whether a state is safe.
But when a process makes a new request, the operating system must decide whether to grant it.
A process should not request more than its declared remaining maximum requirement.
Enough free resource instances must currently exist.
Temporarily allocate the request and run the safety algorithm.
Simple Request Example
Suppose P1 requests:
Request1 = (1,0,2)
P1 Need is: (1,2,2).
So:
(1,0,2) ≤ (1,2,2)
Available is: (3,3,2).
And:
(1,0,2) ≤ (3,3,2)
The operating system can temporarily grant it.
New Available becomes:
(2,3,0)
New P1 Allocation becomes:
(3,0,2)
New P1 Need becomes:
(0,2,0)
The safety algorithm can still find a safe sequence, so this request can be granted.
Advantages and Limitations of Banker's Algorithm
Advantages
Prevents the system from entering unsafe allocation states.
Gives a clear mathematical method for reasoning about multiple resource instances.
Limitations
Processes must declare maximum possible resource requirements in advance.
Resource counts and process sets may change dynamically in real systems.
Repeated safety checks add overhead.
Deadlock Detection in Operating System
A system may decide not to prevent or avoid every dangerous allocation.
Instead, it can allow processes to request resources more freely and periodically run a deadlock detection algorithm.
Deadlock detection is the process of examining the current allocation and waiting relationships to determine whether a deadlock already exists.
Deadlock Detection With Wait-for Graph
When each relevant resource type has one instance, a Wait-for Graph can directly show process dependencies.
Suppose the complete dependency is:
P1 → P2 → P3 → P1
That creates a cycle in the wait-for graph.
Deadlock Detection Algorithm for Multiple Resource Instances
When resource types can have several instances, cycle detection alone is not enough.
A detection algorithm can use:
Available, Allocation and Request.
Work = Work + Allocation[i]
Deadlock Detection Algorithm in Pseudocode
How Often Should Deadlock Detection Run?
Running a detection algorithm continuously can create unnecessary overhead.
Running it too rarely can allow many processes to remain stuck for a long time.
A system may consider factors such as:
Deadlock Recovery in Operating System
Detection only tells the system that deadlock exists.
The operating system must still break the deadlock. This stage is called Deadlock Recovery.
Deadlock recovery is the set of actions used to break an existing deadlock and allow the system to make progress again.
Process Termination
Terminate one or more processes involved in the deadlock.
Resource Preemption
Temporarily take selected resources away from one process and allocate them elsewhere when this can be done safely.
Recovery by Process Termination
The simplest recovery idea is to terminate processes involved in the deadlock.
Option 1: Terminate All Deadlocked Processes
The deadlock is broken immediately because all resources held by those processes are released.
But a large amount of completed work may be lost.
Option 2: Terminate One Process at a Time
Select one victim, terminate it and check whether the deadlock disappears.
If not, terminate another victim.
This may preserve more work but requires repeated detection checks.
How Is a Victim Process Selected?
If the operating system does not want to terminate every deadlocked process, it needs a way to select a victim.
Recovery by Resource Preemption
Instead of terminating a process, the operating system may attempt to preempt selected resources.
Resource preemption means temporarily taking a resource from one process and giving it to another process so that the circular dependency can be broken.
What Is Rollback in Deadlock Recovery?
If a resource is taken away from a process, simply continuing from the middle may not always be safe.
The process may need to return to an earlier consistent state. This is called rollback.
Starvation During Deadlock Recovery
Suppose the same process is always chosen as the victim because it has low priority or low recovery cost.
That process may repeatedly lose resources or repeatedly roll back.
It may never finish.
This becomes a form of starvation.
Deadlock Prevention vs Deadlock Avoidance
| Point | Prevention | Avoidance |
|---|---|---|
| Main Idea | Break a necessary deadlock condition | Grant resources only if the state remains safe |
| Maximum Demand Information | Not always required | Usually required |
| Resource Utilization | Can be reduced by strict rules | Can be better than strict prevention |
| Decision Style | Static restrictions | Dynamic safety checks |
| Example | Global resource ordering | Banker's Algorithm |
Prevention vs Avoidance vs Detection vs Recovery
| Method | When It Acts | Main Idea | Typical Example |
|---|---|---|---|
| Prevention | Before deadlock can form | Break a necessary condition | Resource ordering |
| Avoidance | Before granting each risky request | Stay in a safe state | Banker's Algorithm |
| Detection | After allocations are allowed | Check whether deadlock exists | Wait-for Graph / detection matrices |
| Recovery | After deadlock is detected | Break existing dependency | Termination or resource preemption |
Which Deadlock Handling Method Should Be Used?
The choice depends on the type of system and the cost of deadlock.
Common Mistakes Students Make
Prevention breaks a necessary condition. Avoidance dynamically checks whether resource allocation keeps the state safe.
Unsafe means deadlock cannot be ruled out. Deadlocked means progress is already blocked.
The test compares remaining Need against current Work.
Need represents how many more resources a process may still require.
Another process may satisfy Need ≤ Work and provide a valid safe sequence.
Avoidance acts before allocation. Detection looks for deadlock after allocations exist.
Detection considers current outstanding requests, not declared maximum future demand.
Systems can also terminate selected victims, preempt resources or use rollback.
Complete Deadlock Handling Flow in Simple Words
Deadlock Handling Exam Preparation
For a 2-Mark Question
Answer: Deadlock can be handled through prevention, avoidance, detection and recovery. Prevention breaks a necessary deadlock condition, avoidance keeps the system safe, detection identifies an existing deadlock, and recovery breaks that deadlock.
For a 5-Mark Question
Explain:
- deadlock prevention
- deadlock avoidance
- safe and unsafe state
- deadlock detection
- deadlock recovery
For a 10-Mark Question
Write in this order:
- methods of handling deadlock
- prevention of Mutual Exclusion
- prevention of Hold and Wait
- resource preemption
- resource ordering to prevent Circular Wait
- deadlock avoidance
- safe, unsafe and deadlocked states
- Banker's Algorithm
- safety algorithm
- resource-request algorithm
- deadlock detection
- Wait-for Graph
- multiple-instance detection algorithm
- process termination
- resource preemption, rollback and victim selection
For a Banker's Algorithm Numerical
Golden flow:
Calculate Need = Max − Allocation → initialize Work = Available → find Need ≤ Work → add released Allocation to Work → repeat → write the safe sequence.
Deadlock Prevention, Avoidance, Detection & Recovery Practice Questions
Deadlock Handling Quick Revision
Frequently Asked Questions About Deadlock Handling in Operating System
What are the methods of handling deadlock in Operating System?
The major methods are deadlock prevention, deadlock avoidance, deadlock detection and deadlock recovery.
What is deadlock prevention?
Deadlock prevention is a method that makes at least one necessary condition for deadlock impossible through resource-allocation rules.
How can circular wait be prevented?
Circular wait can be prevented by giving resource types a fixed order and requiring processes to request resources only according to that order.
What is deadlock avoidance?
Deadlock avoidance examines resource requests before granting them and grants a request only if the resulting system state remains safe.
What is a safe state in Operating System?
A safe state is one in which at least one sequence exists that allows every process to obtain its remaining required resources, finish and release its allocated resources.
What is an unsafe state?
An unsafe state is one in which the operating system cannot guarantee a safe completion sequence based on the current allocation and declared maximum demands.
Is an unsafe state the same as deadlock?
No. An unsafe state means deadlock may become possible. A deadlocked state means processes are already unable to make progress because of resource dependencies.
What is Banker's Algorithm?
Banker's Algorithm is a deadlock-avoidance algorithm that checks whether granting a resource request keeps the system in a safe state.
What is the Need matrix in Banker's Algorithm?
The Need matrix represents the remaining maximum resource requirement of each process and is calculated as Need = Max − Allocation.
What is the safety algorithm?
The safety algorithm simulates whether processes can complete one by one using currently available resources plus resources released by previously completed processes. If all processes can finish, the state is safe.
What is the resource-request algorithm?
The resource-request algorithm checks whether a process's request is within its remaining Need and current Available resources. It then temporarily grants the request and runs the safety algorithm before making the allocation permanent.
What is deadlock detection?
Deadlock detection allows resource allocations to occur and later analyzes process and resource relationships to determine whether deadlock exists.
How is deadlock detected when each resource type has one instance?
A Wait-for Graph can be used. If the graph contains a cycle, the processes in that cycle are deadlocked.
How is deadlock detected with multiple resource instances?
A matrix-based detection algorithm can use Available, Allocation and current Request information to determine which processes can finish. Processes that remain unfinished are part of the detected deadlocked set.
What is deadlock recovery?
Deadlock recovery is the process of breaking an existing deadlock after it has been detected.
How can an operating system recover from deadlock?
The system can terminate deadlocked processes, terminate selected victims, preempt suitable resources, roll processes back or combine these techniques.
What is victim selection in deadlock recovery?
Victim selection means choosing the process or resource whose termination or preemption will break deadlock with acceptable cost.
What is rollback in deadlock recovery?
Rollback restores a process to an earlier consistent state so that resources can be reclaimed and the process can later restart from that point.
What is the difference between prevention and avoidance?
Prevention permanently restricts resource allocation so that a necessary deadlock condition cannot hold. Avoidance dynamically evaluates individual requests and grants them only when the state remains safe.
What is the difference between avoidance and detection?
Avoidance acts before granting a potentially unsafe request. Detection allows allocations and later checks whether deadlock has actually occurred.
Conclusion
Deadlock Prevention, Avoidance, Detection and Recovery in Operating System are four different answers to the same problem: how should the operating system handle processes that compete for limited resources?
Deadlock Prevention changes resource-allocation rules so that one necessary deadlock condition cannot occur. Deadlock Avoidance is more flexible and checks each risky request before granting it.
The key avoidance idea is the safe state. Banker's Algorithm uses Available, Allocation, Max and Need to determine whether a safe sequence exists.
Deadlock Detection takes the opposite approach: allow allocation and later inspect whether processes are actually deadlocked. Single-instance systems can use a Wait-for Graph, while multiple-instance systems require a more detailed detection algorithm.
Finally, Deadlock Recovery restores progress by terminating processes, preempting resources, rolling processes back or choosing a suitable victim.
The easiest way to remember the full chapter is: Prevention stops the possibility, Avoidance stays safe, Detection finds the problem, and Recovery breaks the problem.
