πŸŽ“ BookMCQ
← Back to 5. Process Synchronization

πŸ“ Semaphores in Process Synchronization (36 MCQs)

Counting and Binary Semaphores for Resource Coordination

A semaphore is a synchronization variable used to coordinate processes and threads accessing shared resources or communicating through execution order. Its value represents available permits or a synchronization state, and access occurs through atomic wait and signal operations. Binary semaphores represent two states, while counting semaphores manage multiple available resource instances or outstanding events.

Why Processes Need Signalling Beyond Mutual Exclusion

Mutexes provide exclusive ownership, but many coordination problems require tracking several resource instances or notifying a process that an event has occurred. Semaphores address both requirements through a counter and atomic operations. They support producer-consumer coordination, resource pools, and execution ordering without requiring applications to implement low-level atomic state changes independently.

Atomic Wait and Signal Operations

The wait operation attempts to consume a permit and blocks the caller when none is available. The signal operation adds a permit or wakes an eligible waiting process. These operations must be atomic so competing execution activities cannot incorrectly modify the semaphore state. Implementations may use blocking queues or spinning, depending on context and policy.

Managing Three Identical Resource Instances

Suppose a system has three identical printers represented by a counting semaphore initialized to 3. Each process performs wait before acquiring a printer and signal after releasing it. Three processes can acquire resources simultaneously. A fourth process blocks until one printer becomes available and a permit is returned.

Resource Allocation, Blocking, and Synchronization Risks

Semaphores support mutual exclusion, resource counting, and ordering in operating systems and concurrent applications. Blocking implementations conserve CPU time when resources remain unavailable. However, incorrect wait and signal ordering can cause deadlocks, while unfair wake-up policies may produce starvation. Binary semaphores also differ from ownership-enforcing mutexes, so their interchangeability should not be assumed.

Misconceptions About Semaphore Operations

'A semaphore can never have a value greater than one' is incorrect because counting semaphores represent multiple permits. 'wait and signal are ordinary non-atomic assignments' is incorrect because both require atomic synchronization. 'Every semaphore automatically guarantees fair access' is incorrect because fairness depends on the waiting-queue implementation.

Exam Focus: Semaphore Values and Process Blocking

Trace semaphore values across wait and signal operations, identifying when a process consumes a permit or blocks. Compare binary and counting semaphores and distinguish semaphore signalling from mutex ownership. Questions may involve resource allocation, producer-consumer synchronization, execution ordering, and determining whether a sequence of operations causes deadlock or starvation.

Key Terms of Semaphore Synchronization

  • Semaphore - Integer-based synchronization mechanism.
  • wait() - Attempts to consume a permit.
  • signal() - Returns a permit or notifies.
  • Binary semaphore - Semaphore representing two states.
  • Counting semaphore - Tracks multiple resource permits.
  • Permit - Available unit of resource access.
12
Easy
12
Medium
12
Hard

πŸ“ All Semaphores in Process Synchronization MCQs

Q1. A kernel provides a synchronization variable whose state can be changed through wait and signal operations. Which description best identifies a semaphore?

A.A process identifier used to select a CPU
B.A synchronization object with an integer-like state and atomic wait and signal operations βœ…
C.A memory address that permanently identifies a shared resource
D.A scheduling queue that stores only terminated processes
πŸ’‘ Difficulty: easy | βœ… Correct: B

πŸ“– Explanation: A semaphore records synchronization state, commonly as a count of available permits. Processes interact with it through atomic wait and signal operations, rather than modifying its state through ordinary unsynchronized assignments.

Q2. A binary semaphore is used to represent whether one shared resource is available. Which values are valid for this semaphore?

A.Any positive integer
B.Only negative integers
C.Zero or one βœ…
D.Only zero or two
πŸ’‘ Difficulty: easy | βœ… Correct: C

πŸ“– Explanation: A binary semaphore has two logical states: zero represents no available permit, while one represents an available permit. A counting semaphore is used when the system must represent multiple available units.

