Deadlock ka Funda

Understand how Deadlock works with Fullstackgada

🔒 Deadlock Kya Hai?

Deadlock: Jab do ya zyada processes ek-dusre ka intezaar karte-karte fas jaayein — na aage badh saken, na peeche!

💕 Gokuldham Love Triangle Deadlock

👨
Jethalal: Daya ko pakda hai, Babita ji chahiye
👨‍🦲
Iyer: Babita ko pakda hai, koi aur resource chahiye
👩
Babita: Jetha ji ka intezaar kar rahi, Iyer ko chhoda nahi
🔄
Result: Sabko circular wait — koi kisi ko nahi chhod raha!
Click above buttons to explore Deadlock concepts!

🎮 Live Deadlock Simulator

Scenario: Do processes resources ke liye fight kar rahe hain!
🔵 Process 1 (Jethalal)
Status: Ready
🔴 Process 2 (Iyer)
Status: Ready
💎 Resource A (Babita ji)
Owner: None
💍 Resource B (Daya)
Owner: None

Simulation Log:

Ready to simulate deadlock...

4️⃣ Deadlock ki char Conditions

Coffman Conditions: Yeh chaar conditions simultaneously honi chahiye deadlock ke liye!
// 🔐 Mutual Exclusion Condition
// Ek resource sirf ek process use kar sakta hai
Resource babita = new Resource("Babita Ji");
babita.setExclusive(true); // Only one can access
// Jethalal tries to access
if(babita.isAvailable()) {
babita.lockBy("Jethalal");
System.out.println("Jethalal ne Babita ji ko lock kar diya!");
}
// Iyer tries to access same resource
if(babita.isAvailable()) {
System.out.println("Iyer: Babita darling available nahi hai! 😢");
// Will have to wait...
}

🔐 Mutual Exclusion Real Example:

👩
Babita Ji: Ek time mein sirf ek hi person se baat kar sakti
🖨️
Printer: Ek time mein sirf ek document print kar sakta
💾
Database Lock: Ek record ko sirf ek transaction modify kar sakta
Click to test mutual exclusion condition...
// ⏳ Hold and Wait Condition
// Process kuch resources hold karta hai aur dusre ka wait karta hai
class Process {
List heldResources = new ArrayList<>();
List waitingFor = new ArrayList<>();
public void executeJethalal() {
// Jethalal already has Daya
heldResources.add(new Resource("Daya"));
System.out.println("Jethalal: Daya mere paas hai!");
// But wants Babita ji too
waitingFor.add(new Resource("Babita"));
System.out.println("Jethalal: Babita ji ka intezaar kar raha hoon!");
}
}
// Similarly Iyer holds Babita, wants something else
iyer.hold("Babita");
iyer.waitFor("SomeOtherResource");

⏳ Hold and Wait Scenarios:

👨
Jethalal: Daya को hold kiya, Babita का wait kar raha
👨‍🦲
Iyer: Babita को hold kiya, कुछ और resource चाहिए
💻
Database: Table A locked, Table B का wait kar रहा
Click to test hold and wait condition...
// 🚫 No Preemption Condition
// Resource ko forcefully nahi chin sakte
class Resource {
private String owner;
private boolean canPreempt = false;
public boolean forceRelease(String newOwner) {
if(!canPreempt) {
System.out.println(owner + " ne kaha: Main khud chhodunga!");
return false; // Cannot preempt
}
return true;
}
}
// Example usage
Resource babita = new Resource("Babita");
babita.lockBy("Iyer");
// Jethalal tries to preempt
if(!babita.forceRelease("Jethalal")) {
System.out.println("Jethalal: Iyer bhai khud chhodenge tab hi milegi!");
}

🚫 No Preemption Examples:

👨‍🦲
Iyer: "Babita darling mere saath hai, koi zabardasti nahi kar sakta!"
🖨️
Printer: Document print ho raha hai, beech mein nahi rok sakte
💾
File Lock: Process khud release karega, OS force nahi kar sakta
Click to test no preemption condition...
// 🔄 Circular Wait Condition
// Processes gol-gol wait kar rahe hain
// Circular dependency chain
Process jethalal = new Process("Jethalal");
Process iyer = new Process("Iyer");
Process popatlal = new Process("Popatlal");
// Jethalal holds Daya, wants Babita (held by Iyer)
jethalal.hold("Daya");
jethalal.waitFor("Babita"); // Iyer has this
// Iyer holds Babita, wants Rita (held by Popatlal)
iyer.hold("Babita");
iyer.waitFor("Rita"); // Popatlal has this
// Popatlal holds Rita, wants Daya (held by Jethalal)
popatlal.hold("Rita");
popatlal.waitFor("Daya"); // Back to Jethalal!
// Result: Circular wait - koi kisi ko nahi chhod raha!
// Jethalal -> Iyer -> Popatlal -> Jethalal (cycle!)

🔄 Circular Wait Visualization:

👨➡️👩
Jethalal: Daya hai, Babita chahiye (Iyer ke paas)
👨‍🦲➡️👩‍🦰
Iyer: Babita hai, Rita chahiye (Popatlal ke paas)
👨‍💼➡️👩
Popatlal: Rita hai, Daya chahiye (Jethalal ke paas)
🔄
Circle Complete: Sabko doosre ka intezaar, infinite loop!
Click to test circular wait condition...

🛡️ Deadlock Prevention Techniques

Prevention Strategy: Koi ek condition ko tod do, deadlock nahi hoga!
Technique Kaise Kaam Karta Hai Jethalal's Example Pros/Cons
🔑 Resource Ordering Resources ko hamesha same order mein allocate karo Pehle hamesha Daya, phir Babita - ulta nahi! ✅ Simple | ❌ Restrictive
⏰ Timeout Zyada der wait karo to query cancel kar do 5 minute wait kiya, ab chhod do intezaar! ✅ Practical | ❌ May lose work
⚡ Short Transactions Jaldi-jaldi kaam khatam kar do Babita se 2 minute baat, phir chhod do! ✅ Reduces deadlock chance | ❌ May need multiple txns
🔍 Auto Detection System khud deadlock pakad ke resolve kare Database samjh gaya - ek query ko rollback kar diya! ✅ Automatic | ❌ Some work lost

🌍 Real-world Deadlock Examples

Scenario Deadlock Situation How it Happens Solution
🏦 Banking System Two accounts transfer kar rahe hain Account A locks Account 1, Account B locks Account 2, dono ek-dusre ka wait Resource ordering - hamesha lower account number first
🛒 E-commerce Inventory update aur order processing Order locks product, inventory locks order table Short transactions, proper lock ordering
📱 Phone System Do calls simultaneously connect Call A dials B, Call B dials A at same time Call queueing, timeout mechanism
🚗 Traffic Intersection Chaar taraf se cars aa gayi Sabko straight jaana hai, koi peeche nahi hat sakta Traffic signals, roundabouts
💻 File System Two processes file access kar rahe Process 1 ne File A lock kiya, File B chahiye. Process 2 ulta File locking hierarchy, deadlock detection