🎓 BookMCQ
← Back to 5. Process Synchronization

📝 Semaphore Usage (36 MCQs)

Using Semaphores for Mutual Exclusion and Event Ordering

Semaphore usage involves applying atomic wait and signal operations to control access to shared resources and coordinate execution between processes or threads. Different initialization values and operation sequences allow semaphores to implement mutual exclusion, resource counting, and event synchronization. Their effectiveness depends on assigning each semaphore a clear purpose and maintaining a correct relationship between permit acquisition and release.

Choosing Between Resource Counting and Synchronization

A shared resource may have several identical instances, while another situation may require one process to wait for a particular event. A binary semaphore supports exclusive access or event notification, whereas a counting semaphore represents available resources. The initialization value establishes the initial number of permits or the initial synchronization condition.

Wait and Signal Ordering for Common Applications

For mutual exclusion, a binary semaphore commonly begins at 1, and a process performs wait before entering the protected region and signal after leaving. For event ordering, a waiting process begins with a semaphore at 0, while another signals after completing the required event. Counting semaphores represent the availability of multiple resource instances.

Enforcing Producer Before Consumer Execution

Suppose producer P must finish writing data before consumer C reads it. Initialize a semaphore event to zero. P writes the data and performs signal(event). C executes wait(event) and remains blocked until the producer signals. The consumer then proceeds with the required data available, establishing an explicit execution-order relationship.

Synchronization Correctness and Semaphore Misuse

Semaphores coordinate resource pools, event dependencies, operating system services, and concurrent application workflows. Incorrect initialization can allow premature access or unnecessary blocking. Missing signal operations can leave processes waiting indefinitely, while extra signals can permit invalid resource usage. Correct usage therefore requires clear resource accounting, consistent operation ordering, and suitable handling of cancellation and failures.

Misconceptions About Semaphore Applications

'Every semaphore must begin at one' is incorrect because initialization depends on the purpose. 'signal() releases only a resource acquired by the same process' is incorrect because semaphores need not enforce ownership. 'A semaphore initialized to zero cannot become positive' is incorrect because signal operations increase its available permits.

Exam Focus: Selecting Correct Semaphore Initialization and Order

Choose suitable initial semaphore values for mutual exclusion, resource counting, and event ordering. Trace each wait and signal operation to determine whether execution proceeds or blocks. Questions may ask you to identify missing operations, explain producer-consumer coordination, compare binary and counting use, or diagnose an incorrect synchronization sequence.

Key Terms of Semaphore Usage

  • Initialization - Setting the initial semaphore value.
  • Permit - Available resource or event token.
  • Mutual exclusion - Exclusive protected-resource access.
  • Event ordering - Enforcing a required execution sequence.
  • Resource counting - Tracking available resource instances.
  • Blocking - Waiting until a permit becomes available.
12
Easy
12
Medium
12
Hard

📝 All Semaphore Usage MCQs

Q1. A print server uses a counting semaphore initialized to 5 for five identical printers. Three jobs acquire permits, then one job finishes and signals once. How many printer permits are available afterward?

A.1
B.2
C.3 ✅
D.4
💡 Difficulty: easy | ✅ Correct: C

📖 Explanation: The count begins at 5. Three successful waits consume three permits, leaving 2; the completed job signals once, restoring the count to 3. The remaining capacity is therefore three printers.

Q2. A process acquires a binary semaphore initialized to 1 before updating a shared settings record. A second process calls wait while the first still holds the permit. What should occur?

A.The second process enters because the semaphore is binary
B.The second process blocks until the permit is released ✅
C.The semaphore changes automatically to a counting semaphore
D.Both processes enter, but their writes are merged
💡 Difficulty: easy | ✅ Correct: B

📖 Explanation: The initial value provides one permit. The first wait consumes it, so a competing wait cannot succeed until a signal makes a permit available. This is how the binary semaphore enforces exclusive access.

Q3. A worker must not read a report until another process has finished writing it. Which initial value and operation order best express this dependency with a semaphore?

A.Initialize to 1; worker signals before reading
B.Initialize to 1; writer waits after writing
C.Initialize to 0; writer signals after writing and worker waits before reading ✅
D.Initialize to 2; both processes wait before working
💡 Difficulty: easy | ✅ Correct: C

📖 Explanation: Starting at zero leaves no event permit for the worker. The writer signals only after completing the report, and the worker waits before reading, so the required producer-before-consumer order is enforced.

Q4. A machine has four identical accelerator cards that may be used concurrently. Which semaphore configuration directly models the number of cards available?

