Deadlock क्या है? Conditions, Handling और Starvation हिंदी में
Deadlock क्या है? — जब Processes एक-दूसरे का इंतज़ार करती रहें
Deadlock वह situation है जब दो या अधिक processes एक-दूसरे के resources का इंतज़ार करती रहती हैं — और कोई भी आगे execute नहीं कर पाती। System अटक जाता है, काम रुक जाता है।
यह OS की सबसे critical problems में से एक है। Deadlock में न कोई process complete होती है, न कोई resource free होता है — सब एक endless wait में फँसे रहते हैं।
Classic Example:
P1: Printer hold कर रहा है, Scanner चाहिए
P2: Scanner hold कर रहा है, Printer चाहिए
P1 → Scanner का इंतज़ार (P2 के पास है)
P2 → Printer का इंतज़ार (P1 के पास है)
Result: दोनों हमेशा wait करेंगे ❌
Real life में सोचें — दो लोग एक-दूसरे को रास्ता देने के लिए रुके हों और कोई आगे न बढ़े। यही deadlock है।
Deadlock के लिए 4 ज़रूरी Conditions
Deadlock तभी होगा जब ये चारों conditions एक साथ मौजूद हों। एक भी missing हो — deadlock नहीं होगा।
1. Mutual Exclusion
Resource एक समय में केवल एक process use कर सकती है। जब तक वो release नहीं करती, दूसरी process को नहीं मिलेगा।
2. Hold and Wait
Process एक resource hold किए बैठी है और दूसरे resource का इंतज़ार कर रही है — पहला छोड़ती नहीं।
3. No Preemption
Resource को forcefully वापस नहीं लिया जा सकता। जब तक process खुद release न करे, resource उसी के पास रहता है।
4. Circular Wait
Processes एक circle बनाती हैं — P1 → P2 का wait, P2 → P3 का wait, P3 → P1 का wait।
P1 ──needs──▶ R1
▲ │
│ held by
│ │
R2 ◀──held── P2
│
needs
▼
P1 (circular)
यह Circular Wait in Operating System का सबसे clear diagram है। इस circle को तोड़ दो — deadlock खत्म।
| Condition | अगर तोड़ दें तो? |
|---|---|
| Mutual Exclusion | Resources shareable बनाओ |
| Hold and Wait | सभी resources एक साथ माँगो |
| No Preemption | Forcefully resource वापस लो |
| Circular Wait | Resources को order में माँगो |
Deadlock Handling के 4 तरीके
Deadlock Handling
│
├── Prevention
├── Avoidance
├── Detection
└── Recovery
1. Prevention — Deadlock होने ही मत दो
4 conditions में से कोई एक condition को ही खत्म कर दो।
- Hold and Wait तोड़ो → process को सारे resources एक साथ allocate करो
- Circular Wait तोड़ो → resources को एक fixed order में allocate करो
Simple है, लेकिन इससे resource utilization कम हो जाती है।
2. Avoidance — Safe State Maintain करो
System हर allocation से पहले check करता है — क्या यह allocation safe रहेगी? अगर unsafe state में जाने का खतरा हो, तो allocation रोक दो।
इसका सबसे famous algorithm है — Banker's Algorithm।
New Request आई
│
▼
Safe State रहेगी?
│
┌──┴──┐
Yes No
│ │
Allocate Wait
Banker's Algorithm in Hindi में समझें — जैसे एक bank देखता है कि loan देने के बाद भी उसके पास minimum reserve रहेगी या नहीं। अगर नहीं रहेगी, loan नहीं देगा।
3. Detection — Deadlock को पहचानो
System deadlock को रोकता नहीं — होने देता है। फिर periodically check करता है कि deadlock हुआ है या नहीं। इसके लिए Resource Allocation Graph और Wait-for Graph use होते हैं।
4. Recovery — Deadlock से बाहर निकलो
Deadlock detect होने के बाद दो options हैं:
| Method | कैसे? |
|---|---|
| Process Termination | Deadlock में फँसी processes को kill करो |
| Resource Preemption | Forcefully resource वापस लो और दूसरी process को दो |
Process termination आसान है लेकिन महंगा — सारा काम बर्बाद हो सकता है। Resource preemption smarter है लेकिन implement करना मुश्किल।
Starvation क्या है?
Starvation वह situation है जब कोई process बहुत लंबे समय तक CPU या resource नहीं पा पाती — क्योंकि दूसरी higher priority processes बार-बार पहले आ जाती हैं।
Deadlock में सब फँसे होते हैं। Starvation में बाकी processes चल रही हैं — बस एक बेचारी process wait ही करती रह जाती है।
Example: Low priority process ready queue में बैठी है। हर बार कोई high priority process आ जाती है। Low priority process कभी CPU नहीं पाती।
Aging — Starvation का Solution
Aging वह technique है जिसमें लंबे समय से waiting process की priority धीरे-धीरे automatically बढ़ाई जाती है — ताकि वो eventually CPU पा सके।
Process की Initial Priority = 10 (Low) │ Wait करती रही... │ 5 seconds बाद → Priority = 8 10 seconds बाद → Priority = 6 15 seconds बाद → Priority = 4 │ ▼ Eventually High Priority → CPU मिलेगा ✅
Simple idea है लेकिन effective — जितना ज़्यादा wait करो, उतना ज़्यादा important हो जाओ।
Race Condition vs Deadlock vs Starvation
तीनों अलग-अलग problems हैं — exam में इन्हें confuse करना सबसे common mistake है।
| Feature | Race Condition | Deadlock | Starvation |
|---|---|---|---|
| कारण | Shared data का uncontrolled access | Circular waiting for resources | Low priority — हमेशा पीछे रह जाना |
| Effect | गलत result आता है | Execution पूरी तरह रुक जाती है | Process बहुत देर तक wait करती रहती है |
| System चलता है? | हाँ, पर wrong output | नहीं — सब stuck | हाँ, पर एक process को नहीं मिलता |
| Solution | Synchronization (Mutex/Semaphore) | Prevention / Avoidance / Detection | Aging |
एक line में याद रखें:
- Race Condition → गलत answer
- Deadlock → कोई answer नहीं
- Starvation → एक को answer नहीं मिलता
FAQ — Deadlock के बारे में Common Questions
Q1. Deadlock और Starvation में सबसे बड़ा फर्क क्या है?
Deadlock में सभी involved processes रुक जाती हैं — कोई progress नहीं होती। Starvation में system चलता रहता है, दूसरी processes execute होती हैं, लेकिन एक specific process को बहुत लंबे समय तक resource नहीं मिलता। Deadlock usually deadlock prevention/avoidance से handle होता है, Starvation को Aging से।
Q2. Banker's Algorithm कैसे काम करता है?
Banker's Algorithm हर resource request से पहले check करता है कि allocation के बाद system "safe state" में रहेगा या नहीं। Safe state का मतलब है — कोई न कोई sequence है जिसमें सभी processes complete हो सकती हैं। अगर safe sequence मिलती है, resource दो; नहीं मिलती तो process को wait कराओ।
Q3. Circular Wait को practically कैसे तोड़ते हैं?
Circular Wait तोड़ने का सबसे practical तरीका है — resources को एक fixed numbering/order देना। हर process को resources उसी order में माँगने होंगे। जैसे — पहले Printer (R1), फिर Scanner (R2), फिर Disk (R3)। कोई भी process R2 को पहले और R1 को बाद में नहीं माँग सकती। इससे circle बनना automatically impossible हो जाता है।
💬 Leave a Comment & Rating