Q3. A service has five interchangeable database connections and wants to allow no more than five clients to hold connections simultaneously. What initial value should its counting semaphore use?

A.5 βœ…
B.1
C.0
D.6
πŸ’‘ Difficulty: medium | βœ… Correct: A

πŸ“– Explanation: The initial count should equal the number of available interchangeable connections. Starting at five allows five successful acquisitions; a sixth client must wait until a connection is released and its permit is returned.

Q4. Which property makes a counting semaphore more suitable than a binary semaphore for controlling a pool of several identical devices?

A.A counting semaphore never blocks a process
B.A counting semaphore guarantees that every process runs immediately
C.A counting semaphore enforces ownership by the process that first signals
D.A counting semaphore can represent more than one available permit βœ…
πŸ’‘ Difficulty: medium | βœ… Correct: D

πŸ“– Explanation: A counting semaphore can represent several available units, so its count can exceed one. A binary semaphore represents only two logical states and therefore cannot directly record the size of a larger resource pool.

Q5. A semaphore has value 1. Two processors execute wait on it at nearly the same time. A student says both may read 1, decrement it, and enter the protected region. Which flaw does this reasoning expose?

A.The signal operation is being called too frequently
B.The semaphore must be initialized to zero
C.The test for availability and the decrement are not being performed atomically βœ…
D.A semaphore cannot be shared between processors
πŸ’‘ Difficulty: hard | βœ… Correct: C

πŸ“– Explanation: The availability test and decrement form one indivisible operation. If they are separate, both processors can observe the same permit before either updates the count, allowing two acquisitions from a single available unit.

Q6. A process calls wait on a counting semaphore whose current value is 4. Assuming no competing operation occurs, what happens?

A.The value becomes 3 and the process continues βœ…
B.The value becomes 5 and the process blocks
C.The value remains 4 and the process terminates
D.The value becomes zero and all waiting processes wake
πŸ’‘ Difficulty: easy | βœ… Correct: A

πŸ“– Explanation: When the semaphore has a positive count, wait consumes one permit and allows the caller to continue. With an initial value of four, one successful wait leaves three permits available.

Q7. In a blocking semaphore implementation, a process calls wait when the semaphore has no available permits. What normally happens to that process?

A.It continues and uses the resource anyway
B.It enters a waiting state until a permit becomes available βœ…
C.It changes the semaphore value to the maximum integer
D.It signals the semaphore automatically
πŸ’‘ Difficulty: easy | βœ… Correct: B

πŸ“– Explanation: When no permit is available, the calling process cannot complete the acquisition. A blocking implementation places it in the semaphore waiting queue and makes it eligible to continue after a suitable signal.

Q8. A process successfully releases a resource protected by a counting semaphore. No process is waiting on that semaphore. What is the usual abstract effect of signal?

A.It permanently disables future wait operations
B.It resets the semaphore to zero
C.It terminates the process that released the resource
D.It adds one available permit to the semaphore βœ…
πŸ’‘ Difficulty: easy | βœ… Correct: D

πŸ“– Explanation: When no process is waiting, signal makes another permit available by increasing the semaphore count. That permit can later be consumed by a process that calls wait on the same semaphore.

Q9. Process P is blocked on semaphore S. Process Q executes signal(S), and P is moved to the ready state. Why might P still not execute immediately?

A.A signal always requires P to restart
B.The semaphore must first be initialized again
C.The CPU scheduler still decides when the ready process receives processor time βœ…
D.P must execute signal before it can run
πŸ’‘ Difficulty: medium | βœ… Correct: C

πŸ“– Explanation: A signal can make a blocked process ready, but readiness is not the same as running. The scheduler selects among ready processes, so P may wait for processor time after being unblocked.

Q10. A student implements wait as two separate steps: first read S, then decrement S. With S equal to 1, two threads both read the value before either decrements it. What is the correct diagnosis?

