Binary semaphore for mutual exclusion: To ensure that only one of many processes enters its critical section at a time, what should be the initial value of the controlling semaphore?

Difficulty: Easy

Correct Answer: 1

Explanation:


Introduction / Context:
Semaphores are fundamental synchronization primitives. For mutual exclusion (mutex), a binary semaphore controls access to a critical section so that only one process can execute inside at once. The initial value determines whether the first process can enter immediately.



Given Data / Assumptions:

  • Multiple processes contend for the same critical section.
  • Semaphore P (wait) decrements; V (signal) increments.
  • Binary semaphore is used for mutual exclusion.


Concept / Approach:

A binary semaphore initialized to 1 indicates the resource (critical section) is free. The first P operation decrements it to 0 and grants entry. Additional P attempts block until the semaphore is signaled (V) by the process on exit, restoring it to 1. Initializing to 0 would block everyone at start, requiring an extra signal to begin.



Step-by-Step Solution:

Choose a binary semaphore for exclusivity.Initialize semaphore = 1 to allow first entrant.Processes call P before entering; only one succeeds at a time.On exit, process calls V to wake the next waiting process.


Verification / Alternative check:

Standard textbooks define mutex semaphores with initial value 1. Code examples show semaphore value toggling between 0 and 1 during use.



Why Other Options Are Wrong:

  • 8 or 16: These would allow many simultaneous entrants (counting semaphore), not mutual exclusion.
  • 0: Would block all processes initially until an external V occurs.
  • None of the above: Incorrect because 1 is correct.


Common Pitfalls:

Using counting semaphores unintentionally; forgetting to signal on exit leading to deadlock; initializing to 0 and wondering why no process proceeds.



Final Answer:

1

Discussion & Comments

No comments yet. Be the first to comment!
Join Discussion