Bird
Raised Fist0

Consider a binary semaphore initialized to 1 protecting a critical section. If a thread calls wait() twice consecutively without a post(), what happens?

medium🧠🧾 Concept Trace Q4 of Q15
Operating Systems - Semaphore vs Mutex - When to Use Which
Consider a binary semaphore initialized to 1 protecting a critical section. If a thread calls wait() twice consecutively without a post(), what happens?
AThe thread proceeds both times without blocking
BThe semaphore resets automatically to 1 after the first wait()
CThe thread blocks on the second wait() call until another thread posts
DThe semaphore count becomes negative, causing an error
Step-by-Step Solution
Solution:
  1. Step 1: Understand binary semaphore behavior

    Binary semaphore initialized to 1 allows one wait() to proceed, decrementing count to 0.
  2. Step 2: Analyze second wait() call

    Second wait() finds count 0, so thread blocks until a post() increments count.
  3. Step 3: Evaluate other options

    Proceeding both times (A) ignores blocking semantics. Negative count (C) is not standard; semaphores block instead. Automatic reset (B) does not happen.
  4. Final Answer:

    Option C -> Option C
  5. Quick Check:

    Second wait() blocks on zero count [OK]
Quick Trick: Second wait() on binary semaphore blocks if count is zero [OK]
Common Mistakes:
MISTAKES
  • Assuming binary semaphore allows multiple entries without post
  • Believing semaphore count can go negative causing error
  • Thinking semaphore resets automatically
Trap Explanation:
PITFALL
  • Candidates often confuse binary semaphore with mutex or think it resets automatically after wait.
Interviewer Note:
CONTEXT
  • Checks understanding of semaphore wait/post blocking behavior and count semantics.
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