A.The wait operation is not atomic, so both threads can consume the same permit βœ…
B.The signal operation has incorrectly increased S
C.The semaphore should always be initialized to a negative value
D.The threads must be using different semaphore objects
πŸ’‘ Difficulty: hard | βœ… Correct: A

πŸ“– Explanation: The flawed step is separating the availability check from the decrement. They must occur atomically; otherwise both threads can act on the same observed permit and violate the resource limit.

Q11. Process P1 must complete statement X before process P2 begins statement Y. A shared semaphore S is initialized to zero. Which arrangement enforces the required order?

A.P1 waits on S before X, while P2 signals after Y
B.P1 signals S after X, while P2 waits on S before Y βœ…
C.Both processes signal S before their statements
D.Both processes wait on S after their statements
πŸ’‘ Difficulty: medium | βœ… Correct: B

πŸ“– Explanation: The zero initial value prevents P2 from passing its wait before the event occurs. P1 signals after completing X, allowing P2 to pass wait and then execute Y in the required order.

Q12. A traditional semaphore model permits negative values to record waiting requests. S starts at 2, and four processes each call wait; no signal occurs. What is the resulting logical value?

A.2
B.0
C.4
D.-2 βœ…
πŸ’‘ Difficulty: hard | βœ… Correct: D

πŸ“– Explanation: Each wait accounts for one request. Starting at two, four waits produce the logical count 2βˆ’4=βˆ’22-4=-2; the negative magnitude represents two requests waiting beyond the two initially available permits at that point.

Q13. A counting semaphore starts at 5. The operation sequence is wait, wait, signal, wait, signal, with no blocked callers or other activity. What is its final value?

A.2
B.4 βœ…
C.5
D.6
πŸ’‘ Difficulty: medium | βœ… Correct: B

πŸ“– Explanation: Each wait subtracts one and each signal adds one. The sequence gives 5βˆ’1βˆ’1+1βˆ’1+1=45-1-1+1-1+1=4, so four permits remain after all five operations have completed without any direct handoff to waiters here.

Q14. A laboratory has three identical scanners. A counting semaphore is initialized to 3, and each process acquires one scanner by calling wait. Before any scanner is released, how many processes can acquire one?

A.1
B.2
C.3 βœ…
D.4
πŸ’‘ Difficulty: easy | βœ… Correct: C

πŸ“– Explanation: The initial count represents three available scanners. Each successful wait consumes one permit, so three processes can acquire scanners before the count reaches zero; any additional requester must wait under this stated no-release condition.

Q15. A server limits active uploads with a counting semaphore initialized to 8. Seven uploads have acquired permits, then one upload releases its permit. How many permits are available afterward?

A.2 βœ…
B.1
C.7
D.8
πŸ’‘ Difficulty: medium | βœ… Correct: A

πŸ“– Explanation: Seven successful waits reduce the count from eight to one. The completed upload signals once, returning one permit, so the available count becomes 8βˆ’7+1=28-7+1=2. No other requests or releases affect this result.

Q16. A program cancels a request while it is still blocked in wait(S), before that wait acquires a permit. Its cleanup handler calls signal(S) unconditionally. What defect can result?

A.The semaphore is converted into a mutex
B.The blocked request consumes two permits
C.The semaphore becomes permanently inaccessible
D.An extra permit may be created because the cancelled request never consumed one βœ…
πŸ’‘ Difficulty: hard | βœ… Correct: D

πŸ“– Explanation: Cleanup must determine whether acquisition succeeded. Signaling after cancellation before acquisition returns a permit that was never consumed, inflating the count and potentially allowing more concurrent users than the resource capacity.

Q17. A graph of semaphore S over time gives these points: (0,0), (1,1), (2,1), (3,0), (4,0), where each pair is (time, available permits). What does the graph show at time 2?

A.One permit is available and has not yet been consumed βœ…
B.Two processes have acquired the resource
C.A process is necessarily blocked
D.The semaphore has become a binary mutex with value zero
πŸ’‘ Difficulty: medium | βœ… Correct: A

