TheHingineer

  • Operating System


  • OS Part-1

  • OS Part-2

  • OS Part-3

  • OS Part-4

  • OS Part-5

  • Critical Section Problem in Operating System 

    Critical Section Problem Kya Hai?

    Critical Section Problem ek multi-process environment me hota hai jisme multiple processes ek shared resource (jaise ki memory, files, variables) ko access karti hain. Problem yeh hoti hai ki ek samay par sirf ek hi process shared resource ko access kare taki data inconsistency aur race condition na ho.

    Important Terms:

    • Critical Section (CS): Program ka wo part jisme shared resource ka access hota hai.

    • Race Condition: Jab do ya zyada processes ek shared resource ko ek saath modify karne ki koshish karti hain, to incorrect results mil sakte hain.

    • Mutual Exclusion: Sirf ek process ek samay par critical section me ho sakti hai.


    Critical Section Problem ka Solution

    Ek sahi solution ko ye 3 conditions fulfill karni chahiye:

    1. Mutual Exclusion

    Sirf ek hi process ek time par critical section me ho.

    2. Progress

    Agar koi process critical section me nahi hai, to waiting processes ko enter karne ka mauka milna chahiye.

    3. Bounded Waiting

    Har process ko ek fixed time ke andar critical section me enter karne ka chance milna chahiye.


    Example: Bank Account System (Race Condition Problem)

    Maan lo ki do log ek bank account ko ek saath access kar rahe hain:

    1. Person A balance check karta hai: ₹10,000

    2. Person B balance check karta hai: ₹10,000

    3. Person A ₹5,000 withdraw karta hai → New balance ₹5,000 hona chahiye.

    4. Person B ₹3,000 withdraw karta hai → Ideally, balance ₹2,000 hona chahiye.

    5. Agar dono transactions ek saath chal rahi hain, to final balance ₹7,000 dikhega, jo ki galat hai!

    🔴 Problem: Dono transactions ek saath chalne ki wajah se final balance incorrect ho gaya.

    ✅ Solution: Critical Section techniques ka use karke ensure karna ki ek transaction complete hone ke baad hi dusra start ho.


    Critical Section Problem ke Solutions

    1. Hardware-Based Solutions

    • Interrupts Disable Karna: Ek time par sirf ek process chalegi, par yeh inefficient hai.

    • Test-and-Set Lock: Ek special instruction jo ensure karti hai ki ek samay par sirf ek process CS me ho.

    2. Software-Based Solutions

    • Peterson’s Algorithm

    • Dekker’s Algorithm

    • Bakery Algorithm (Har process ek “token” leti hai aur order ke hisaab se enter karti hai).

    3. Synchronization Tools

    • Mutex (Mutual Exclusion Lock): Sirf ek process ko allow karta hai.

    • Semaphores: Ek counter system jisme multiple processes ka access control hota hai.

    • Monitors: Object-oriented synchronization technique jo process coordination ko manage karti hai.


    Diagram: Critical Section Execution

    Process 1              Process 2
    ----------------------------------------
    Entry Section          Entry Section
    Check CS free hai?     Check CS free hai?
    Agar free hai to enter Wait karo agar occupied hai
    Modify Shared Data     Modify Shared Data
    Exit CS Process 1      exit hone ke baad enter karo
    Continue Execution     Continue Execution
     

    Conclusion

    ✅ Critical Section Problem multiprocessor systems me important hai kyunki ye race conditions ko prevent karta hai.
    ✅ Mutual Exclusion, Progress, aur Bounded Waiting ko ensure karna zaroori hai.
    ✅ Mutex, Semaphores, aur Monitors jaise solutions shared resource access ko safe banate hain.

    Scroll to Top