Introduction (परिचय)
Operating System में कई processes या threads एक साथ run करते हैं।
कई बार ये processes एक ही shared resource (जैसे variable, file, memory) को access करते हैं।
अगर इन processes को properly control नहीं किया गया, तो system में error या wrong output आ सकता है।
इसी समस्या को Critical Section Problem कहा जाता है।
Critical Section क्या होता है
Program का वह हिस्सा जहाँ shared resource access किया जाता है, उसे Critical Section कहते हैं।
Simple Definition
Critical Section = code का वह part जहाँ shared data access होता है
Critical Section Structure
हर process के execution को चार parts में divide किया जाता है:

1. Entry Section
यहाँ process check करता है कि वह critical section में enter कर सकता है या नहीं।
2. Critical Section
यह वह भाग है जहाँ shared resource use होता है।
3. Exit Section
यहाँ process critical section छोड़ता है।
4. Remainder Section
बाकी normal code यहाँ execute होता है।
Race Condition क्या है
जब दो या अधिक processes एक ही समय पर shared data को access करते हैं और result unpredictable या गलत हो जाता है, उसे Race Condition कहते हैं।
Example (समझने के लिए)
मान लो एक shared variable है:
count = 5
दो processes हैं:
P1: count = count + 1
P2: count = count + 1
अगर दोनों processes एक साथ execute करें:
- दोनों 5 read करेंगे
- दोनों 6 write करेंगे
Final result = 6 (गलत)
Correct result = 7 (5+1+1) होना चाहिए
Critical Section Problem का Goal
यह ensure करना कि:
- एक समय में केवल एक process critical section में जाए
- data corruption न हो
- correct output मिले
Solution Requirements
Critical Section Problem का सही solution इन तीन conditions को satisfy करना चाहिए:
1. Mutual Exclusion
एक समय में केवल एक process critical section में होना चाहिए।
2. Progress
अगर critical section खाली है, तो किसी waiting process को enter करने दिया जाना चाहिए।
3. Bounded Waiting
हर process को limited time में chance मिलना चाहिए।
कोई process indefinite wait न करे।
Real Life Example
मान लो एक bank account है:
- Balance = 1000
- दो लोग एक साथ पैसे निकाल रहे हैं
अगर system properly control नहीं करता:
- दोनों same balance देखेंगे
- दोनों पैसे निकाल लेंगे
Result: गलत balance
Solution Techniques (Short Idea)
Critical Section Problem को solve करने के लिए:
- Locks
- Semaphores
- Monitors
का use किया जाता है (इन topics को आगे पढ़ेंगे)
Key Points (Exam Focus)
- Critical Section definition
- Race Condition example
- Mutual Exclusion, Progress, Bounded Waiting
- Structure (Entry, Critical, Exit, Remainder)
Conclusion
Critical Section Problem Operating System का एक fundamental concept है, जो shared resources के safe use को ensure करता है।
अगर इसे सही तरीके से handle नहीं किया जाए, तो system में गलत data और inconsistent results आ सकते हैं।