Bird
Raised Fist0

You are designing a multi-threaded server where threads must limit concurrent access to a pool of 5 identical database connections. Which synchronization primitive do you choose and why?

hard🌍 Real-world Scenario Q9 of Q15
Operating Systems - Semaphore vs Mutex - When to Use Which
You are designing a multi-threaded server where threads must limit concurrent access to a pool of 5 identical database connections. Which synchronization primitive do you choose and why?
AMutex, because it enforces exclusive access to each connection individually
BCounting semaphore initialized to 5, because it tracks available connections and limits concurrent access
CBinary semaphore initialized to 1, because it protects the entire pool as a single resource
DSpinlock, because it provides low-latency locking for high concurrency
Step-by-Step Solution
Solution:
  1. Step 1: Understand resource pool constraints

    There are 5 identical connections; up to 5 threads can use them concurrently.
  2. Step 2: Analyze synchronization options

    Mutex (B) protects one resource at a time, but managing 5 connections individually is complex. Binary semaphore (C) limits to 1 thread, underutilizing resources. Spinlock (D) is inefficient for blocking on resource availability.
  3. Step 3: Identify best primitive

    Counting semaphore initialized to 5 tracks available connections and limits concurrent access correctly.
  4. Final Answer:

    Option B -> Option B
  5. Quick Check:

    Counting semaphore matches resource pool concurrency [OK]
Quick Trick: Counting semaphore tracks multiple identical resources concurrently [OK]
Common Mistakes:
MISTAKES
  • Using mutex for multiple identical resources without counting
  • Using binary semaphore limiting concurrency to 1
  • Choosing spinlock for blocking resource management
Trap Explanation:
PITFALL
  • Candidates often default to mutex or binary semaphore, missing counting semaphore's concurrency control.
Interviewer Note:
CONTEXT
  • Evaluates practical application of synchronization primitives to resource pools.
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