A.A counting semaphore initialized to 4 ✅
B.A binary semaphore initialized to 0
C.A binary semaphore initialized to 1
D.A counting semaphore initialized to 0 regardless of card availability
💡 Difficulty: easy | ✅ Correct: A

📖 Explanation: A counting semaphore represents the number of available instances of an interchangeable resource. Initializing it to four allows four successful acquisitions; another request must wait until a card is released.

Q5. A file-processing service needs to protect its queue structure and separately track how many completed files are ready. Which design best separates these two responsibilities?

A.Use one semaphore for both purposes and interpret its count in two ways
B.Use a mutual-exclusion semaphore for queue updates and a counting semaphore for ready files ✅
C.Use only a zero-initialized event semaphore for every queue operation
D.Use a counting semaphore for queue indexes and no protection for updates
💡 Difficulty: medium | ✅ Correct: B

📖 Explanation: The queue lock protects simultaneous modifications to shared structure, while the item count records available work. Separating them prevents a resource count from being confused with permission to modify queue metadata.

Q6. A task-completion semaphore starts at 0. A producer completes four tasks and signals once after each completion. Before any further signal, how many successful waits can a dispatcher perform?

A.One
B.Two
C.Three
D.Four ✅
💡 Difficulty: medium | ✅ Correct: D

📖 Explanation: Each signal contributes one event permit, so four completions create four permits. Each successful wait consumes one. The dispatcher can therefore complete four waits before another task-completion signal is required.

Q7. Two independent preparation tasks must both finish before a final calculation begins. Each task signals the same semaphore once after finishing; the semaphore starts at 0. How many waits must the final task complete?

A.One
B.Two ✅
C.Three
D.Four
💡 Difficulty: medium | ✅ Correct: B

📖 Explanation: Each completed preparation contributes one permit. The final calculation must consume both permits, one for each prerequisite. A single wait would prove only one completion, not that both independent tasks had finished.

Q8. A programmer uses a binary semaphore initialized to 1 to represent a pool containing three identical scanners. A fourth request is expected to wait only when all scanners are occupied. What is wrong with this choice?

A.A binary semaphore cannot represent three simultaneously available instances ✅
B.A binary semaphore always permits three acquisitions
C.The semaphore must start at 0 because scanners are physical devices
D.A scanner pool must be protected only by an event semaphore
💡 Difficulty: hard | ✅ Correct: A

📖 Explanation: A binary semaphore represents at most one available permit, so it cannot express three independently usable scanners. A counting semaphore initialized to three accurately models the pool capacity for this scenario.

Q9. A monitoring table records available permits after each operation: step 0 = 6; step 1 = 5; step 2 = 4; step 3 = 5; step 4 = 6. Which operation sequence matches the table?

A.Signal, signal, wait, wait
B.Wait, signal, wait, signal
C.Wait, wait, signal, signal ✅
D.Wait, wait, wait, signal
💡 Difficulty: medium | ✅ Correct: C

📖 Explanation: The count decreases from 6 to 5 and then 4, indicating two successful waits. It increases to 5 and then 6, indicating two signals. Thus the sequence is wait, wait, signal, signal.

Q10. A parking facility has 12 spaces and initializes its availability semaphore to 12. Five cars enter and two leave, with each entry waiting once and each exit signaling once. What is the available count?

A.5
B.7
C.9 ✅
D.12
💡 Difficulty: easy | ✅ Correct: C

📖 Explanation: Five entries consume five permits, reducing availability from 12 to 7. Two departures return two permits, giving 7 plus 2, or 9 available spaces. The semaphore therefore reports nine available spaces at the end.

Q11. A developer adds signal(S) immediately before entering a protected region and again after leaving it. S was initialized to 1. What is the most direct risk?

A.The permit count can increase, allowing simultaneous entry and breaking mutual exclusion ✅
B.The semaphore becomes permanently zero
C.Every waiting process is forced to restart
D.The protected region becomes atomic without acquiring a permit
💡 Difficulty: medium | ✅ Correct: A

📖 Explanation: The signal before entry adds a permit instead of acquiring one. The count may exceed the intended single permit, allowing competing processes to enter together and defeating the protection during concurrent execution.

Q12. A banking thread updates a balance by reading it, calculating a new value, and writing it. Which semaphore placement protects the complete read-modify-write operation?

A.Wait before reading and signal only after the write completes ✅
B.Wait before reading, then signal immediately after the read
C.Signal before the calculation and wait after the write
D.Wait only when the calculated balance is negative
💡 Difficulty: medium | ✅ Correct: A