πŸ“– Explanation: The plotted value remains one from time 1 through time 2, so a permit is still available at time 2. The later drop to zero indicates that a wait consumed it between those observations.

Q18. A graph for a two-connection service plots blocked clients over time: (0,0), (1,0), (2,1), (3,2), (4,2). Both connections remain in use throughout the interval. What is the best interpretation?

A.The semaphore has granted more permits than the service capacity
B.The system must have a processor scheduling failure
C.The resource pool is exhausted, so additional clients are waiting; the graph alone does not prove deadlock βœ…
D.The semaphore count must have increased at every point
πŸ’‘ Difficulty: hard | βœ… Correct: C

πŸ“– Explanation: Two held connections exhaust the configured capacity, explaining why later clients block. The rising wait count indicates contention, but deadlock cannot be concluded without evidence that the holders cannot release their connections.

Q19. A binary semaphore initialized to 1 protects a shared counter. Thread A completes wait and enters the update; thread B calls wait while A still holds the permit. What should happen?

A.B waits until A releases the permit βœ…
B.B enters because binary semaphores allow two users
C.The semaphore changes automatically into a counting semaphore
D.A is forced to terminate
πŸ’‘ Difficulty: easy | βœ… Correct: A

πŸ“– Explanation: The initial permit allows one thread to enter. A successful wait consumes it, so B cannot enter until A performs the corresponding signal and makes a permit available again inside the protected critical section.

Q20. A cloud service uses a semaphore initialized to 6 for six equivalent worker slots. Five jobs have successfully acquired slots. A sixth job calls wait while one slot is still free. What should occur?

A.The sixth job blocks because five acquisitions always exhaust the pool
B.The sixth job proceeds and consumes the final permit βœ…
C.The semaphore changes to zero before the sixth call
D.The sixth job must signal twice before proceeding
πŸ’‘ Difficulty: medium | βœ… Correct: B

πŸ“– Explanation: Five successful waits reduce the count from six to one. Because one permit remains, the sixth job can acquire it; the count then becomes zero, and only later requests must block.

Q21. In a shared task queue, producers first wait for an available-slot semaphore and then acquire a binary semaphore protecting queue updates. Why is this order safer than acquiring the binary semaphore first?

A.It guarantees producers always run before consumers
B.It removes the need to return permits
C.It makes queue updates non-atomic
D.A producer blocked for lack of capacity does not hold the queue lock needed by a consumer to remove an item βœ…
πŸ’‘ Difficulty: hard | βœ… Correct: D

πŸ“– Explanation: A producer that waits for capacity before locking cannot block a consumer while holding the queue lock. The consumer can remove an item, release the lock, and return a capacity permit.

Q22. A service has one instance each of resource A and resource B. A developer uses a single counting semaphore initialized to 2 for both, but clients may specifically request A twice. Why is the single count insufficient by itself?

A.It counts interchangeable permits but does not identify which distinct resource remains available βœ…
B.A counting semaphore cannot be initialized to 2
C.A semaphore always assigns the first resource to every requester
D.The two resources must be acquired by the same process
πŸ’‘ Difficulty: hard | βœ… Correct: A

πŸ“– Explanation: A count of two describes the total number of permits, not the identity of each resource. Separate availability state or a selection protocol is needed to prevent two clients from claiming the same exclusive resource.

Q23. Thread A acquires a semaphore, but the API permits thread B to call signal on that semaphore even though B did not acquire it. Which distinction should a programmer remember?

A.Semaphores can only be used inside one process
B.A semaphore necessarily records the acquiring thread as its owner
C.Semaphore signaling does not universally enforce mutex-style ownership by the signaling thread βœ…
D.A signal from another thread always terminates the process
πŸ’‘ Difficulty: medium | βœ… Correct: C

πŸ“– Explanation: A semaphore generally manages permits and waiting, rather than enforcing ownership in the same way as an ownership-checking mutex. Programs must therefore ensure that each signal corresponds to a valid release in their protocol.

