TheHingineer

  • Operating System


  • OS Part-1

  • OS Part-2

  • OS Part-3

  • OS Part-4

  • OS Part-5

  • Deadlock Prevention and Avoidance in Operating System

    Deadlock ek aisi situation hoti hai jisme multiple processes ek dusre ka wait karte hain aur system halt ho jata hai (ruk jata hai). Is problem ko solve karne ke liye Deadlock Prevention aur Deadlock Avoidance ka use kiya jata hai.


    Deadlock Prevention

    Deadlock prevention ka matlab hai ki deadlock hone se pehle hi usko rok diya jaye.

    🔹 Coffman’s Conditions ke 4 rules hote hain jo deadlock hone ke liye zaroori hote hain:

    1. -> Mutual Exclusion (Resource ek samay par sirf ek process ke paas ho)

    2. -> Hold and Wait (Process ek resource hold karke dusre ka wait kare)

    3. -> No Preemption (Resources zabardasti nahi liye ja sakte)

    4. -> Circular Wait (Processes ek cycle me ek dusre ka wait karein)

    Deadlock Prevention Ka Tarika

    Agar hum in 4 conditions me se koi ek tod de, toh deadlock prevent ho sakta hai.

    Coffman’s Condition Deadlock Prevention Technique
    Mutual Exclusion Jitne ho sake utne resources ko shareable banaya jaye. (Jaise: Read-Only files)
    Hold and Wait Process ko saare resources ek saath request karne honge ya bina resource hold kiye wait karna hoga.
    No Preemption Agar ek process ko resource mil gaya hai lekin use complete nahi kar raha, toh system forcefully us resource ko free kar sakta hai.
    Circular Wait System ek fixed order me resource allocation karega jisse circular wait na bane.

    🔹 Example:
    Sochiye ek printing system hai jisme Printer aur Scanner dono resources available hain.

    • Without Prevention: Ek process printer lega, dusra scanner le lega, aur dono ek dusre ka wait karenge → Deadlock!

    • With Prevention: System decide karega ki pehle scanner milega phir printer. Isse circular wait break ho jayega.


    Deadlock Avoidance

    Deadlock avoidance ka matlab hai ki system hamesha check kare ki ek resource assign karne ke baad deadlock hoga ya nahi.

    🔹 How it Works?

    • Process ko pahle hi batana padega ki usko kitne resources chahiye.

    • System tabhi resource dega jab confirm ho ki koi circular wait nahi hoga.

    • Safe State aur Unsafe State ka concept use hota hai.

    Safe State vs Unsafe State

    ✔ Safe State: Agar system kisi process ko resource deke bhi deadlock avoid kar sakta hai, toh wo safe state me hai.
    ❌ Unsafe State: Agar system kisi process ko resource deke deadlock me fas sakta hai, toh wo unsafe state me hai.

    💡 Example:
    Maan lijiye 3 processes (P1, P2, P3) hain aur system ke paas sirf 10 resources hain.

    Process Maximum Need Already Allocated Remaining Need
    P1 7 2 5
    P2 6 3 3
    P3 5 2 3

    Agar hum bina check kiye koi bhi resource allocate kar dete hain, toh system unsafe state me chala jayega aur deadlock ho sakta hai.


    Banker’s Algorithm (Deadlock Avoidance Technique)

    🔹 Banker’s Algorithm ek deadlock avoidance technique hai jo check karta hai ki system safe hai ya nahi.

    🔹 Steps:

    1. Har process ko pehle batana hota hai ki usko maximum kitne resources chahiye.

    2. Jab bhi ek process resource maangta hai, toh system check karta hai ki agar ye resource de diya jaye toh bhi koi deadlock toh nahi hoga.

    3. Agar safe state hai, toh resource de diya jata hai.

    🔹 Example:
    Maan lijiye ek bank hai jisme 5 customers hain aur sirf 10 lakh rupees hain.

    • Agar koi customer poora paisa ek saath le jaye, toh bank bankrupt ho sakta hai.

    • Isliye Banker’s Algorithm ensure karta hai ki paise is tarah allocate kiye jayein ki koi bhi deadlock na ho.


    Conclusion

    ✅ Deadlock Prevention ka matlab hai deadlock hone se pehle hi usko rok dena.
    ✅ Deadlock Avoidance ka matlab hai pehle check karna ki deadlock hoga ya nahi.
    ✅ Banker’s Algorithm ek popular deadlock avoidance technique hai jo safe state maintain karta hai.

    Scroll to Top