TheHingineer

  • Operating System


  • OS Part-1

  • OS Part-2

  • OS Part-3

  • OS Part-4

  • OS Part-5

  • Classical Problems of Synchronization in Operating System

    Introduction

    Operating System me process synchronization ek important concept hai jo ensure karta hai ki multiple processes ya threads ek sath kaam kare bina kisi conflict ke.

    Synchronization problems ko real-world concurrency issues ko samajhne ke liye define kiya gaya hai. Yeh problems semaphores, monitors, aur mutex locks ka use karke solve ki ja sakti hain.


    1. Producer-Consumer Problem

    Problem Statement:

    • Ek buffer hai jisme producer process items produce karke store karta hai.

    • Consumer process items consume karta hai buffer se.

    • Agar buffer full ho jaye to producer naye items nahi daal sakta.

    • Agar buffer empty ho jaye to consumer naye items nahi nikal sakta.

    • Dono processes ek sath buffer ko access na kare taki data inconsistency na ho.

    Solution Using Semaphores:

    Is problem ko solve karne ke liye 3 semaphores use hote hain:

    • Mutex → Buffer ka mutual exclusion ensure karta hai.

    • Full → Kitne slots occupied hain ye count karta hai.

    • Empty → Kitne slots free hain ye count karta hai.

    Producer Process:

    In C Language

    wait(empty); // Agar buffer full hai to wait karega
    wait(mutex); // Lock buffer
    // Item buffer me add karega
    signal(mutex); // Buffer unlock karega
    signal(full); // Consumer ko notify karega

    Consumer Process:

    In C language

    wait(full); // Agar buffer empty hai to wait karega
    wait(mutex); // Lock buffer
    // Item buffer se remove karega
    signal(mutex); // Buffer unlock karega
    signal(empty); // Producer ko notify karega


    2. Dining Philosophers Problem

    Problem Statement:

    • 5 philosophers ek gol table pe baithe hain aur unke samne spaghetti ka bowl hai.

    • Har philosopher ko 2 forks chahiye (ek left aur ek right) khane ke liye.

    • Sirf 5 forks available hain.

    • Agar sare philosophers ek fork utha le aur dusre ke wait kare, to deadlock ho sakta hai.

    Solution Using Semaphores:

    Har fork ko ek semaphore se represent kiya jata hai jo initially 1 hota hai.

    In C language

    wait(fork[i]); // Left fork uthayega
    wait(fork[(i+1)%5]); // Right fork uthayega
    // Khana khayega
    signal(fork[i]); // Left fork wapas rakhega
    signal(fork[(i+1)%5]); // Right fork wapas rakhega

    Deadlock Prevention Strategies:

    1. Ek time pe maximum 4 philosophers hi forks utha sakein.

    2. Ek philosopher pehle right fork uthaye, baaki pehle left fork uthaye.


    3. Readers-Writers Problem

    Problem Statement:

    • Ek shared database hai jise multiple readers aur writers access kar rahe hain.

    • Readers ek sath database read kar sakte hain.

    • Writers tab tak write nahi kar sakte jab tak koi reader ya writer database access kar raha ho.

    Solution Using Semaphores:

    • Mutex → Reader count update karne ke liye.

    • WriteLock → Writer ko exclusive access dene ke liye.

    Reader Process Logic:

    In C language

    wait(mutex); // Reader count update karne ke liye lock
    readerCount++;
    if (readerCount == 1) wait(writeLock); // Pehla reader writers ko block karega
    signal(mutex); // Lock release karega
    // Read operation
    wait(mutex);
    readerCount--;
    if (readerCount == 0) signal(writeLock); // Last reader writer ko access dega
    signal(mutex);

    Writer Process Logic:

    In C language

    wait(writeLock); // Database lock karega
    // Write operation
    signal(writeLock); // Database unlock karega


    4. Sleeping Barber Problem

    Problem Statement:

    • Ek barbershop hai jisme ek barber aur kuch waiting chairs hain.

    • Agar koi customer nahi hai, to barber so jata hai.

    • Agar customer aata hai, to wo barber ko uthata hai.

    • Agar sab chairs full hain, to naye customers chale jate hain.

    • Challenge ye hai ki customers aur barber ko efficiently manage kiya jaye.

    Solution Using Semaphores:

    • Mutex → Waiting room access control karta hai.

    • Customers → Customers ka count track karta hai.

    • Barbers → Barber available hai ya nahi ye track karta hai.

    Customer Process Logic:

    In C language

    wait(mutex);
    if (waiting < chairs) {
    waiting++;
    signal(customers); // Barber ko uthayega
    signal(mutex);
    wait(barber); // Haircut lega
    } else {
    signal(mutex); // Agar chairs full hain to chala jayega
    }

    Barber Process Logic:

    In C Language

    wait(customers); // Customer ka wait karega
    wait(mutex);
    waiting--;
    signal(mutex);
    signal(barber); // Haircut start karega
    // Haircut karega


    Conclusion

    ✅ Synchronization problems real-world concurrency issues ko represent karti hain.
    ✅ Semaphores, mutex locks, aur monitors ka use karke safe execution ensure kiya jata hai.
    ✅ Deadlocks aur starvation avoid karne ke liye proper handling zaroori hoti hai.

    Scroll to Top