Chapter 2 (Hindi)

7. Deadlock

Introduction of Deadlock in Operating System

Deadlock in Operating System

Deadlock एक ऐसी स्थिति है जिसमें दो या अधिक processes हमेशा के लिए stuck हो जाते हैं क्योंकि हर process दूसरे के resource का wait कर रहा होता है।

👉 Deadlock तब होता है जब ये 4 conditions एक साथ मौजूद हों:

  • Mutual Exclusion
  • Hold and Wait
  • No Preemption
  • Circular Wait

Simple Example

  • P1 के पास R1 है और उसे R2 चाहिए
  • P2 के पास R2 है और उसे R1 चाहिए

👉 Result:
दोनों process एक-दूसरे का wait करते रहते हैं → Deadlock


How Deadlock Occurs in OS

एक process resources को इस sequence में use करता है:

  1. Request a resource
  2. Use the resource
  3. Release the resource

👉 Deadlock तब होता है जब:

  • Process कुछ resources hold करता है
  • और दूसरे resources का wait करता है

Example (Step-wise)

  • Process P1 holds R1 और R2 request करता है
  • Process P2 holds R2 और R1 request करता है

👉 दोनों processes आगे नहीं बढ़ सकते → system deadlock में चला जाता है


Deadlock Handling Methods

1. Prevention

  • Deadlock की conditions को होने से पहले ही रोकना

2. Avoidance

  • System safe state maintain करता है
  • Example: Banker’s Algorithm

3. Detection and Recovery

  • Deadlock detect किया जाता है
  • फिर processes को terminate या resources release करके recover किया जाता है

d

Examples of Deadlock

1. Tape Drive Example

  • System में 2 tape drives हैं
  • P0 और P1 दोनों के पास एक-एक tape drive है
  • दोनों को दूसरा tape drive भी चाहिए

👉 Result:
दोनों process wait करते रहते हैं → Deadlock


2. Semaphore Example

Semaphores A और B की initial value = 1

P0 Action P1 Action
wait(A) wait(B)
wait(B) wait(A)

👉

  • P0 ने A ले लिया, अब B का wait कर रहा है
  • P1 ने B ले लिया, अब A का wait कर रहा है

➡️ दोनों stuck → Deadlock


3. Memory Allocation Example

Total memory = 200KB

  • P0 request: 80KB → मिल गया
  • P1 request: 70KB → मिल गया
  • P0 request: 60KB → wait
  • P1 request: 80KB → wait

👉
दोनों processes अपने पास memory hold किए हुए हैं और extra memory का wait कर रहे हैं

➡️ Result: Deadlock


Necessary Conditions for Deadlock ⭐

Deadlock तभी होता है जब ये चारों conditions एक साथ मौजूद हों:


1. Mutual Exclusion

  • एक resource एक समय में केवल एक process use कर सकता है
  • Resource non-sharable होता है

2. Hold and Wait

  • Process एक resource hold करता है
  • और दूसरे resource का wait करता है

3. No Preemption

  • Resource को process से forcefully वापस नहीं लिया जा सकता
  • Process खुद release करेगा

4. Circular Wait

  • Processes एक circular chain में एक-दूसरे का wait करते हैं

👉 Example:
P1 → P2 → P3 → P4 → फिर P1

Circular wait example

ऊपर दिया गया circular wait deadlock इस प्रकार समझा जा सकता है:

  • P1 के पास R1 resource है और वह R2 का wait कर रहा है (जो P2 के पास है)।
  • P2 के पास R2 resource है और वह R3 का wait कर रहा है (जो P3 के पास है)।
  • P3 के पास R3 resource है और वह R4 का wait कर रहा है (जो P4 के पास है)।
  • P4 के पास R4 resource है और वह R1 का wait कर रहा है (जो P1 के पास है)।