TheHingineer

  • DBMS


  • DBMS Part-1

  • DBMS Part-2

  • DBMS Part-3

  • DBMS Part-4

  • DBMS Part-5

  •  Deadlock in DBMS 


     Deadlock kya hota hai? 

    DBMS mein deadlock ek aisi situation hoti hai jahan 2 ya zyada transactions ek dusre ka wait karte rehte hain resource release krne ke liye, aur koi bhi aage nahi badh pata.

    example:

    Imagine:

    • Person A ke pas pen hai
    • Person B ke pas notebook hai
    • A bolta hai “Mai sirf tabhi hi likhunga jab mjhe notebook milegi”
    • B bolta hai “Mai tumhe notebook tabhi hi dunga jab mujhe pen milegi”

       Koi bhi nahi likh pata na hi A, na hi B .ise hi deadlock kehte hai.


     Deadlock ko handle karna kyun zaroori hai?

    Agar hum deadlock ko handle nahi karte:

    • Transactions indefinitely wait karte rahenge

    • System hang ya slow ho sakta hai

    • Performance aur data consistency kharaab ho jaati hai

     Deadlock ko handle karne ke 4 main tareeke

    1. Deadlock Prevention
    2. Deadlock Avoidance
    3. Deadlock Detection & Recovery
    4. Deadlock Ignorance (chhoti systems mein)

    1. Deadlock Prevention 

    Goal: Deadlock hone hi na do. Isme hum 4 Coffman Conditions me se koi ek tod dete hain.

     Coffman Conditions:

    Condition Matlab
    Mutual Exclusion Ek resource ek waqt mein sirf ek hi le sakta hai (resource non sharable hai)
    Hold and Wait Transaction ek resource pakad ke dusra maangta hai
    No Preemption Resource zabardasti nahi cheen sakte
    Circular Wait Transactions ek dusre ka wait kar rahe hote hain in cycle

     Kaise rok sakte hain?

    a) Hold and Wait todna

    Transaction ko bolte hain ki saare resources ek saath maango. Agar mil gaye to chalo, warna wait karo.

     Example:
    T1 ko A aur B chahiye — dono ek saath maange, nahi to wait kare.

    b) Circular Wait todna

    Resources ko number de do (e.g. A=1, B=2, C=3). Har transaction ko increasing order mein hi resource maangna hoga.

     Example:
    T1 ne A (1) le liya — ab sirf B (2) ya C (3) maang sakta hai, A se chhota resource nahi.

    2. Deadlock Avoidance 

    Goal: Deadlock hone se pehle hi check karo ki situation safe hai ya nahi.

     Safe State:

    Aisi condition jahan har transaction safely complete ho sakta hai.

     Unsafe State:

    Aisi condition jahan aage jaake deadlock ho sakta hai.

     Algorithm: Banker’s Algorithm

     Example:
    T1 resource A maangta hai → system check karega:

    • Kya sabhi transactions safe tareeke se complete ho sakte hain?

    • Agar haan → resource de do

    • Agar nahi → wait karvao

    Iske liye har transaction ka maximum resource requirement pehle se pata hona chahiye

    3. Deadlock Detection & Recovery (Pehchaan ke baad sudharna)

    Goal: Deadlock hone do, par turant detect karo aur solve karo.

     Kaise detect karte hain?

    Use karte hain Wait-For Graph (WFG):

    • Har node = ek transaction

    • Edge = T1 is waiting for T2

     Cycle in graph = Deadlock

     Example Wait-for Graph

    T1 → T2 → T3
     ↑         ↓
     └─────────┘

     Ye cycle hai ⇒ deadlock hua

     Recovery kaise karte hain?

    Deadlock detect hone ke baad system kisi ek transaction ko rollback kar deta hai:

     Recovery options:

    • Sabse young transaction ko kill karo

    • Sabse purana transaction ko rollback karo

    • Least costly (kam resources) transaction rollback karo

    • Partial rollback bhi kar sakte ho

     Example:
    T1, T2, T3 deadlock mein hain. T2 ko rollback kar diya, uske resources free ho gaye, aur baaki aage badh gaye.

    4. Deadlock Ignorance (Ignore karna)

    Kuch DBMS (jaise MySQL) deadlock ko ignore karte hain:

    • Sochte hain deadlock rare hota hai

    • User ko manually transaction retry karna padta hai

     Ye approach serious systems ke liye safe nahi hai


     Comparison Table

    Method Kab Use Hota Hai Kaise Kaam Karta Hai Example
    Prevention Pehle se Deadlock ki condition tod deta hai Saare resources ek saath maangna
    Avoidance Resource allocation se pehle Safe/Unsafe state check karta hai Banker’s Algorithm
    Detection & Recovery Deadlock hone ke baad Cycle detect karta hai, rollback karta hai Wait-for Graph
    Ignore Small systems mein Khaali chhod dete hain, user handle kare Manual retry

     Final Summary

    • Deadlock tab hota hai jab transactions ek dusre ka wait karte hain aur koi bhi aage nahi badh pata

    • Hum deadlock ko prevent, avoid, detect or ignore kar sakte hain

    • Har method ka use depend karta hai system ki complexity, performance aur reliability pe

    Scroll to Top