Skip to content

Deadlock


Task: Watch two threads lock each other out

HELD BYHELD BYWAITING FORWAITING FORP1PrinterScannerP2CIRCULAR WAIT1.5s frozen

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:

  1. Mutual Exclusion → resources can't be shared.
  2. Hold and Wait → processes hold resources while waiting for others.
  3. No Preemption → resources can't be forcibly taken away.
  4. 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.
HELD BYWAITS FORHELD BYWAITS FORP1PrinterP2Scanner

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.