TheHingineer

  • Operating System


  • OS Part-1

  • OS Part-2

  • OS Part-3

  • OS Part-4

  • OS Part-5

  • Deadlock Detection aur Recovery – Operating System

    Deadlock ek aisi situation hoti hai jisme ek ya zyada processes resources ke wait me atak jati hain aur kabhi complete nahi ho pati. Agar deadlock prevention aur avoidance techniques ka use nahi kiya gaya hai, toh system ko deadlock detect karna padta hai aur usse recover karna hota hai.


    Deadlock Detection

    Jab system me deadlock hone ka chance hota hai, toh system ek detection algorithm ka use karke check karta hai ki koi circular wait ho raha hai ya nahi.

    Deadlock Detect Karne Ke Tarike

    1. 1. Resource Allocation Graph (RAG) Cycle Detection

    2. 2. Wait-For Graph (WFG) Method

    3. 3. Detection Algorithm for Multiple Instances


    1. Resource Allocation Graph (RAG) Cycle Detection

    Agar sirf ek instance per resource hai, toh Resource Allocation Graph (RAG) ka use karke cycle detection se deadlock detect kiya jata hai.

    🔹 Example:
    Agar P1 → R1 → P2 → R2 → P1 ka ek cycle ban raha hai, toh ye deadlock hai!

    💡 Diagram:

    P1 → R1 → P2
    ↑         ↓
    R2  ←   P1
     

    ✔ Cycle Present → Deadlock Confirmed


    2. Wait-For Graph (WFG) Method

    • Jab sirf processes ka relation dekha jata hai (resources ko hata ke), toh usko Wait-For Graph (WFG) kehte hain.

    • Agar graph me cycle detect hota hai, toh deadlock confirm hota hai.

    💡 Example:
    Agar P1 → P2 → P3 → P1 ek cycle me hai, toh system deadlock me hai.

    ✔ Cycle Hai → Deadlock Confirm
    ❌ Cycle Nahi Hai → Deadlock Nahi Hai


    3. Detection Algorithm for Multiple Instances

    Agar ek resource ke multiple instances available hain, toh ek matrix-based detection algorithm ka use kiya jata hai:

    • Available Matrix: Kaunse resources abhi available hain.

    • Allocation Matrix: Har process ke paas kaunse resources hain.

    • Request Matrix: Har process ko aur kitne resources chahiye.

    Algorithm Steps:

    1. 1. Har process ke liye check karo ki kya uske required resources available hain.

    2. 2. Agar available hain, toh allocate karke process complete hone do.

    3. 3. Agar koi process complete nahi ho sakta, toh deadlock detect ho jayega.


    Deadlock Recovery

    Jab system detect kar leta hai ki deadlock ho gaya hai, toh system ko usse nikalne ke liye recovery techniques ka use karna padta hai.

    Deadlock Recovery Ke Tarike:

    1. 1. Process Termination (Abort)

    2. 2. Resource Preemption (Forcibly Take Resources)


    1. Process Termination (Abort)

    System deadlock todne ke liye kuch processes ko forcefully terminate (kill) kar sakta hai.

    🔹 Do Tarike:

    • Abort All Deadlocked Processes: Saare deadlocked processes ko terminate kar do.

    • Abort One by One: Ek-ek process ko terminate karke check karo ki deadlock khatam hua ya nahi.

    ❌ Downside: Agar kisi important process ko terminate kiya gaya, toh data loss ho sakta hai.


    2. Resource Preemption (Forcibly Take Resources)

    • System kuch resources ko zabardasti wapas le sakta hai aur kisi aur process ko de sakta hai.

    • Rollback: Agar kisi process ko resources nahi mile, toh usko pichle state par wapas le aaya jaye.

    🔹 Steps:

    1. 1. Select Victim Process: Konsa process ka resource le sakte hain.

    2. 2. Rollback Process: Process ko pehle wale state me le jao.

    3. 3. Starvation Avoidance: Agar ek process baar-baar preempt hota hai, toh usko priority di jaye.

    💡 Example:
    Agar ek printer P1 ke paas hai, aur P2 bhi printer maang raha hai, toh system P1 se printer wapas le sakta hai aur P2 ko de sakta hai.


    Conclusion

    ✅ Deadlock Detection system ko check karta hai ki koi process circular wait me fasa hai ya nahi.
    ✅ Deadlock Recovery ke liye system process terminate kar sakta hai ya resources ko forcibly wapas le sakta hai.
    ✅ Best Practice: Deadlock prevention techniques ka use karo taki detection aur recovery ki zaroorat hi na pade! 🚀

    Scroll to Top