TheHingineer

  • Operating System


  • OS Part-1

  • OS Part-2

  • OS Part-3

  • OS Part-4

  • OS Part-5

  • Semaphores in Operating System

    Introduction to Semaphores

    Semaphore ek synchronization tool hai jo multiprocessing aur multithreading environments me shared resources ka access manage karne ke liye use hota hai. Yeh race conditions aur critical section problems ko avoid karne me help karta hai.

    Semaphore ek integer variable hota hai jo shared resources ki availability ko control karta hai.


    Types of Semaphores

    Semaphores do tareeke ke hote hain:

    1. Binary Semaphore (Mutex)

    • Iska value sirf 0 ya 1 ho sakta hai.

    • 0: Resource occupied hai (koi use kar raha hai).

    • 1: Resource free hai (koi bhi use kar sakta hai).

    • Ek baar me sirf ek process hi resource ko access kar sakti hai.

    • Example: Agar ek file ko ek process access kar rahi hai, to dusri process tab tak wait karegi jab tak pehli process ka kaam complete nahi ho jata.

    2. Counting Semaphore

    • Iska value 0 se zyada ho sakta hai (1, 2, 3,…).

    • Multiple instances wale resources (e.g., printers, database connections) ka access control karta hai.

    • Example: Agar system me 3 printers available hain, to initially semaphore ki value 3 hogi, aur har baar jab ek printer use hoga, value 1 se decrement ho jayegi.


    Semaphores ka Working Mechanism

    Semaphore do atomic operations ka use karta hai:

    1. wait() Operation (P operation / Down operation)

    Agar resource available hai, to process use kar sakti hai, warna wait karegi.

    In C language

    wait(S) {
    while (S <= 0); // Jab tak S > 0 na ho, tab tak wait karo
    S = S - 1; // Resource occupy karne ke baad value reduce kar do
    }

    2. signal() Operation (V operation / Up operation)

    Jab ek process resource use kar ke free kar deti hai, tab signal operation execute hota hai.

    In C language

    signal(S) {
    S = S + 1; // Resource free hone ke baad value increase kar do
    }


    Example: Semaphore ka use Process Synchronization ke liye

    Scenario: Do Processes Shared Resource Use Kar Rahe Hain

    Maan lo ki do processes (P1 aur P2) ek shared resource ko access kar rahe hain. Semaphore ensure karega ki ek samay par sirf ek process hi resource ko access kare.

    Initial State:

    In java

    Semaphore S = 1 (Resource free hai)

    Execution Steps:

    1. 1. P1 enters critical section → wait(S) execute hota hai → S = 0 ho jata hai

    2. 2. P2 bhi enter karna chahta hai → lekin S = 0 hai → P2 wait karega

    3. 3. P1 ka kaam complete hota hai → signal(S) execute hota hai → S = 1 ho jata hai

    4. 4. P2 ab enter kar sakta hai → wait(S) execute hota hai → S = 0 ho jata hai

    Diagram Representation:

    Process P1           Process P2
    ----------------------------------
    wait(S)          wait(S) (Blocked)
    Enter CS
    Modify Resource
    signal(S)        Enter CS (After P1 exits)
    Exit CS          Modify Resource
                     signal(S)
                     Exit CS


    Real-World Use Cases of Semaphores

    ✅ Process Synchronization: Multiple processes ke beech orderly execution ensure karta hai.
    ✅ Race Condition Avoidance: Shared resources par multiple processes ke simultaneous access se hone wale issues ko prevent karta hai.
    ✅ Producer-Consumer Problem: Buffer management me producer aur consumer ka synchronization maintain karta hai.
    ✅ Readers-Writers Problem: Multiple processes ke read aur write operations ko synchronize karta hai.


    Conclusion

    🎯 Semaphores ek synchronization mechanism hai jo processes aur threads ke beech coordination establish karta hai.
    🎯 Binary semaphore sirf ek process ko resource access dene me madad karta hai, jabki counting semaphore multiple instances wale resources ka access control karta hai.
    🎯 wait() aur signal() operations ke through process synchronization achieve kiya jata hai.

    Scroll to Top