📖 Explanation: The read, calculation, and write form one logical update. Holding the semaphore across all three steps prevents another thread from changing the balance between the read and final write during a concurrent update.

Q13. Two processes replace wait(S) with separate steps: read S, check that it is positive, then decrement it. Both can run these steps concurrently. Why is the replacement unsafe?

A.The separate check and decrement can let both processes consume the same permit ✅
B.A positive semaphore value cannot be decremented
C.The semaphore must be initialized to the number of processes
D.A decrement automatically signals every blocked process
💡 Difficulty: hard | ✅ Correct: A

📖 Explanation: Both processes may read the same positive value before either decrements it. Because the test and decrement are not indivisible, both can proceed using one permit, violating the resource limit.

Q14. A chart gives semaphore availability after successive operations: (0, 3); (1, 2); (2, 1); (3, 2); (4, 1); (5, 2). What is the net change from the initial state?

A.A decrease of two permits
B.A decrease of one permit ✅
C.An increase of one permit
D.No net change
💡 Difficulty: medium | ✅ Correct: B

📖 Explanation: The count begins at 3 and ends at 2, so the net change is minus one. The intermediate rises and falls represent signals and waits, but only the endpoints determine the overall change.

Q15. A laboratory has eight identical test stations. A counting semaphore starts at 8; six researchers acquire stations and two later release theirs. Assuming every operation succeeds, how many permits remain?

A.2
B.3
C.4 ✅
D.6
💡 Difficulty: easy | ✅ Correct: C

📖 Explanation: Six acquisitions reduce the count from 8 to 2. Two releases add two permits, producing 4. This result represents four stations that are currently available for the next researchers in the laboratory.

Q16. A producer signals an event semaphore before the consumer reaches wait(event). The semaphore began at 0. What should the later wait do?

A.Block forever because the consumer missed the signal
B.Consume the stored permit and continue ✅
C.Reset the semaphore to its initial value without continuing
D.Require the producer to signal a second time
💡 Difficulty: easy | ✅ Correct: B

📖 Explanation: A semaphore records an available event permit. The earlier signal raises the count from zero to one, so the later wait consumes that permit and proceeds instead of losing the notification.

Q17. A program uses one semaphore both to protect a shared queue and to count completed jobs. During load, consumers sometimes enter the queue while another consumer is changing its indexes. What design error best explains this?

A.The same semaphore is being used for distinct counting and mutual-exclusion responsibilities ✅
B.A semaphore cannot be used by consumers
C.A counting semaphore always prevents queue corruption without a lock
D.The semaphore must be initialized to the number of consumers
💡 Difficulty: hard | ✅ Correct: A

📖 Explanation: A job count records how much work is available; it does not necessarily serialize modifications to queue indexes. A separate mutual-exclusion mechanism must protect the shared queue structure during concurrent queue access.

Q18. A buffer has capacity 5 and is initially empty. Which initial values correctly represent empty slots and filled slots for producer-consumer coordination?

A.empty = 0 and full = 5
B.empty = 1 and full = 1
C.empty = 5 and full = 0 ✅
D.empty = 5 and full = 5
💡 Difficulty: easy | ✅ Correct: C

📖 Explanation: All five positions are initially empty and none contains an item. Therefore empty begins at the buffer capacity, five, while full begins at zero; producer and consumer operations update these counts.

Q19. A producer writes an item into a shared buffer but signals the full-items semaphore before completing the write. A consumer wakes and reads immediately. What is the flaw?

A.The consumer may run before the item is fully available ✅
B.The signal operation automatically completes the producer write
C.The full-items semaphore must always start at the buffer capacity
D.The consumer should signal full-items before reading
💡 Difficulty: hard | ✅ Correct: A

📖 Explanation: The notification must follow the operation it announces. Signaling too early can let the consumer proceed while the buffer update is incomplete, exposing uninitialized or inconsistent item data to the reader.

Q20. In a producer-consumer buffer, a semaphore counts available items. Which operation should a consumer perform before removing an item?

A.wait on the available-items semaphore ✅
B.signal the available-items semaphore
C.wait on the empty-slots semaphore
D.signal the mutex twice
💡 Difficulty: easy | ✅ Correct: A

📖 Explanation: The consumer must first obtain an item permit, which is available only after a producer has inserted an item and signaled. Removing an item before waiting could access an empty buffer.

