Semaphore initialization for mutual exclusion: What initial value should a semaphore have so that only one of many processes can enter its critical section at a time?
-
A8
-
B1
-
C16
-
D0
-
ENone of the above
Answer
Correct Answer: 1
Explanation
Introduction / Context:Mutual exclusion ensures that concurrent processes do not simultaneously execute a critical section that accesses shared data. A binary semaphore is commonly used to enforce this policy. The correct initial value allows the first entrant and blocks the rest until release.
Given Data / Assumptions:
- Semaphore supports atomic wait (P) and signal (V) operations.
- We want exactly one process at a time in the critical section.
- No process should start blocked unnecessarily once the system begins.
Concept / Approach:
A binary semaphore initialized to 1 indicates the resource is free. The first P makes it 0, entering the critical section. Any subsequent P calls block (since value is 0) until the current process performs V, restoring the semaphore to 1 and allowing the next process to proceed.
Step-by-Step Solution:
Choose a binary semaphore for exclusivity.Initialize to 1 to allow one immediate entrant.Upon exit, signal (V) to wake the next waiter.This enforces one-at-a-time access to the critical section.Verification / Alternative check:
Textbook mutex examples and POSIX semaphores mirror this pattern: initial value 1 for a lock-like behavior. Initial value 0 is used for ordering/barrier patterns, not initial entry.
Why Other Options Are Wrong:
- 8 or 16: Counting semaphore allowing many concurrent entrants, violating mutual exclusion.
- 0: Blocks all processes at start until a V occurs from outside the section.
- None of the above: Incorrect because 1 is the standard initial value.
Common Pitfalls:
Forgetting to release (signal) causing starvation; mixing multiple locks inconsistently causing deadlock; using a counting semaphore inadvertently when exclusivity is required.
Final Answer:
1