6. Critical section

Critical Section in Synchronization

Critical Section

Critical Section program का वह हिस्सा होता है जहाँ shared resources (जैसे memory, files, variables) को multiple processes/threads द्वारा access किया जाता है।

👉 Problems avoid करने के लिए:

  • एक समय में केवल एक process/thread को ही critical section execute करना चाहिए
  • इसके लिए synchronization techniques (जैसे mutex, semaphore) का उपयोग किया जाता है

👉 इससे:

  • Race condition avoid होती है
  • Data consistency maintain रहती है
  • System safe और predictable बनता है

Structure of a Critical Section

1. Entry Section

  • Process critical section में जाने की permission मांगता है
  • यहाँ mutex या semaphore जैसे tools का उपयोग होता है

2. Critical Section

  • यह actual code होता है
  • जहाँ shared resources को access या modify किया जाता है

3. Exit Section

  • Process critical section से बाहर निकलते समय lock release करता है
  • ताकि अन्य processes अंदर आ सकें

4. Remainder Section

  • Program का बाकी हिस्सा
  • जहाँ shared resource access नहीं होता

आसान समझ (Simple Understanding)

👉 Flow कुछ इस तरह होता है:

Entry → Critical Section → Exit → Remainder

Critical_section

Critical Section Problem

Shared Resources और Race Condition

Shared resources में शामिल होते हैं:

  • memory
  • global variables
  • files
  • databases

👉 Race Condition तब होती है जब:
दो या अधिक processes एक ही समय पर shared data को update करते हैं, जिससे गलत result आता है।

📌 Example:
दो bank transactions एक साथ account balance update करें → final balance गलत हो सकता है


Pseudo Code (Simple समझ)

 
do{
flag = 1;
while(flag); // entry section
// critical section
if (!flag)
// remainder section
} while(true);
 

Requirements of a Good Solution

एक अच्छा critical section solution में ये qualities होनी चाहिए:

  • Correctness → data consistent रहे
  • Efficiency → waiting कम हो, CPU utilization ज्यादा हो
  • Fairness → कोई process unfairly wait न करे

3 Main Conditions (Very Important ⭐)

1. Mutual Exclusion

  • एक समय में केवल एक process critical section में जाए
  • Conflicts को prevent करता है

2. Progress

  • अगर कोई process critical section में नहीं है, तो next process को entry मिलनी चाहिए
  • System idle या stuck नहीं होना चाहिए

3. Bounded Waiting

  • हर process के wait करने का limit होना चाहिए
  • किसी process को हमेशा के लिए wait (starvation) नहीं होना चाहिए

Simple Solution (Lock Concept)

 
acquireLock();
// Critical Section
releaseLock();
 

👉

  • Process पहले lock acquire करता है
  • Critical section execute करता है
  • फिर lock release करता है

Real-Life Examples of Critical Section

1. Banking System (ATM / Online Banking)

  • Critical Section: account balance update
  • ❌ Problem: simultaneous withdrawal → wrong balance

2. Ticket Booking System

  • Critical Section: last seat booking
  • ❌ Problem: दो users एक ही seat book कर सकते हैं

3. Print Spooler

  • Critical Section: print queue access
  • ❌ Problem: print jobs mix या skip हो सकते हैं

4. Shared File Editing (Google Docs, MS Word)

  • Critical Section: file save/write
  • ❌ Problem: data conflict या data loss