🎓 BookMCQ
← Back to 5. Process Synchronization

📝 Implementing a Monitor Using Semaphores in Process synchronization (36 MCQs)

Building Monitor Mutual Exclusion from Semaphore Primitives

A monitor can be implemented using semaphores by combining a binary semaphore for monitor entry with additional synchronization variables for condition waiting and signalling. This approach translates the high-level monitor abstraction into lower-level atomic operations. The implementation must ensure that only one process executes inside the monitor and that condition-variable operations correctly suspend and resume waiting processes.

Why High-Level Monitor Operations Need Low-Level Support

Hardware provides atomic operations and operating systems provide synchronization primitives, but programming languages may express coordination through monitors. Implementing monitors with semaphores creates the required mutual exclusion and condition-waiting behaviour without requiring dedicated monitor hardware. The main difficulty is preserving the monitor's specified signalling semantics while transferring exclusive execution between waiting and active processes.

Entry Semaphore and Condition-Specific Waiting Queues

A classical implementation uses a binary semaphore mutex initialized to one for monitor entry. Each condition variable has its own semaphore and waiting count. A condition wait releases the monitor mutex, blocks on its condition semaphore, and reacquires monitor access when resumed. Signal operations wake an appropriate condition waiter according to the chosen monitor semantics.

Implementing a Wait and Signal Sequence

Suppose process P enters a monitor using wait(mutex), then discovers that condition x is false. It increments x_count, signals mutex to release monitor access, and waits on x_sem. When process Q changes the state and signals x, P is awakened and must regain monitor access before continuing its protected operation.

Implementation Correctness and Semaphore Coordination Costs

Semaphore-based monitor implementations provide a foundation for translating structured synchronization into atomic blocking and wake-up operations. They are useful for understanding runtime and operating system coordination. However, correct implementation requires careful handling of waiter counts, handoff rules, and competing entrants. A missing signal or incorrect ordering can cause deadlock, lost wake-ups, or invalid monitor execution.

Misconceptions About Implementing Monitors with Semaphores

'One binary semaphore is sufficient for every monitor condition' is incorrect because separate conditions require distinct waiting mechanisms. 'Condition wait keeps holding the monitor mutex' is incorrect because it must release monitor access while blocking. 'Any semaphore implementation automatically matches every monitor model' is incorrect because signalling semantics differ.

Exam Focus: Translating Monitor Operations into Semaphores

Identify the monitor-entry semaphore, condition semaphores, and associated waiter counters. Trace how wait releases mutual exclusion and how signal resumes a blocked process. Questions may examine semaphore initialization, signaler handoff, condition-queue management, and the differences between Hoare-style and Mesa-style monitor implementations.

Key Terms of Semaphore-Based Monitor Implementation

  • Monitor mutex - Semaphore controlling monitor entry.
  • Condition semaphore - Blocks condition-variable waiters.
  • Waiter count - Number of condition-blocked processes.
  • Handoff - Transfer of monitor execution rights.
  • Semaphore queue - Stores blocked synchronization participants.
  • Condition signal - Wakes an eligible condition waiter.
12
Easy
12
Medium
12
Hard

📝 All Implementing a Monitor Using Semaphores in Process synchronization MCQs

Q1. In a semaphore-based monitor, what initial value is assigned to the entry semaphore `mutex` in the standard single-entrant design?

A.0, because the monitor must start locked
B.1, representing one available entry permit ✅
C.The total number of threads expected to call the monitor
D.2, allowing one process and one signaler to enter
💡 Difficulty: easy | ✅ Correct: B

📖 Explanation: The initial value one represents a single available entry permit. A process consumes it before entering, and the monitor's exit protocol returns access only after protected execution finishes inside the monitor.

Q2. What is the role of the `next` semaphore in a signal-and-wait implementation of a monitor?

A.It counts processes waiting outside the monitor
B.It stores notifications for condition variables that have no waiters
C.It lets a signaling process suspend while the awakened condition waiter runs ✅
D.It permits several external procedures to execute concurrently
💡 Difficulty: easy | ✅ Correct: C