Q24. Thread A enters a critical section after wait on a binary semaphore. Thread B, which never acquired the protected resource, incorrectly signals it. Thread C then completes wait. What is the principal risk?

A.Thread A is guaranteed to leave immediately
B.Thread C may enter while A is still inside, violating mutual exclusion βœ…
C.The semaphore count must become negative
D.Thread B automatically becomes the new owner of A’s data
πŸ’‘ Difficulty: hard | βœ… Correct: B

πŸ“– Explanation: The incorrect signal can make a permit available while A is still using the protected region. C may then acquire that permit and enter concurrently, because the semaphore protocol no longer represents actual resource ownership.

Q25. Process B must not execute statement Y until process A has completed statement X. If the event semaphore starts at zero, what does B’s wait accomplish?

A.It makes A execute X a second time
B.It prevents A from ever signaling
C.It lets B pass immediately without an event
D.It keeps B from passing until A signals completion βœ…
πŸ’‘ Difficulty: easy | βœ… Correct: D

πŸ“– Explanation: A zero-valued event semaphore has no permit initially, so B blocks at wait. After A completes X and signals, B can pass the wait and safely begin Y under the intended execution protocol.

Q26. A graph for event semaphore E shows (time, count): (0,0), (1,1), (2,1), (3,0), (4,0). Which sequence is consistent with these observations?

A.A wait occurred before any signal, followed by two signals
B.Two waits occurred before the first signal
C.A signal created a retained permit, which a later wait consumed βœ…
D.The semaphore was initialized to 4 and never modified
πŸ’‘ Difficulty: easy | βœ… Correct: C

πŸ“– Explanation: The count rises from zero to one at time 1, remains available at time 2, and later returns to zero. This is consistent with signal occurring before a subsequent wait consumes the retained permit.

Q27. A dependency semaphore should start at zero so task B waits for task A. A programmer initializes it to one instead. What can happen?

A.B can pass wait before A completes, violating the intended ordering βœ…
B.A cannot execute its signal operation
C.B will always wait for two signals
D.The semaphore automatically restores the correct initial value
πŸ’‘ Difficulty: easy | βœ… Correct: A

πŸ“– Explanation: The initial permit allows B to pass wait immediately, even though A has not yet completed. A zero initial value is needed when the only valid permit must come from A’s completion signal.

Q28. A producer writes a shared record and then signals a semaphore. A consumer successfully waits before reading the record. What implementation guarantee is also important for reliable data visibility?

A.The consumer must sleep for a fixed delay
B.The semaphore count must equal the record size
C.The signal and successful wait must provide suitable memory-ordering synchronization βœ…
D.The record must be copied into the consumer’s stack before signaling
πŸ’‘ Difficulty: medium | βœ… Correct: C

πŸ“– Explanation: The event protocol orders the producer before the consumer, but shared writes must also become visible across threads. Semaphore operations should provide the required release and acquire memory-ordering guarantees in the consumer’s subsequent read.

Q29. A notification system must preserve the number of five separate events even when the receiver has not yet begun waiting. Why is a counting semaphore more suitable than a binary semaphore?

A.A binary semaphore always stores every repeated notification
B.A counting semaphore can accumulate multiple unconsumed permits, while a binary state cannot represent five pending events βœ…
C.A counting semaphore prevents the receiver from blocking
D.A binary semaphore requires each event to terminate a process
πŸ’‘ Difficulty: medium | βœ… Correct: B

πŸ“– Explanation: A counting semaphore can retain five permits for five events until they are consumed. A binary semaphore has only two states, so repeated signals cannot represent an arbitrary number of distinct pending events.

Q30. Process P1 begins by waiting on semaphore A and then signals B. Process P2 begins by waiting on B and then signals A. Both semaphores start at zero. What is the likely outcome?

A.Both processes signal before waiting
B.P1 completes because it was created first
C.Both processes can block indefinitely before reaching either signal βœ…
D.Both semaphores automatically change to one when a process waits
πŸ’‘ Difficulty: hard | βœ… Correct: C

