π Monitors in Process synchronization (36 MCQs)
High-Level Synchronization Through Monitors
A monitor is a high-level synchronization construct that encapsulates shared data, the procedures operating on that data, and condition variables used for coordination. It belongs to structured process synchronization and provides automatic mutual exclusion between concurrent monitor operations. Only one process or thread is permitted to execute inside a monitor at a time under the classical monitor model.
Why Lock Management Needs Structured Encapsulation
Using separate semaphores throughout a large program makes correct operation ordering difficult to maintain. Programmers may forget to release locks or signal waiting processes. Monitors address this complexity by combining protected data and synchronization operations within one abstraction, reducing direct manipulation of low-level locking protocols and improving modularity.
Monitor Procedures, Mutual Exclusion, and Condition Variables
A process calls a monitor procedure to access protected data. The monitor automatically enforces mutual exclusion. When a required condition is false, the process executes wait on a condition variable and temporarily leaves the monitor. Another process can enter, update the state, and signal the waiting condition when appropriate.
Waiting for an Available Resource Inside a Monitor
Suppose a monitor manages a resource with three available units. A process requesting one unit checks the available count inside a monitor procedure. If the count is zero, it waits on a condition variable. When another process releases a unit and signals the condition, the waiting process resumes and checks availability before proceeding.
Encapsulation, Mutual Exclusion, and Condition Waiting Costs
Monitors simplify synchronization in concurrent applications by centralising shared-state protection and coordination. They are useful for bounded buffers, resource managers, and producer-consumer systems. However, condition-variable semantics differ between implementations, and poorly designed procedures can still cause deadlocks or starvation. Monitor availability also depends on programming-language or runtime support.
Misconceptions About Monitor Execution
'A monitor allows every procedure to execute concurrently' is incorrect because classical monitors provide mutual exclusion. 'Calling signal always transfers control immediately' is incorrect because signal semantics depend on the monitor model. 'Condition variables permanently store notification counts' is incorrect because they coordinate waiting conditions rather than functioning as counting semaphores.
Exam Focus: Monitor Entry and Condition-Variable Behaviour
Identify monitor shared data, procedures, and condition variables in a synchronization design. Trace wait and signal operations and determine which process currently occupies the monitor. Compare monitor synchronization with explicit semaphore locking and distinguish Hoare-style from Mesa-style condition signalling, especially when a waiting process must recheck its condition.
Key Terms of Monitor Synchronization
- Monitor - Encapsulated mutual-exclusion construct.
- Condition variable - Mechanism for conditional waiting.
- wait() - Suspends execution pending a condition.
- signal() - Notifies a condition waiter.
- Mutual exclusion - Allows one monitor occupant.
- Encapsulation - Combines data and operations.
π All Monitors in Process synchronization MCQs
Q1. A concurrent program uses a monitor to manage a shared inventory. Which combination best describes the monitor abstraction?
π Explanation: A monitor groups the shared state with the procedures that manipulate it and condition variables used for coordination. This structure gives callers a controlled interface instead of direct access by design.
Q2. A monitor protects a linked list, but its design allows application code to modify the internal head pointer directly. Which monitor principle has been broken?
π Explanation: Encapsulation requires callers to use the monitor interface rather than modifying internal state directly. Bypassing that interface also bypasses its mutual-exclusion discipline and can corrupt shared invariants during concurrent execution.
Q3. Two threads simultaneously call a monitor procedure that increments a shared counter using a read, add, and write sequence. What prevents a lost update in the classical monitor model?
π Explanation: The monitor provides mutual exclusion for its procedures, so the two read-modify-write sequences cannot interleave. Both increments are therefore reflected in the shared counter when each call completes under the monitor lock.
Q4. Thread A is executing inside monitor M when thread B calls another procedure of M. What normally happens to B in a classical monitor?
π Explanation: A classical monitor permits one active thread inside it. A competing caller waits in the monitor entry queue until the current occupant exits or blocks on a condition during that interval.
Q5. A consumer finds a queue empty inside a monitor and calls wait(notEmpty). Which action is essential for the producer to enter and insert an item?
π Explanation: A condition wait suspends its caller and releases the monitor, allowing another procedure to enter and change the shared state. The waiting thread reacquires the monitor before continuing and releasing its ownership.
Q6. A programmer releases the monitor lock and only afterward registers a thread as waiting on condition spaceAvailable. What race can occur during this gap?
π Explanation: If the state change and notification occur before waiter registration, the notification can be lost and the thread may sleep unnecessarily. An atomic release-and-wait operation closes this gap during the transition.
Q7. After a condition-variable wait is signaled, what must happen before the waiting thread safely resumes a monitor procedure?
π Explanation: The awakened thread must regain the monitor lock before returning to protected code. This restores mutual exclusion for inspecting shared state, but it does not prove that the waited-for condition is still true.
Q8. A programmer treats condition variable ready as an integer permit count and expects each signal to be saved until a future wait. What is the fundamental error?
π Explanation: Condition variables coordinate threads that are currently waiting; they do not retain signal credits like counting semaphores. The actual predicate must be stored separately in protected shared data rather than by notification.
Q9. A monitor calls signal(dataReady) when no thread is waiting on dataReady. A thread begins waiting afterward. What should it expect?
π Explanation: A condition-variable signal issued with no waiter is normally discarded. A later thread does not inherit that notification, so it must inspect the shared predicate before deciding whether to wait.
Q10. Four threads are waiting on the same condition variable. A monitor procedure invokes signal once. Without assuming a special policy, what is the expected effect?
π Explanation: A single signal normally selects one waiter, if one exists. It does not wake the entire waiting group, create resource units, or guarantee that the selected waiter will find its predicate true.
Q11. A resource manager has six waiting clients. Its state changes so that several clients may proceed, and it calls broadcast on their condition variable. What does broadcast mean?
π Explanation: Broadcast makes all waiters on the selected condition variable eligible to resume. They still compete to reacquire the monitor and must test the resource predicate before claiming anything when they reacquire it.
Q12. A waiter wakes after signal but finds that the shared predicate is still false. Which design correctly represents the role of a condition variable?
π Explanation: A condition variable reports that a state change may matter; it does not represent the state itself. The monitor must maintain the predicate in shared data and test it before proceeding.
Q13. Under Mesa-style monitor semantics, thread P signals waiting thread Q while both are associated with the same monitor. What happens immediately after the signal?
π Explanation: Mesa signaling uses signal-and-continue behavior: the signaling thread retains the monitor until it leaves or waits. The awakened thread becomes runnable but must compete to reacquire the monitor after the signaling call.
Q14. A Mesa-style monitor contains the statement if queueEmpty then wait(notEmpty). Why is this unsafe for a consumer?
π Explanation: Under Mesa semantics, another consumer may remove the available item before the awakened thread runs. A while loop rechecks the queue predicate after reacquiring the monitor and prevents an invalid removal.
Q15. In a Hoare-style monitor, a signaler wakes a thread waiting on condition c. Which scheduling action defines the classical signal-and-wait behavior?
π Explanation: Hoare-style semantics transfer monitor control immediately to the awakened waiter. The signaler is suspended temporarily, allowing the waiter to rely on the condition established at the signaling point before the signaler continues.
Q16. A monitor uses Hoare-style signal-and-wait semantics. Immediately after a waiting thread resumes because of signal, what can it infer about the condition?
π Explanation: Under Hoare semantics, the signaled thread runs immediately after the transfer, before another monitor procedure can change the protected state. The condition established by the signaler therefore holds at handoff.
Q17. A team changes a monitor from Hoare-style to Mesa-style signaling but leaves a condition check as a single if statement. Which change is most important?
π Explanation: Mesa signaling does not immediately transfer control, so intervening threads can invalidate the predicate. A while loop makes the waiter verify the condition again each time it reacquires the monitor.
Q18. A thread is blocked because another thread is currently executing a monitor procedure. A different thread is blocked after calling wait on condition hasItem. Where are these threads waiting?
π Explanation: A caller blocked by monitor occupancy waits for entry, while a caller that executes condition wait joins that condition variable queue. These queues represent different reasons for being outside the monitor.
Q19. A trace records the following events: t0, A enters monitor M; t1, B requests M; t2, A calls wait(c); t3, B enters M. What explains B entering at t3?
π Explanation: A condition wait blocks A and releases the monitor, so the entry queue can admit B. The trace preserves mutual exclusion; A is waiting on condition c rather than occupying M.
Q20. A monitor manages three identical devices. Its available count becomes three, and it broadcasts to seven waiting threads. Assuming no other device changes, how many threads can successfully reserve a device?
π Explanation: Broadcast wakes all seven waiters but creates no devices. At most three can decrement the available count from three to zero; the remaining four must observe the false predicate and wait again.
Q21. A monitor uses one condition variable for two predicates: printerAvailable and scannerAvailable. A signal wakes a thread waiting for the scanner even though only the printer became available. What problem can result?
π Explanation: A shared condition queue can wake a thread whose predicate was not satisfied by the state change. Separate condition variables, or a carefully chosen broadcast, can avoid leaving an eligible waiter asleep.
Q22. A monitor increments availableUnits and then calls signal(unitsAvailable). Why is updating the shared count before signaling a sound ordering?
π Explanation: The notification should follow the state transition it announces. Updating the count first lets a waiter observe the intended state after reacquiring the monitor, while predicate checking remains essential under the same lock.
Q23. A monitor has two waiters for a resource. The state table is: t0 available=0, waiters=2; t1 release changes available=1; t2 signal wakes one; t3 that waiter reserves the unit. What is the state at t3?
π Explanation: The release raises availability to one, signal makes one waiter eligible, and that waiter reserves the unit at t3. The count returns to zero, while the other waiter remains blocked.
Q24. A lock-dependency graph contains edges T1 holds M1 and requests M2; T2 holds M2 and requests M1. What does the cycle establish under exclusive monitor locks?
π Explanation: T1 cannot proceed until T2 releases M2, while T2 cannot proceed until T1 releases M1. The cycle creates circular wait and can leave both threads permanently blocked without external intervention.
Q25. A developer writes the following sequence: check predicate while holding M; unlock M; register on condition c; sleep. Another thread may change the predicate and signal between the unlock and registration. What is the precise failure?
π Explanation: The sequence has a lost-wakeup window because the waiter is not yet registered when the signal occurs. A correct condition wait atomically releases the monitor and blocks as one operation.
Q26. A monitor offers getCount() and removeItems(n) as separate procedures. A client calls getCount(), then removeItems(n) using the returned value. Why is the whole client sequence not necessarily atomic?
π Explanation: Mutual exclusion protects each monitor procedure, not a caller sequence spanning multiple returns and reentries. Another client can change the collection between getCount and removeItems, invalidating the earlier result unless combined into one procedure.
Q27. A monitor returns a pointer to one of its internal mutable records. The caller modifies that record after the method returns, while another thread invokes a monitor update. What is the central flaw?
π Explanation: The pointer exposes internal mutable state beyond the monitor boundary. Later caller writes are not serialized with monitor procedures, so shared invariants can be violated despite correct locking inside the monitor.
Q28. A monitor procedure calls an external helper that changes the monitorβs shared counter, but the helper does not acquire the monitor. What conclusion follows?
π Explanation: The monitor only serializes accesses that follow its synchronization discipline. An external helper that modifies the same counter without protection can interleave with monitor calls and invalidate its invariants unless it follows that discipline.
Q29. A monitor validates an order and updates inventory, then performs a slow network audit before returning. The audit need not be atomic with the inventory update. What is the best design?
π Explanation: The validation and inventory change belong in the protected operation, but an independent slow audit need not extend the critical section. Moving it outside reduces contention while requiring defined audit-failure handling.
Q30. A monitor procedure changes shared state and signals a waiter. An exception then bypasses the monitor-exit code, leaving its lock held. Which consequence is most serious?
π Explanation: If exceptional control flow fails to release the monitor, no other caller can enter to inspect or repair the state. Reliable cleanup must release the lock on every exit path.
Q31. A monitor correctly protects a resource pool, but the scheduler repeatedly ignores one thread after it has been awakened. Which statement is most accurate?
π Explanation: Monitor mutual exclusion protects shared-state safety, but does not by itself guarantee CPU time or fair admission. A scheduler can indefinitely postpone a ready waiter without violating mutual exclusion under this scheduling policy.
Q32. A classical monitor serializes every procedure, including a procedure that only reads a shared value. What is the direct consequence of this rule?
π Explanation: The classical single-occupant rule applies to monitor procedures regardless of whether they read or write. This simplifies reasoning but may reduce concurrency compared with carefully designed reader-writer access within the protected abstraction.
Q33. A pool monitor starts with five available units. Three waiters are awakened by broadcast; the first two reserve one unit each, and the third has not reacquired the monitor yet. What availability will the third waiter observe if no release occurs?
π Explanation: The pool begins with five units and two successful reservations consume two, leaving three. Broadcast changes waiter eligibility only; it does not alter the resource count or reserve units for all awakened threads.
Q34. Consider this Mesa-style trace: A holds M and signals B; A then changes predicate P from true to false and exits; B later reacquires M. What must B do before using the resource guarded by P?
π Explanation: The trace shows the signaler continuing after notification and changing P before B runs. Mesa waiters must test the predicate after reacquiring M and return to waiting when it is false.
Q35. A condition variable c has no waiters when signal(c) occurs. A semaphore s has value zero when signal(s) occurs. A later thread calls wait on each object. Which comparison is correct?
π Explanation: Condition-variable signals are not remembered when no waiter exists. A semaphore signal increments its count, so a later wait can consume that stored permit; this distinguishes their coordination semantics for a future waiter.
Q36. A monitor manages three identical servers. At time t0, available=0 and clients P and Q wait on hasServer. At t1, one server is released and broadcast occurs. P reacquires first and reserves it; Q reacquires afterward. Which outcome is correct?
π Explanation: Broadcast wakes both clients, but only one server was released. P reserves it under the monitor lock, so Q must observe zero availability and wait again rather than claim the same server.