Bird
Raised Fist0

In a system where multiple producers and consumers share a fixed-size buffer, which synchronization mechanism best ensures that producers wait when the buffer is full and consumers wait when the buffer is empty?

easy🔍 Pattern Recognition Q11 of Q15
Operating Systems - Producer-Consumer Problem Using Semaphores
In a system where multiple producers and consumers share a fixed-size buffer, which synchronization mechanism best ensures that producers wait when the buffer is full and consumers wait when the buffer is empty?
AMutex lock only to protect buffer access
BSpinlocks to busy-wait until buffer space is available
CCounting semaphores to track empty and full slots along with a mutex for mutual exclusion
DCondition variables without any semaphore or mutex
Step-by-Step Solution
  1. Step 1: Identify synchronization needs

    Producers must wait if the buffer is full; consumers must wait if empty. This requires tracking counts of empty and full slots.
  2. Step 2: Role of counting semaphores

    Counting semaphores elegantly track available slots (empty) and filled slots (full), enabling blocking waits without busy-waiting.
  3. Step 3: Need for mutual exclusion

    A mutex is required to protect concurrent access to the buffer itself to avoid race conditions.
  4. Step 4: Why other options fail

    Mutex alone (A) cannot block producers/consumers based on buffer state; spinlocks (C) waste CPU cycles; condition variables (D) require mutexes and signaling to work correctly in this scenario.
  5. Final Answer:

    Option C -> Option C
  6. Quick Check:

    Counting semaphores + mutex is the classic producer-consumer solution [OK]
Quick Trick: Counting semaphores track buffer state; mutex protects access
Common Mistakes:
MISTAKES
  • Assuming mutex alone handles waiting
  • Using spinlocks wastes CPU
  • Thinking condition variables alone suffice
Trap Explanation:
PITFALL
  • Mutex alone protects critical section but doesn't block on buffer full/empty; spinlocks look simple but cause busy-waiting; condition variables need mutex and signaling, so alone they are insufficient.
Interviewer Note:
CONTEXT
  • Tests candidate's ability to identify correct synchronization primitives for producer-consumer problem
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