OS Deadlock – 4 Conditions and Banker’s Algorithm for GATE CSE 2026
Deadlock is most important topic of Operating System for GATE. 2-3 marks every year.
What is Deadlock?
Deadlock means 2 or more processes waiting for each other forever. No one finishes.
Example: Process P1 holds Resource R1 and wants R2. Process P2 holds R2 and wants R1. Both wait forever = Deadlock.
4 Necessary Conditions for Deadlock – GATE Favorite (Coffman Conditions)
All 4 must be true for deadlock:
- 1. Mutual Exclusion: Only one process can use resource at a time
- 2. Hold and Wait: Process holds one resource and waits for another
- 3. No Preemption: Resource cannot be taken forcibly
- 4. Circular Wait: Circular chain P1->P2->P3->P1 waiting
GATE Trick: If any 1 condition is false, NO deadlock!
Banker’s Algorithm – Deadlock Avoidance
Banker’s Algorithm is used to avoid deadlock. It checks safe sequence.
Important Terms:
Available: Free resources
Max: Maximum need of each process
Allocation: Currently allocated
Need = Max - Allocation
Safe Sequence means all processes can finish without deadlock.
Python Code Logic for GATE
def is_safe(available, max_need, allocation):
need = max_need - allocation
if need <= available:
# Process can finish
available += allocation
return True
return False
GATE PYQ on Deadlock
Q1. GATE CSE 2022
Q: Which condition can be removed to prevent deadlock?
Ans: Any one of the 4! Usually we break Circular Wait by ordering resources.
Q2. GATE CSE 2020 – Banker’s Algorithm
Q: 3 processes, Available = [3 3 2], Need = P0[0 0 2], P1[0 2 0], P2[1 0 0]. Safe sequence?
Ans: P1 -> P2 -> P0 is safe. Because P1 need [0 2 0] <= Available [3 3 2], so it finishes and releases.
Q3. GATE CSE 2019 – Deadlock Detection
Q: Resource Allocation Graph has cycle, deadlock?
Ans: If only single instance of each resource, cycle = deadlock. If multiple instances, cycle may or may not be deadlock (check Banker’s).
Deadlock Handling Methods
| Method | Use |
|---|---|
| Prevention | Break any 1 of 4 conditions |
| Avoidance | Banker’s Algorithm |
| Detection & Recovery | Allow deadlock, then kill process |
| Ignorance (Ostrich) | Used in Windows, Linux |
Conclusion
For GATE remember: 4 Conditions = M H N C (Mutual, Hold & Wait, No Preemption, Circular). Banker’s = Safe Sequence. 3 marks pakke!
Congratulations! Your 10 GATE posts are complete for GATESTUDY.IN
