📝 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.
📝 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?
📖 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?
📖 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?
📖 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?
📖 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?
📖 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?
📖 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()`?
📖 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?
📖 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?
📖 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?
📖 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?
📖 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?
📖 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?
📖 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?
📖 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?
📖 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?
📖 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?
📖 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`?
📖 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?
📖 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?
📖 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`?
📖 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?
📖 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?
📖 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?
📖 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?
📖 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?
📖 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?
📖 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?
📖 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?
📖 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?
📖 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?
📖 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?
📖 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?
📖 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)`?
📖 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?
📖 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?
📖 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.