📖 Explanation: The `next` semaphore lets a signaling process suspend while the awakened condition waiter runs. After the waiter leaves or waits again, the handoff can resume the suspended signaler without admitting an outside entrant.

Q3. What does the integer variable `next_count` record in the semaphore-based monitor protocol?

A.The number of signaling processes suspended on `next` ✅
B.The number of processes waiting on every condition combined
C.The number of threads blocked before entering through `mutex`
D.The number of monitor procedures currently executing
💡 Difficulty: easy | ✅ Correct: A

📖 Explanation: The variable `next_count` records how many signaling processes are suspended on `next`. It does not count condition waiters or external entrants, and guides the monitor's handoff decisions when procedures finish or waiters yield.

Q4. For each condition variable x in the standard semaphore construction, which pair is created and initialized to zero?

A.A shared entry semaphore and the number of external callers
B.A counting semaphore initialized to one and a monitor-wide queue length
C.The monitor mutex and the number of active procedures
D.A condition semaphore `x_sem` and a waiter counter `x_count` ✅
💡 Difficulty: medium | ✅ Correct: D

📖 Explanation: Each condition variable has its own zero-initialized semaphore and waiter counter. This keeps notifications tied to the predicate being awaited, rather than waking a process waiting for a different condition.

Q5. When an external monitor procedure F finishes, which exit rule preserves the signal-and-wait handoff?

A.Always signal `mutex`, regardless of suspended signalers
B.Signal `next` if `next_count` is positive; otherwise signal `mutex` ✅
C.Signal every condition semaphore before returning
D.Decrement every condition waiter count and then signal `mutex`
💡 Difficulty: medium | ✅ Correct: B

📖 Explanation: At procedure exit, a suspended signaller has priority: signal `next` when `next_count` is positive; otherwise signal `mutex`. This preserves the internal handoff before admitting a new entrant from outside the monitor.

Q6. A process calls `x.signal()` when `x_count` is zero. What should the standard condition-variable implementation do?

A.Increase `x_sem` so the next waiter can pass
B.Signal `mutex` twice to release the monitor
C.Do nothing because no process is registered on condition x ✅
D.Wake a process waiting on any other condition
💡 Difficulty: easy | ✅ Correct: C

📖 Explanation: If `x_count` is zero, no process is registered on condition x, so `x.signal()` does not release `x_sem`. This prevents an unused notification from becoming a permit for a future wait.

Q7. Why must `x_count` be incremented before the calling process releases monitor access in `x.wait()`?

A.So the count represents all threads in the operating system
B.So the condition semaphore becomes the entry lock
C.So an external entrant can bypass the monitor queue
D.So a signaler cannot inspect the queue before the waiter is registered ✅
💡 Difficulty: easy | ✅ Correct: D

📖 Explanation: The waiter count must increase before the monitor lock is released, while the caller still controls the monitor. A signaler then cannot overlook a process that is preparing to block.

Q8. Why does a process waiting on a monitor condition release the monitor entry lock before blocking?

A.Another monitor procedure may need to change the predicate that the waiter is waiting for ✅
B.The condition semaphore can only be signaled outside a monitor
C.The entry lock must be available to every process simultaneously
D.A blocked process must continue modifying protected state
💡 Difficulty: easy | ✅ Correct: A

📖 Explanation: A blocked condition waiter must release monitor access so another procedure can change the predicate. Retaining the entry lock would prevent the required producer or state-changing process from entering the monitor.

Q9. In this implementation, what does `x_count` represent?

A.All processes that have ever called condition x
B.Only processes currently executing inside the monitor
C.Processes registered as waiting on condition x, including a waiter between lock release and its semaphore block ✅
D.Processes waiting to acquire the monitor entry semaphore
💡 Difficulty: medium | ✅ Correct: C

📖 Explanation: The counter represents processes registered on condition x, including one that has released monitor access but has not yet completed its semaphore wait. It is not the number outside the monitor.

