Q.5)

 Define deadlock in DBMS. How can deadlocks be detected and resolved? Explain with an example.

Deadlock Kya Hai? (What is Deadlock?)

Deadlock DBMS mein ek aisi situation (paristhiti) hoti hai jahan do ya usse zyada transactions hamesha ke liye block (atak) ho jate hain. Aisa isliye hota hai kyunki har transaction ne kam se kam ek resource (jaise koi table ya data) ko pakad rakha (lock kar rakha) hota hai, aur wo kisi dusre resource ka wait kar raha hota hai jise kisi aur transaction ne lock kiya hua hai.

Kyunki saare transactions ek circular (gol) tarike se ek dusre ka wait kar rahe hote hain, unme se koi bhi aage nahi badh pata aur poora system wahin atak jata hai (stuck ho jata hai).

Kahan hota hai ye? Deadlock aamtaur par multi-user database systems mein hota hai jahan bahut saare transactions ek sath chalte hain aur shared resources (jaise data items, tables, ya locks) ke liye aapas mein compete karte hain.


Deadlock Ki Conditions (Conditions for Deadlock)

Deadlock sirf tabhi hota hai jab yeh charo conditions ek sath (same time par) satisfy hoti hain. Agar inme se ek bhi condition toot jaye, toh deadlock nahi hoga:

  1. Mutual Exclusion (Sirf Ek Ka Adhikar): Kam se kam ek resource aisa hona chahiye jo shareable na ho (non-shareable). Matlab ek time par us resource ko sirf ek hi transaction use kar sake.

  2. Hold and Wait (Pakadna aur Intezaar Karna): Ek transaction ne already ek resource ko hold (lock) kiya hua hai, aur wo dusre resources ka wait kar raha hai jo dusre transactions ke paas hain.

  3. No Preemption (Zabardasti Nahi Chheen Sakte): Kisi bhi transaction se uska locked resource zabardasti nahi chheena ja sakta. Transaction jab apna kaam poora kar lega, tabhi wo apni marzi se (voluntarily) us resource ko release karega.

  4. Circular Wait (Gol-Gol Intezaar): Transactions ki ek aisi circular chain ban jati hai jahan har transaction us resource ka wait kar raha hota hai jo agle transaction ne hold kiya hua hai.


Deadlock Ka Example (Example of Deadlock)

Chaliye ise visualize karte hain:

Maan lijiye do transactions hain T1 aur T2, aur do resources hain A aur B:

  • T1 ne resource A ko lock kar liya hai aur ab wo resource B ke free hone ka wait kar raha hai.

  • T2 ne resource B ko lock kar liya hai aur ab wo resource A ke free hone ka wait kar raha hai.

Ab kya hoga?

  • T1 aage nahi badh sakta kyunki B ko T2 ne pakda hua hai.

  • T2 aage nahi badh sakta kyunki A ko T1 ne pakda hua hai.

Yeh ek circular waiting condition (gol chakkar) bana deta hai jahan dono transactions hamesha ke liye block ho jate hain. Isi situation ko Deadlock kehte hain.


Deadlock Ke Effects (Asar ya Nuksan)

Agar system mein deadlock aa jaye, toh uske yeh nuksan hote hain:

  1. System ki performance bahut slow ho jati hai kyunki transactions atak jate hain aur complete nahi hote.

  2. Resources locked reh jate hain aur unhe koi dusra transaction use nahi kar pata.

  3. Waiting processes ki wajah se system ka CPU aur memory waste hota hai.

  4. Database ka throughput (ek time mein complete hone wale kaam ki speed) kafi kam ho jata hai.

  5. Agar situation severe (gambhri) ho jaye, toh poora system hang ho sakta hai ya respond karna band kar sakta hai.


Deadlock Handling (Deadlock Se Kaise Niptein?)

Deadlock handling ka matlab hai wo techniques (tarike) jo DBMS use karta hai taaki deadlock ko manage, prevent (rokna), detect (pata lagana), aur resolve (theek) kiya ja sake, jisse system smoothly chalta rahe. Iske 4 main tarike hain:

1. Deadlock Prevention (Pehle hi rok dena)