Q21. A queue uses a counting semaphore to represent the number of ready items. Several producers and consumers also update a shared head index and tail index. Why is the item semaphore alone insufficient?

A.It counts available items but does not necessarily serialize simultaneous index updates ✅
B.It prevents every consumer from removing an item
C.It forces producers to run only after all consumers finish
D.It makes the queue indexes immutable
💡 Difficulty: hard | ✅ Correct: A

📖 Explanation: The counting semaphore coordinates whether an item exists, while the indexes are shared mutable state. Concurrent updates to those indexes still need mutual exclusion to prevent inconsistent queue structure during concurrent queue operations.

Q22. A graph lists available permits at events E0 through E4: E0 = 4; E1 = 3; E2 = 2; E3 = 3; E4 = 2. Which sequence is consistent with these changes?

A.Wait, wait, signal, wait ✅
B.Signal, wait, wait, signal
C.Wait, signal, signal, wait
D.Wait, wait, wait, signal
💡 Difficulty: medium | ✅ Correct: A

📖 Explanation: The count falls twice, from 4 to 3 to 2, so two waits occur first. It then rises to 3 after a signal and falls to 2 after another wait, matching the first sequence.

Q23. A program uses a semaphore to notify a waiting process that an event has occurred. Which statement about the process performing signal is generally correct?

A.It must be the same process that previously performed wait
B.It may be a different process from the one that later waits ✅
C.It must own a mutex with the same name
D.It can signal only when the semaphore began at 1
💡 Difficulty: easy | ✅ Correct: B

📖 Explanation: Event synchronization commonly has one process produce a notification and another consume it. Unlike an ownership-based mutex, a semaphore signal is not generally restricted to the process that performed the corresponding wait.

Q24. Three clients compete for one available license represented by a semaphore initialized to 1. One client acquires it; two clients wait. When the license is released, what can be concluded without a stated queueing policy?

A.Both waiting clients must acquire the license together
B.A waiting client can proceed, but the identity of the next client is not guaranteed ✅
C.The releasing client must reacquire the license first
D.The semaphore permanently rejects both waiting clients
💡 Difficulty: medium | ✅ Correct: B

📖 Explanation: The signal makes the resource available to a waiter, but the selection policy is unspecified. Without a FIFO or other fairness guarantee, the exact waiting client that proceeds cannot be inferred.

Q25. A service has four identical download slots and initializes a counting semaphore to 4. Four downloads have acquired permits; a fifth is waiting. One download finishes and signals once. What does the signal make possible?

A.One waiting download can acquire the released capacity ✅
B.All waiting downloads can acquire slots simultaneously
C.The semaphore count must increase to four additional permits
D.The fifth download can proceed only after the semaphore is reinitialized
💡 Difficulty: medium | ✅ Correct: A

📖 Explanation: A completed download returns one permit. That permits one waiting request to proceed, preserving the four-slot limit. A single release does not create capacity for every waiting download while respecting the pool limit.

Q26. A worker acquires a permit for a shared service, then exits through an error path without signaling. Later workers remain blocked even though the service is idle. What usage practice prevents this avoidable failure?

A.Ensure every successful acquisition has a matching release on all exit paths ✅
B.Signal before the worker acquires the permit
C.Increase the initial count whenever a worker starts
D.Allow waiting workers to bypass the semaphore after a timeout
💡 Difficulty: hard | ✅ Correct: A

📖 Explanation: A permit remains consumed if an error path skips the release. Cleanup must return the permit after the protected use ends, including exceptional exits, so idle capacity does not remain unavailable.

Q27. Process P acquires semaphore X and then waits for Y. Process Q acquires Y and then waits for X. Both semaphores started at 1, and neither process releases its first permit. Which outcome is possible?

A.Both processes can wait indefinitely while holding the resource each other needs ✅
B.Both processes must complete because each semaphore began at 1
C.A signal on X is generated automatically when Q waits for X
D.The two semaphore counts merge into one shared permit
💡 Difficulty: hard | ✅ Correct: A

📖 Explanation: P holds X while requesting Y, and Q holds Y while requesting X. Each waits for a permit held by the other, so the chosen acquisition order can create a deadlock.

Q28. A producer fills a data structure and then signals eventReady. A consumer waits on eventReady before reading the structure. Which additional property must the synchronization mechanism provide for reliable publication?

A.The producer must signal before writing so the consumer can prepare
B.The signal and wait must establish the required memory visibility of the producer writes ✅
C.The consumer must rewrite the data before reading it
D.The event semaphore must have an initial count equal to the number of bytes
💡 Difficulty: hard | ✅ Correct: B

