Lesson 5 � Intermediate

OS: Deadlock Detection

Deadlock tab hota hai jab do processes ek doosre ke resources ko wait karte hain aur koi bhi release nahi karta � permanent blocking ho jaata hai.

? 20 min✓ Intermediate✓ Processes, Memory basics

Deadlock hota kya hai?

WHAT

Deadlock ek situation hai jab do ya zyada processes ek doosre ke resources ko permanently block kar deti hain. Koi bhi process aage nahi badh paata � circular wait ho jaata hai.

WHEN

Database transactions, multi-threaded applications, operating system resource allocation � deadlock kabhi bhi ho sakta hai jab 4 conditions simultaneously satisfy hon.

WHERE

Database locks, mutex in multi-threading, file locks, printer spooling � deadlock ki examples hain. Real systems mein deadlock detection aur recovery zaroori hai.

Deadlock ke 4 Conditions (Necessary)

# 1. Mutual Exclusion
✓ Resource ek time pe sirf ek process use kar sakte hain
✓ Printers, locks � exclusive hote hain

# 2. Hold and Wait
✓ Process ne resource hold kiya hai aur doosra resource wait kar rahi hai
? "Mere paas printer hai, mujhe scanner chahiye"

# 3. No Preemption
✓ Resource forcibly nahi le sakte � process khud chhodegi
✓ OS resource nahi cheen sakta

# 4. Circular Wait
✓ P1 ✓ P2 ✓ P3 ✓ P1 (circular chain)
✓ Sab ek doosre ka resource wait kar rahe hain

# DEADLOCK = All 4 conditions simultaneously true
✓ Agar ek bhi condition tod do ✓ deadlock nahi hoga!
Mental Model: Deadlock jaise traffic jam at a 4-way intersection. Agar 4 cars aayi hain � ek taraf se ek, doosri taraf se ek � sab wait kar rahe hain ki doosra jaaye. Koi nahi jaata = deadlock. Agar ek car ne rasta de diya (preemption) toh deadlock toot jaayega.

Deadlock Prevention

# Prevention: Koi ek condition tod do permanently

# 1. Mutual Exclusion todna
✓ Resources ko sharable banao
✓ Problem: Sab resources sharable nahi ho sakte
✓ Example: Printer � exclusive hona zaroori hai

# 2. Hold and Wait todna
✓ Process shuru mein saare resources ek saath le
✓ Ya resource chahiye toh pehle chhodo doosra
✓ Problem: Resource utilization kam hota hai

# 3. No Preemption todna
✓ Agar process naya resource na le sake toh pehle chhodo
✓ OS forcefully resource le sakta hai
✓ Problem: State save karna padta hai (complex)

# 4. Circular Wait todna
✓ Resources ko numerical order mein number karo
✓ Process sirf higher number le sake
✓ No circular chain possible
✓ BEST PRACTICE!

Deadlock Avoidance

# Avoidance: Runtime pe safe state ensure karo

# Banker's Algorithm (Dijkstra):
✓ OS bank jaise kaam karta hai
✓ Har process maximum needs bataata hai
✓ OS allocate tab karta hai jab safe state ho

# Safe State: Ek sequence hai jisme sab processes complete ho jaayein

# Example:
4 processes: P0, P1, P2, P3
3 resources: A(10), B(5), C(7)

Allocation: Max: Available:
P0: 0 1 0 7 5 3 3 3 2
P1: 2 0 0 3 2 2
P2: 3 0 2 9 0 2
P3: 2 1 1 2 2 2

Need = Max - Allocation:
P0: 7 4 3
P1: 1 2 2
P2: 6 0 1
P3: 0 1 1

Safe Sequence: P1 ✓ P3 ✓ P2 ✓ P0 ?

# If safe sequence exists ✓ State is safe ✓ Allocate
# If no safe sequence ✓ State is unsafe ✓ Don't allocate

Deadlock Detection

# Detection: Deadlock ho chuka hai ya nahi � find karo

# Wait-for Graph:
✓ Edge: Pi ✓ Pj means Pi is waiting for Pj
✓ CYCLE in graph = DEADLOCK!

# Detection Algorithm:
✓ Periodically run karo detection algorithm
✓ Resources allocated, requested check karo
✓ Cycle detect karo

# Recovery Options:
1. Process Termination
 ✓ Sabse kam priority wala process terminate karo
 ✓ Ya sab processes terminate karo ( drastic)

2. Resource Preemption
 ✓ Kisi process se resource cheeno
 ✓ Rollback karo previous safe state pe

# Trade-off:
✓ Detection overhead vs Deadlock impact
✓ High stakes systems (banking) mein regularly run karo
✓ Low stakes systems mein recovery strategy rakho

Exercise

Question: Deadlock hone ke liye kitni conditions simultaneously true honi chahiye? (1 word)

Question: Banker's Algorithm allocate tab karta hai jab state kya ho? (2 words)

Common mistakes

Lesson complete?

Deadlock samajh aa gaya✓ Ab DBMS ke topics shuru karte hain � B-Tree Indexing seekhte hain.