Yeh ek aisi technique hai jo ensure karti hai ki system mein kabhi deadlock ki conditions hi paida na hon. Yeh rules ko strict kar deta hai.

  • Yeh deadlock ki 4 zaruri conditions mein se kam se kam ek ko banne se rokat hai.

  • Hold-and-wait ko rokna: Yeh rule bana deta hai ki transaction ko apne saare resources ek hi baar mein shuru mein maangne padenge.

  • Circular wait ko rokna: Yeh resources ko ek fixed order (numbering) mein allot karta hai taaki cycle na bane.

  • Yeh ek proactive approach hai (pehle se taiyar rehna) jo deadlock aane hi nahi deta.

  • Nuksan: Strict rules ki wajah se system ki efficiency kam ho jati hai.

2. Deadlock Avoidance (Bach ke nikalna)

Is technique mein system koi bhi resource allocate karne se pehle check karta hai ki kya yeh request puri karne se system aage chalkar deadlock mein toh nahi phasega (kya system "safe state" mein rahega?).

  • Yeh ensure karta hai ki system hamesha safe state mein rahe.

  • Resources tabhi diye jate hain jab future mein unse deadlock na ho.

  • Iske liye system ko pehle se pata hona chahiye ki kis transaction ko maximum kitne resources chahiye.

  • Banker’s Algorithm deadlock avoidance ke liye sabse famous algorithm hai.

  • Yeh prevention se zyada efficient hai, par isko implement karna thoda complex (mushkil) hota hai.

3. Deadlock Detection (Pata lagana)

Is method mein system deadlock ko aane deta hai (rokat nahi hai), par time-to-time check karta rehta hai ki kya system mein deadlock aa chuka hai.

  • System periodically (kuch der baad) check karta hai ki deadlock hai ya nahi.

  • Iske liye ek Wait-For Graph ka use kiya jata hai. Is graph mein nodes transactions hote hain aur edges (lines) yeh dikhati hain ki kaun kiska wait kar raha hai.

  • Agar is graph mein koi cycle (gol chakkar) milta hai, toh iska matlab deadlock hai.

  • Yeh un systems ke liye acha hai jahan deadlock bahut kam (rarely) aate hain.

4. Deadlock Recovery (Theek karna)

Jab Detection method ko pata chal jata hai ki deadlock aa chuka hai, tab usko theek karne ke process ko Recovery kehte hain.

  • Cycle ko todne ke liye deadlock mein phase hue ek ya usse zyada transactions ko terminate (kill/band) kar diya jata hai.

  • System chune gaye transactions ko rollback kar deta hai (yani unhe wapas shuruati halat mein bhej deta hai) taaki unke locked resources free ho jayein.

  • Kise kill karna hai (victim transaction), yeh is baat par depend karta hai ki uski cost kya hai, priority kya hai, ya wo kitni der se chal raha hai.

  • Jaise hi resources free hote hain, baaki transactions apna kaam normally aage badhate hain.



Key Points (Mukhy Bindu)

  1. Deadlock wo situation hai jahan transactions ek dusre ke resources ke liye hamesha ke liye wait karte reh jate hain.

  2. Yeh transactions ke beech circular waiting ki wajah se hota hai.

  3. Iske liye char conditions zaruri hain: mutual exclusion, hold and wait, no preemption, aur circular wait.

  4. Example: T1 T2 ka wait kar raha hai, aur T2 T1 ka.

  5. Deadlock se system slow hota hai aur resources waste hote hain.

  6. Deadlock handling ke methods hain: Prevention, Avoidance, Detection, aur Recovery.

  7. Prevention deadlock ko hone hi nahi deta.

  8. Avoidance Banker's Algorithm jaisi techniques se safe allocation ensure karta hai.

  9. Detection wait-for graph ka use karke deadlock ka pata lagata hai.

  10. Recovery deadlock mein phase transactions ko terminate (kill) karke system ko theek karta hai.

  11. Sahi deadlock management kisi bhi DBMS ki achi performance ke liye bahut zaroori hai.

Comments

Popular posts from this blog

Q.1)

Q.2)