Bird
Raised Fist0

In a system where threads must both mutually exclude access to a critical section and also wait for specific events, which synchronization design best avoids deadlocks and ensures correctness?

hard🎤 Interviewer Follow-up Q10 of Q15
Operating Systems - Semaphore vs Mutex - When to Use Which
In a system where threads must both mutually exclude access to a critical section and also wait for specific events, which synchronization design best avoids deadlocks and ensures correctness?
AUse mutexes combined with condition variables to wait and signal events safely
BUse binary semaphores for both mutual exclusion and event signaling
CUse only mutexes for mutual exclusion and busy-wait for events
DUse counting semaphores for mutual exclusion and event notification
Step-by-Step Solution
Solution:
  1. Step 1: Identify synchronization needs

    Mutual exclusion plus waiting for events requires both locking and waiting mechanisms.
  2. Step 2: Analyze options

    Mutex alone with busy-wait (A) wastes CPU and risks deadlock. Binary semaphores (B) do not enforce ownership and can cause race conditions. Counting semaphores (D) do not provide atomic release-and-wait semantics.
  3. Step 3: Correct design

    Mutex combined with condition variables (C) allows atomic release of mutex while waiting and safe event signaling, avoiding deadlocks.
  4. Final Answer:

    Option A -> Option A
  5. Quick Check:

    Mutex + condition variables enable safe wait/signal [OK]
Quick Trick: Mutex + condition variables avoid deadlock and enable event waits [OK]
Common Mistakes:
MISTAKES
  • Using busy-wait loops with mutexes
  • Confusing semaphores with condition variables for event waits
  • Assuming counting semaphores handle mutual exclusion and events atomically
Trap Explanation:
PITFALL
  • Candidates often misuse semaphores for event waiting, missing condition variable semantics.
Interviewer Note:
CONTEXT
  • Tests deep understanding of synchronization primitives and deadlock avoidance in complex scenarios.
Master "Semaphore vs Mutex - When to Use Which" 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