📖 Explanation: Correct sequencing requires the producer to finish its writes before signaling. The synchronization primitive must also make those writes visible to the consumer after its wait, not merely transfer a logical permit.

Q29. A monitoring table reports blocked consumers over time: t0 = 0; t1 = 1; t2 = 3; t3 = 2; t4 = 0. Which interpretation is best supported by these values?

A.The number of consumers unable to obtain work first rises, then falls ✅
B.The semaphore count must equal 0, 1, 3, 2, and 0 respectively
C.Exactly three consumers execute simultaneously at t2
D.The producer has necessarily stopped permanently at t4
💡 Difficulty: medium | ✅ Correct: A

📖 Explanation: The table records how many consumers are blocked, not the internal semaphore count or the number running. The rise and fall indicate changing availability or wakeups, but reveal no exact execution schedule.

Q30. Stage B must start only after stage A completes, and stage C only after B completes. With event semaphores AB and BC both initialized to 0, which order is correct?

A.A signals AB; B waits AB, completes, then signals BC; C waits BC ✅
B.B signals AB; A waits AB; C signals BC before B starts
C.A waits AB; B waits BC; C signals both events
D.C waits AB; A signals BC; B starts without waiting
💡 Difficulty: medium | ✅ Correct: A

📖 Explanation: The first semaphore carries the A-to-B completion event, and the second carries B-to-C. Each stage waits for its prerequisite and signals only after its own work is complete in the specified dependency chain.

Q31. A semaphore initialized to 0 receives five signals. A dispatcher performs four successful waits, with no other operations. How many permits remain?

A.0
B.1 ✅
C.4
D.5
💡 Difficulty: easy | ✅ Correct: B

📖 Explanation: Five signals raise the count from zero to five. Four successful waits consume four permits, leaving one. The remaining permit represents one signaled event that has not yet been consumed.

Q32. A resource semaphore starts at 6. Three clients acquire permits, and then five signal operations occur, although only three clients actually release resources. What is the main issue?

A.The arithmetic count becomes 8, exceeding the real pool capacity because two signals are unmatched ✅
B.The count becomes 2 because signal cancels every earlier wait
C.The count remains 6 because signals are ignored when the pool is busy
D.The count becomes 3 because only acquired permits can be counted
💡 Difficulty: hard | ✅ Correct: A

📖 Explanation: The arithmetic is 6 minus 3 plus 5, which equals 8. Only three releases are legitimate, so two unmatched signals overstate availability and can permit more clients than the six-resource pool supports.

Q33. A system must allow up to seven identical devices to be used at once. Which initialization correctly represents the available capacity before any process acquires a device?

A.0
B.1
C.6
D.7 ✅
💡 Difficulty: hard | ✅ Correct: D

📖 Explanation: The initial count should equal the number of available device instances. With seven free devices, seven acquisitions may succeed before another request must wait, so the semaphore begins at seven.

Q34. A resource semaphore starts at 5. Process A waits successfully, process B waits successfully, process C signals, and process D then waits successfully. What is the final available count?

A.1
B.2
C.3 ✅
D.4
💡 Difficulty: hard | ✅ Correct: C

📖 Explanation: Starting from 5, A and B reduce the count to 3. C signals, raising it to 4, and D waits, reducing it to 3. The correct final count is three, option C.

Q35. A task-completion event semaphore starts at 0. Task X signals twice; task Y then waits once; task Z signals once; task W performs two waits. How many successful waits occur in total?

A.Two
B.Three ✅
C.Four
D.Five
💡 Difficulty: hard | ✅ Correct: B

📖 Explanation: The two initial signals create two permits; Y consumes one, leaving one. Z adds a second permit, and W consumes both. Across Y and W, three waits succeed before any additional signal arrives.

Q36. A service has one exclusive administrative session and also needs to count four queued requests. Which semaphore arrangement represents both requirements without confusing their meanings?

A.One binary semaphore initialized to 4
B.One counting semaphore initialized to 1 for both conditions
C.A binary semaphore initialized to 1 for the session and a counting semaphore initialized to 4 for queued requests ✅
D.Two binary semaphores both initialized to 0, regardless of the queue length
💡 Difficulty: easy | ✅ Correct: C

📖 Explanation: The session requires a single exclusive permit, while the queue requires a count of four pending requests. Separate semaphores with purpose-specific initial values keep access control distinct from event counting.

🔗 Related Topics (MCQs)