Bird
Raised Fist0

In a producer-consumer system with a fixed-size buffer, which semaphore usage pattern ensures that producers block when the buffer is full and consumers block when the buffer is empty, while allowing safe concurrent access?

easy💻 Programming Q1 of Q15
Operating Systems - Producer-Consumer Problem Using Semaphores
In a producer-consumer system with a fixed-size buffer, which semaphore usage pattern ensures that producers block when the buffer is full and consumers block when the buffer is empty, while allowing safe concurrent access?
AUse a single binary semaphore to control both producer and consumer access to the buffer.
BUse two counting semaphores: one for tracking empty slots and one for full slots, plus a mutex for mutual exclusion.
CUse only a mutex lock without any counting semaphores to synchronize producers and consumers.
DUse a counting semaphore initialized to zero for both producers and consumers.
Step-by-Step Solution
Solution:
  1. Step 1: Use two counting semaphores

    One semaphore counts empty slots (initialized to buffer size), the other counts full slots (initialized to 0).
  2. Step 2: Use a mutex

    Mutex ensures mutual exclusion when accessing the buffer to prevent race conditions.
  3. Final Answer:

    Option B -> Option B
  4. Quick Check:

    Counting semaphores track buffer state, mutex protects critical section [OK]
Quick Trick: Two counting semaphores + mutex for safe buffer access [OK]
Common Mistakes:
MISTAKES
  • Using only a mutex without semaphores causes blocking issues.
  • Using a single binary semaphore cannot track buffer fullness.
  • Initializing semaphores incorrectly leads to deadlocks.
Trap Explanation:
PITFALL
  • Single semaphore or mutex-only approaches fail to properly block producers or consumers.
Interviewer Note:
CONTEXT
  • Tests understanding of semaphore roles and synchronization patterns in producer-consumer.
Master "Producer-Consumer Problem Using Semaphores" in Operating Systems

2 interactive learning modes - each teaches the same concept differently

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Operating Systems Quizzes