Q10. After `x.signal()` wakes a condition waiter, why does the signaling process execute `wait(next)` in a signal-and-wait implementation?

A.To convert the condition semaphore into a counting semaphore
B.To suspend itself while the awakened waiter receives monitor execution ✅
C.To allow the signaler and waiter to execute protected code together
D.To release all external entrants before the waiter runs
💡 Difficulty: medium | ✅ Correct: B

📖 Explanation: The signaler waits on `next` after waking a condition waiter, yielding monitor execution to that waiter. This prevents both processes from continuing protected monitor code at the same time inside it.

Q11. Why can `next_count` and condition waiter counters be updated without separate locks in the classical implementation?

A.Semaphores make every integer update atomic even when monitor procedures overlap
B.The counters are read-only after initialization
C.Every condition variable owns the entire monitor exclusively
D.Only one process is active inside the monitor, so the bookkeeping updates are serialized by monitor ownership ✅
💡 Difficulty: hard | ✅ Correct: D

📖 Explanation: Because monitor code admits only one active process, updates to `next_count` and each condition's waiter count are serialized by monitor ownership. Separate locks for these bookkeeping variables are therefore unnecessary in this design.

Q12. A semaphore-based monitor provides correct mutual exclusion, but its entry queue has no specified fairness policy. Which conclusion is justified?

A.Mutual exclusion may hold even though starvation freedom is not guaranteed ✅
B.Every entrant is guaranteed to be served in arrival order
C.The monitor must admit all blocked entrants at once
D.A correct mutex automatically prevents priority inversion
💡 Difficulty: medium | ✅ Correct: A

📖 Explanation: Mutual exclusion only ensures that protected procedures do not overlap. A semaphore's scheduling policy may still repeatedly favor one entrant, so fairness and starvation freedom require additional guarantees from the scheduler or queue.

Q13. A waiter increments `x_count` and releases monitor access, but has not yet executed `wait(x_sem)`. A signaler then calls `x.signal()`. Why can the waiter still receive the notification?

A.The monitor mutex converts the signal into a broadcast
B.The signaler is required to wait until the waiter reaches the semaphore call
C.The semaphore can retain the signal permit until the registered waiter executes `wait(x_sem)` ✅
D.The waiter count automatically resumes the waiting process
💡 Difficulty: hard | ✅ Correct: C

📖 Explanation: The waiter has already registered by increasing `x_count`, so the signaler recognizes it and signals `x_sem`. The semaphore retains that permit until the waiter reaches its wait operation, avoiding a missed wake-up.

Q14. Why does an external procedure signal `next` instead of `mutex` when `next_count` is positive?

A.To increase the number of permitted concurrent entrants
B.To resume a process already suspended inside the monitor before admitting a new outside caller ✅
C.To wake every process waiting on all condition variables
D.To ensure the monitor mutex remains permanently unavailable
💡 Difficulty: hard | ✅ Correct: B

📖 Explanation: The exit test checks `next_count` because a process already suspended inside the monitor should regain control before a new external caller. Signaling `mutex` first would break the intended handoff priority.

Q15. The entry semaphore starts at 1. T1 enters and T2 blocks on `wait(mutex)`. T1 finishes with `next_count = 0`. Which operation allows T2 to enter?

A.`signal(mutex)` executed by T1 ✅
B.`signal(x_sem)` for every condition
C.`signal(next)` executed by T2
D.`wait(mutex)` executed again by T1
💡 Difficulty: easy | ✅ Correct: A

📖 Explanation: P1's exit code must return the entry permit because no signaller is suspended on `next`. That `signal(mutex)` releases the single lock so blocked T2 can enter next under mutual exclusion.

Q16. At the end of monitor procedure F, `next_count = 2`. Which semaphore should F signal according to the exit protocol?

