Deadlock
Task: Watch two threads lock each other out
Why deadlock happens
Analogy
- Imagine two friends walking from opposite ends of a narrow bridge.
- Both refuse to step back, so neither can move forward.
- This is a deadlock: everyone is stuck, waiting for the other to act.
What is Deadlock?
Definition
- Deadlock is a situation in an operating system where two or more processes are stuck, each waiting for resources held by the other.
- None of them can proceed, causing the system to freeze for those tasks.
Necessary conditions
Deadlock occurs when all four conditions hold:
- Mutual Exclusion → resources can't be shared.
- Hold and Wait → processes hold resources while waiting for others.
- No Preemption → resources can't be forcibly taken away.
- Circular Wait → processes form a cycle, each waiting for the next.
Example in OS
- Process P1 holds a printer and waits for a scanner.
- Process P2 holds the scanner and waits for the printer.
- Neither can continue → deadlock.
Handling deadlock
- Prevention → avoid one of the four conditions.
- Avoidance → use algorithms like Banker's Algorithm.
- Detection & Recovery → allow deadlock, then detect and fix it.
Advantages of studying deadlock
- Helps design safer systems.
- Improves resource management.
- Builds foundation for advanced OS concepts like scheduling and concurrency.
Real-world example
- Traffic jam at a four-way intersection where each car waits for another to move.
- Unless one car backs up, no one moves forward.
🎯 Quick recap
Key takeaway
- Deadlock = processes stuck waiting for each other.
- Needs 4 conditions: mutual exclusion, hold & wait, no preemption, circular wait.
- Solutions: prevention, avoidance, detection & recovery.