πŸ“– Explanation: Each process blocks on its first wait because the corresponding semaphore is zero. Neither can reach its signal operation, so both remain blocked in a circular dependency unless another action intervenes.

Q31. A graph of available permits shows (time, count): (0,5), (1,4), (2,3), (3,2), (4,3). Which interval most likely contains a signal operation, assuming one operation occurs between successive observations?

A.Between time 0 and time 1
B.Between time 1 and time 2
C.Between time 2 and time 3
D.Between time 3 and time 4 βœ…
πŸ’‘ Difficulty: easy | βœ… Correct: D

πŸ“– Explanation: The count decreases by one in each of the first three intervals, consistent with waits. It rises from two to three in the final interval, indicating one signal added a permit.

Q32. Several processes repeatedly wait on a semaphore. The wake-up policy always selects the newest waiter, while one older process is bypassed indefinitely. Which risk does this illustrate?

A.The semaphore has become a counting semaphore
B.The atomic decrement has stopped working
C.The semaphore count must be negative
D.Starvation can occur because semaphore fairness depends on the waiting and wake-up policy βœ…
πŸ’‘ Difficulty: hard | βœ… Correct: D

πŸ“– Explanation: Atomic wait and signal operations do not by themselves guarantee fair service. If the wake-up policy repeatedly bypasses an older waiter, that process may remain blocked indefinitely despite other requests progressing.

Q33. A counting semaphore starts at 3. A student calculates that two successful waits followed by three signals leave the value at 3, claiming signals only wake blocked processes. No process is blocked. What is the correct value?

A.4, because the two waits reduce the count to 1 and three signals add three permits βœ…
B.3, because every signal is discarded when no process waits
C.1, because signals do not change a counting semaphore
D.0, because the first wait consumes every permit
πŸ’‘ Difficulty: medium | βœ… Correct: A

πŸ“– Explanation: The flawed step is treating signal as a no-op when nobody waits. With no blocked process, each signal adds a permit: 3βˆ’2+3=43-2+3=4, so the final count is four as part of the stated operation sequence.

Q34. A process acquires the only permit for a shared file and crashes before signaling. Another process waits indefinitely. Which response best addresses this failure mode?

A.Assume the semaphore will detect the crash and release itself
B.Increase the semaphore value after every wait
C.Add a recovery protocol that can identify and restore resources after an abandoned acquisition βœ…
D.Allow the blocked process to enter without acquiring the permit
πŸ’‘ Difficulty: hard | βœ… Correct: C

πŸ“– Explanation: A semaphore does not inherently detect that a holder has crashed. A robust design needs recovery, ownership tracking, timeout handling, or another mechanism to restore the abandoned resource without creating duplicate permits.

Q35. A student uses one counting semaphore with value 2 for two different exclusive resources, A and B. Two clients both request A and successfully consume the two permits. Which part of the design is wrong?

A.The permit count tracks the total but not which resource each client acquired; resource-specific availability must also be enforced βœ…
B.A counting semaphore can never represent two permits
C.The clients should signal before making their requests
D.The semaphore must be initialized to zero for every resource
πŸ’‘ Difficulty: hard | βœ… Correct: A

πŸ“– Explanation: A single count represents two interchangeable permits and cannot distinguish resource A from B. The design must track each resource separately or enforce selection so both permits cannot be spent on the same exclusive item.

Q36. A client successfully acquires one permit from a semaphore and later finishes using the associated resource. What should the client normally do to make that capacity available again?

A.Call wait one additional time
B.Call signal exactly once as part of the release protocol βœ…
C.Set the semaphore directly to its original value
D.Terminate every process waiting on the semaphore
πŸ’‘ Difficulty: easy | βœ… Correct: B

πŸ“– Explanation: A successful acquisition consumes one permit, so releasing that resource should return one permit or hand it to an eligible waiter. The release protocol should signal exactly once for that acquisition.

πŸ”— Related Topics (MCQs)