A.`mutex`, so an outside entrant goes first
B.`x_sem`, because every exit must notify condition x
C.`both `mutex` and `next` to wake two processes
D.`next`, to resume one suspended signaller before an outside entrant ✅
💡 Difficulty: easy | ✅ Correct: D

📖 Explanation: A positive `next_count` means suspended signallers are waiting for their handoff. The procedure must signal `next`, not `mutex`, so one suspended process resumes before a new external caller can enter.

Q17. There are three registered waiters on condition x. One `x.signal()` wakes one waiter, and that waiter resumes past `wait(x_sem)`. What is `x_count` after it performs its decrement?

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

📖 Explanation: The signal operation wakes one condition waiter; when that waiter continues past `wait(x_sem)`, it reduces `x_count` once. Starting from three registered waiters, the remaining count is two in this trace.

Q18. A process executes `x.wait()` while `next_count = 2`. Immediately after increasing `x_count`, which semaphore does the wait operation signal before blocking on `x_sem`?

A.`x_sem`, because the caller is waiting on x
B.`mutex`, because all wait operations release only the entry lock
C.`next`, because a suspended signaller has priority over an outside entrant ✅
D.No semaphore; it keeps monitor access while blocked
💡 Difficulty: medium | ✅ Correct: C

📖 Explanation: The wait path increments `x_count` and checks `next_count`. Because a signaller is suspended, it signals `next` to resume that process before the current thread blocks on `x_sem` for condition x.

Q19. A process calls `x.signal()` with `x_count = 1` and `next_count = 0`. After it increments `next_count` and signals `x_sem`, what is its next synchronization operation?

A.`wait(next)`, suspending until the awakened process yields monitor control ✅
B.`signal(mutex)`, allowing outside entrants to run concurrently
C.`wait(x_sem)`, joining the condition queue itself
D.`signal(x_sem)` again, waking every condition waiter
💡 Difficulty: medium | ✅ Correct: A

📖 Explanation: After waking one condition waiter, the signaler executes `wait(next)`. This suspends it until the awakened process yields monitor control, preserving the signal-and-wait ordering required by this implementation inside the monitor.

Q20. Initially `x_count = 2` and `next_count = 0`. A signaler calls `x.signal()`, one waiter resumes and decrements `x_count`, then exits and wakes the signaler. After the signaler resumes and updates its count, what are the final values?

A.`x_count = 2`, `next_count = 1`
B.`x_count = 1`, `next_count = 1`
C.`x_count = 0`, `next_count = 1`
D.`x_count = 1`, `next_count = 0` ✅
💡 Difficulty: hard | ✅ Correct: D

📖 Explanation: The waiter reduces `x_count` from two to one when it resumes. Its exit signals `next`, allowing the suspended signaler to continue and decrement `next_count` from one to zero before returning.

Q21. A process calls `x.wait()` when `next_count = 1`. After incrementing `x_count`, which handoff action occurs before it blocks on `x_sem`?

A.It signals `mutex` so an outside entrant can execute first
B.It signals `next` to resume the suspended signaller ✅
C.It signals every condition semaphore in the monitor
D.It decrements `next_count` itself before waiting
💡 Difficulty: hard | ✅ Correct: B

📖 Explanation: Since `next_count` is positive, the waiting process first signals `next` to hand control to a suspended signaller. It then blocks on `x_sem` until its condition is signaled by another monitor procedure.

Q22. A programmer writes `signal(mutex)` before incrementing `x_count` in `x.wait()`. A signaler enters during the gap, sees `x_count = 0`, and skips `x.signal()`. Which error explains the blocked waiter?

A.The condition semaphore should have started at 1
B.The signaler should increment `x_count` for every condition
C.Waiter registration occurred after releasing the lock, creating a missed-notification race ✅
D.The monitor should allow the waiter to continue without any signal
💡 Difficulty: easy | ✅ Correct: C

📖 Explanation: Releasing the lock before increasing `x_count` creates a race: a signaler may inspect zero and skip the notification. Register the waiter first, then release monitor access through the coordinated protocol.

Q23. A developer implements `x.signal()` by always signaling `x_sem`, even when `x_count = 0`. What defect can follow?

A.A permit may remain stored and let a later wait pass without a matching notification ✅
B.The monitor entry semaphore becomes permanently equal to zero
C.Every condition waiter is guaranteed to execute in FIFO order
D.The signaler loses its ability to enter the monitor
💡 Difficulty: medium | ✅ Correct: A

📖 Explanation: A signal with no registered waiter should be ignored. Otherwise `x_sem` retains a permit, allowing a later process to pass `wait(x_sem)` without a corresponding condition notification while its predicate may remain false.

Q24. A monitor procedure always executes `signal(mutex)` on exit, even when `next_count > 0`. What important property can this mistake undermine?

A.The ability to maintain one condition semaphore per condition
B.The initial value required for each condition semaphore
C.The ability of a waiter to decrement its condition count
D.The priority handoff to a suspended signaller, because an outside entrant may acquire the monitor first ✅
💡 Difficulty: medium | ✅ Correct: D

📖 Explanation: The unconditional mutex signal admits an outside entrant even while a signaller waits on `next`. The exit branch must prefer `next` whenever `next_count` is positive, preserving the handoff to the resumed waiter.

Q25. A condition semaphore `x_sem` is initialized to 1 instead of 0. A thread calls `x.wait()` while its predicate is false. What is the likely defect?

A.The monitor cannot admit any external procedure
B.The first wait may consume an unused permit and continue even though no signal occurred ✅
C.The signaler becomes the only process allowed to enter
D.Every waiter on condition y is awakened
💡 Difficulty: hard | ✅ Correct: B

📖 Explanation: A condition semaphore begins at zero because no notification is available initially. Starting it at one lets the first `wait(x_sem)` consume a false permit, so the thread may continue even though its predicate is false.

Q26. A programmer decrements `x_count` inside `x.signal()` before the awakened process has resumed. Why is this incorrect?

A.The count should represent the number of signallers suspended on `next`
B.It makes the entry mutex non-binary
C.The count may understate actual condition waiters, causing a later signal to be skipped ✅
D.It forces every waiter to wake simultaneously
💡 Difficulty: hard | ✅ Correct: C

📖 Explanation: The waiter count should be decremented by the resumed waiter after `wait(x_sem)` returns, not by the signaler. Decrementing too early undercounts blocked processes and can suppress later required signals in subsequent notification decisions.

Q27. A text plot records active processes executing monitor procedures: (0,0), (1,1), (2,1), (3,2), (4,1), (5,0), where each pair is (time, active count). Which point demonstrates a mutual-exclusion violation?

A.-11
B.-21
C.-41
D.-32 ✅
💡 Difficulty: easy | ✅ Correct: D

📖 Explanation: A conventional monitor allows only one active procedure. At time three the plotted count reaches two, directly showing simultaneous execution and a failure of the entry mutual-exclusion protocol for protected code.

Q28. A trace gives event and `x_count` values: (0,0), (1,1), (2,2), (3,1), (4,1), (5,0). Which event shows one of two condition waiters resuming and decrementing the count?

A.Event 3 ✅
B.Event 1
C.Event 4
D.Event 5
💡 Difficulty: medium | ✅ Correct: A

📖 Explanation: The trace falls from two registered waiters to one at event three. That single decrement corresponds to one waiter resuming and executing the decrement after its condition semaphore wait returns.

Q29. A plot shows `next_count` over time as (0,0), (1,1), (2,1), (3,2), (4,1), (5,0). What is the maximum number of signallers suspended on `next` during the trace?

A.0
B.1
C.2 ✅
D.5
💡 Difficulty: medium | ✅ Correct: C

📖 Explanation: The highest plotted value is two, reached at events one and three. Therefore, two signallers are simultaneously suspended on `next` at the peak of this trace shown in the monitor.

Q30. A trace lists condition-signal invocations and the corresponding `x_count`: (1,2), (2,0), (3,1), (4,3), (5,1). At which invocation should `x.signal()` do nothing?

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

📖 Explanation: At invocation two the count is zero, so no waiter is registered on condition x. The implementation skips `signal(x_sem)` there, unlike the other plotted invocations in this sequence shown here.

Q31. A bounded-buffer monitor has separate conditions `notEmpty` and `notFull`. A producer inserts an item into the buffer. Which condition should it signal to enable a blocked consumer?

A.`notFull`, because insertion always frees a slot
B.`mutex`, because condition variables cannot notify consumers
C.`next`, because all consumers are suspended signalers
D.`notEmpty`, because an item is now available for consumption ✅
💡 Difficulty: easy | ✅ Correct: D

📖 Explanation: The producer releases the buffer's not-empty condition after inserting an item. A consumer waiting on that condition can then be awakened, while not-full remains the producer's separate condition for available capacity.

Q32. In a signal-and-wait monitor, a waiter has just received control from a signaller. The waiter exits while `next_count` is positive, and an external process is waiting on `mutex`. Which process should the exit code resume first?

A.The suspended signaller waiting on `next` ✅
B.The external process waiting on `mutex`
C.Every process waiting on every condition
D.The most recently created process regardless of its queue
💡 Difficulty: medium | ✅ Correct: A

📖 Explanation: The waiter receives the immediate handoff first. When it leaves, the exit code sees the suspended signaler in `next_count` and signals `next` before releasing `mutex` to outside callers waiting at monitor entry.

Q33. A monitor is adapted to signal-and-continue semantics instead of immediate handoff. The signaler continues inside the monitor after notification. What must a resumed waiter do before using shared data?

A.Assume the predicate remains true because it was signaled
B.Skip monitor locking because the signaler has already notified it
C.Reacquire monitor access and recheck its predicate in a loop ✅
D.Signal every other condition variable before proceeding
💡 Difficulty: hard | ✅ Correct: C

📖 Explanation: Under signal-and-continue semantics, the signaler retains monitor execution and the waiter only becomes eligible to run. The waiter must reacquire the lock and recheck its predicate before proceeding in a loop.

Q34. In a semaphore-based monitor, what is the distinct role of `signal(x_sem)` compared with `signal(mutex)`?

A.Both operations release the same queue of external entrants
B.`signal(x_sem)` wakes a waiter for condition x, while `signal(mutex)` admits an external monitor entrant ✅
C.`signal(x_sem)` always transfers control to a suspended signaller
D.`signal(mutex)` wakes every condition waiter at once
💡 Difficulty: hard | ✅ Correct: B

📖 Explanation: A condition signal wakes a registered condition waiter; it does not itself hand the entry permit to an external process. The `next` and `mutex` paths separately manage monitor ownership under the handoff protocol.

Q35. Initially `x_count = 1`, `next_count = 0`, and `mutex = 1`. A signaler calls `x.signal()`, the waiter resumes and exits, the signaler resumes, and its procedure then finishes. What are the final counts and entry state?

A.`x_count = 0`, `next_count = 0`, `mutex = 1` ✅
B.`x_count = 0`, `next_count = 1`, `mutex = 1`
C.`x_count = 1`, `next_count = 0`, `mutex = 0`
D.`x_count = 1`, `next_count = 1`, `mutex = 0`
💡 Difficulty: hard | ✅ Correct: A

📖 Explanation: After `x.signal`, `next_count` becomes one and the waiter runs. The waiter decrements `x_count` to zero, then signals `next` on exit; the signaller resumes, decrements `next_count`, and finally releases `mutex` for the next entrant.

Q36. A monitor has `x_count = 2` and `y_count = 1`. One process executes `x.signal()`, and exactly one x waiter resumes and completes its wait. No y operation occurs. What are the resulting condition counts?

A.`x_count = 2`, `y_count = 0`
B.`x_count = 0`, `y_count = 1`
C.`x_count = 1`, `y_count = 0`
D.`x_count = 1`, `y_count = 1` ✅
💡 Difficulty: hard | ✅ Correct: D

📖 Explanation: Only the signaled x waiter is affected. After it resumes, `x_count` decreases from two to one, while `y_count` remains one because no operation in the trace signals condition y or changes its waiter count.

🔗 Related Topics (MCQs)