📝 Monitors Usage in Process synchronization (36 MCQs)
Applying Monitor Constructs to Shared Resource Management
Monitor usage involves organising shared data and synchronization operations within a protected module. Applications use monitor procedures to request, modify, and release resources without directly manipulating low-level lock variables. Condition variables allow processes to wait for particular resource states, while automatic monitor mutual exclusion prevents concurrent execution of protected procedures under the classical monitor model.
Why Complex Synchronization Benefits from Monitor Encapsulation
Programs that use multiple mutexes and semaphores can become difficult to verify because resource rules are scattered across different execution paths. A monitor keeps resource state and the operations controlling it together. This reduces accidental unsynchronised access, provides a clearer programming interface, and makes resource-management conditions easier to reason about.
Monitor Entry, Condition Checks, and Resource Release
A process enters a monitor by calling one of its public procedures. The monitor grants exclusive execution, and the procedure checks the current resource condition. If the requested state is unavailable, the process waits on an associated condition variable. Another process changes the state and signals the condition, allowing the waiter to resume according to the monitor's signalling semantics.
Managing a Resource Pool with Two Waiting Processes
Consider a monitor managing a single available printer. Process P1 acquires the printer, while P2 enters a request procedure and waits because the printer is occupied. When P1 releases it, the monitor updates the availability state and signals a waiting process. P2 resumes and checks that the printer is available before acquiring it.
Safer Resource Access and Condition-Checking Requirements
Monitor-based resource management suits shared buffers, printer pools, database objects, and scheduling services. Encapsulation reduces manual lock management, while condition variables support organised waiting. However, condition signalling alone does not guarantee that a resource remains available when a process resumes. Mesa-style monitors therefore require waiters to recheck their conditions using loops.
Misconceptions About Monitor Resource Management
'A monitor automatically grants every waiting process its requested resource' is incorrect because the process must recheck availability. 'A process can execute several monitor procedures simultaneously' is incorrect under classical mutual exclusion. 'Condition signalling always provides fairness' is incorrect because scheduling and waiter-selection policies determine which process resumes.
Exam Focus: Tracing Monitor-Based Resource Allocation
Trace monitor entry, resource-state checks, condition-variable waiting, state updates, and signalling. Determine which process owns a resource before and after each operation. Questions may involve printer pools, bounded buffers, shared counters, and Mesa-style condition loops, or ask how monitor encapsulation differs from separate semaphore operations.
Key Terms of Monitor Usage
- Monitor procedure - Operation accessing protected monitor state.
- Monitor entry - Admission into the protected construct.
- Condition check - Evaluation of resource availability.
- Waiter - Process suspended on a condition.
- Signalling - Notification of eligible waiters.
- Resource pool - Collection of managed resource instances.
📝 All Monitors Usage in Process synchronization MCQs
Q1. A shared inventory service defines its balance and update procedures inside a classical monitor. Two clients call different public procedures at the same time. What does the monitor guarantee?
📖 Explanation: A classical monitor provides mutual exclusion across its procedures: while one thread executes inside, another must wait at the monitor entry. This guarantee applies to monitor execution, not unrelated code.
Q2. A thread calls signal on condition variable resourceReady when no thread is waiting on that condition. What happens to the notification?
📖 Explanation: A condition variable records waiting threads, not a stored notification count. If signal occurs with no waiter, it has no later effect; a future waiter still blocks until signaled again.
Q3. A file-processing service protects its job queue with a monitor. A method removes a job and then performs a slow network upload before returning. Other threads cannot enqueue jobs during the upload. What is the best improvement?
📖 Explanation: The upload is external and may take seconds, while queue removal requires only a brief protected state change. Reserve or remove the job under the monitor, then upload after leaving it.
Q4. A consumer enters a monitor and discovers that its queue is empty. Which operation lets it sleep while allowing a producer to enter and add an item safely?
📖 Explanation: The condition wait must atomically place the consumer on the condition queue and release the monitor. A producer can then enter, add an item, and signal without a lost-wakeup gap.
Q5. Which statement correctly distinguishes a condition variable from a counting semaphore?
📖 Explanation: Unlike a semaphore, a condition variable has no persistent count. Its wait operation always blocks, while signal with an empty waiter queue is discarded rather than saved for a later caller.
Q6. A bank monitor has a condition variable for withdrawals waiting until the balance is sufficient. A deposit signals a waiting withdrawal. Why must the withdrawal test the balance again after waking?
📖 Explanation: Under signal-and-continue semantics, the depositing thread may continue running after signaling. Other withdrawals can change the balance before the waiting thread resumes, so the withdrawing method must verify the predicate again under the monitor lock.
Q7. A monitor manages five connections. Two clients each acquire two connections, and a third client requests two connections as one atomic operation. If the first allocations remain active, what should happen to the third request?
📖 Explanation: Two acquisitions consume four connections, leaving . A request for two cannot be satisfied; an atomic pool operation should wait without allocating one connection and holding it while awaiting another.
Q8. A cache monitor exposes one method to check whether a key exists and another to insert it if absent. Two clients call these methods separately and both initially observe that the key is absent. What is the safest correction?
📖 Explanation: Each method is mutually exclusive only while it runs. Between the check and insert calls, another thread can insert the same key, so the whole check-then-act operation must be one monitor procedure.
Q9. A consumer checks that a queue is empty, releases the monitor lock, and only afterward registers itself as waiting. A producer can insert and signal during this gap. What failure can result?
📖 Explanation: The producer may signal after the consumer releases the lock but before the consumer is actually queued. That notification is lost, leaving the consumer asleep despite a nonempty queue; wait registration and lock release must be atomic.
Q10. A monitor releases a resource by increasing availableCount. Which order should the method follow to wake a thread waiting for available resources?
📖 Explanation: The producer must first increment the available count while holding the monitor, then signal the condition. Otherwise the waiter may wake, see the old zero count, and immediately block again.
Q11. A Mesa-style monitor contains this logic: if (available == 0) wait(resourceReady); acquireResource(); A reviewer says it is correct because signal wakes a waiter. What is the flaw?
📖 Explanation: The flawed if check assumes the condition stays true after notification. Under Mesa semantics the notifier continues, so another thread may consume the resource first; use while and recheck after reacquiring the monitor.
Q12. Under Hoare-style monitor semantics, what happens immediately after a thread signals a condition that has a waiting process?
📖 Explanation: Hoare-style signal-and-wait semantics immediately transfer monitor execution to the signaled waiter. The signaling process is suspended until the waiter leaves or waits, so the condition is guaranteed at handoff time.
Q13. A producer signals notEmpty after adding an item, but continues executing inside a Mesa-style monitor. Before the consumer resumes, another consumer may remove the item. What must the awakened consumer do?
📖 Explanation: In Mesa-style signaling, the producer retains the monitor after signaling. It may change the state again before the consumer resumes, so the consumer must test the availability predicate again before proceeding.
Q14. A queue monitor records occupancy after each event as (time, items): (0,0); (1,2); (2,1); (3,3); (4,2); (5,4). At which recorded time is the queue fullest?
📖 Explanation: The listed occupancies are 0, 2, 1, 3, 2, and 4. The maximum is 4 at time 5, so the graph indicates the queue is fullest at the final recorded point.
Q15. A resource monitor has two waiters: one needs at least one unit, while another needs at least four. The available count rises from zero to one, and signal wakes one arbitrary waiter. Which design best avoids leaving the eligible waiter asleep because the other waiter was selected?
📖 Explanation: The single signal may wake the waiter needing four units, whose predicate is still false, while the eligible one-unit waiter remains asleep. Separate condition queues or broadcast plus predicate loops prevent this selection mismatch.
Q16. A monitor correctly prevents concurrent execution of its procedures, but one client can remain blocked indefinitely while newer clients repeatedly receive service. Which property is not guaranteed by mutual exclusion alone?
📖 Explanation: Automatic mutual exclusion prevents simultaneous execution of monitor procedures, but it does not choose a fair admission order. A correct monitor can therefore still starve a client under an unfair scheduling policy.
Q17. A connection pool contains six connections. One is already in use, and a client atomically acquires three more. Assuming no other requests intervene, how many connections remain available?
📖 Explanation: After one is already in use, five connections remain free. Acquiring three changes availability to . The monitor must perform the count update and ownership assignment as one protected operation.
Q18. A monitor's available-resource count is recorded as (time, free units): (0,0); (1,1); (2,2); (3,1); (4,3); (5,2). A waiting client requires three units atomically. At which earliest recorded time can it proceed?
📖 Explanation: A request for three can first be satisfied at time 4, when the recorded count reaches 3. Earlier points show fewer than three free resources, so the waiter must continue waiting.
Q19. A monitor manages a pool of connections. A client needs three connections, but only two are free. Which allocation policy avoids making this client hold one connection while waiting for the others?
📖 Explanation: The pool should grant all requested resources together or none. Waiting without a partial allocation avoids making the requester hold one connection while waiting indefinitely for the remaining connections resources.
Q20. A monitor-based queue contains one job and has four waiting consumers. The producer broadcasts on notEmpty. What happens when the consumers resume and compete for the monitor?
📖 Explanation: Broadcast wakes all eligible consumers, but the monitor lock still serializes removal. The first consumer takes the sole job; others recheck the empty-queue predicate and return to waiting inside the monitor.
Q21. A monitor's release method increases the available-resource count but never signals the condition variable used by blocked clients. What may happen even though the resource is now free?
📖 Explanation: A condition variable does not watch shared state or wake threads automatically. After increasing availability, the release method must signal an appropriate waiter; otherwise existing waiters may remain blocked indefinitely.
Q22. A reader obtains several account fields through separate monitor calls. A writer updates all fields in one call, and the reader sees a mixture of old and new values. What explains this result?
📖 Explanation: Separate protected calls do not create an atomic snapshot across the entire read. The update can occur between calls, producing a combination of values from different versions; provide one snapshot operation.
Q23. A monitor method reserves a printer and then waits for a remote approval service while keeping the monitor locked. Other clients cannot use unrelated printers. Which design is usually preferable when the operation's invariants permit it?
📖 Explanation: The network call can block for a long time while unrelated clients need the monitor. Record a reservation under protection, release the monitor for external work, then reacquire and validate the result.
Q24. A monitor method owns a non-reentrant lock and calls another public method that tries to acquire that same lock again. What is the likely result?
📖 Explanation: The outer method already owns the non-reentrant lock. Reacquiring it in the nested call blocks the same thread on its own lock; use an internal helper that assumes monitor ownership or avoid reacquisition.
Q25. A monitor implementation releases its lock only along the normal return path. An exception occurs before that statement, and later callers block forever. Which correction addresses the defect?
📖 Explanation: The flawed implementation releases the lock only on normal return. An exception bypasses that statement, leaving every later caller blocked; use guaranteed cleanup such as a finally block or scoped locking.
Q26. A job monitor tracks queued jobs and active jobs. Its trace is (event, queued, active): (0,4,0); (1,3,1); (2,2,2); (3,3,1); (4,2,2); (5,1,3). How many jobs are outstanding at event 5?
📖 Explanation: At event 5, one task remains queued and three are active, giving outstanding tasks. The state accounting must include both queued and in-progress jobs to preserve the service invariant.
Q27. A monitor removes a job from its queue and then records the job identifier in an active-job set. If the second update fails, what is the most important design requirement?
📖 Explanation: Removing a job before recording its active ownership can make it disappear from both visible states if the second update fails. Make the transition atomic or define compensating recovery for consistency.
Q28. A monitor protects a deposit(amount) procedure. The method is synchronized correctly, but its contract allows only positive deposits. A caller supplies -20. What should the monitor do?
📖 Explanation: The lock prevents interleaving, not invalid requests. The monitor must enforce its method contract by rejecting a negative amount before changing the balance, preserving both synchronization and application invariants consistently.
Q29. A resource monitor offers isFree(resource) and acquire(resource) as separate calls. A client checks that a printer is free, but another client acquires it before the first client calls acquire. What is the best fix?
📖 Explanation: A true availability result can become stale as soon as the method returns. Another caller may claim the resource first; combine checking and claiming in one monitor procedure to make ownership atomic.
Q30. A monitor wakes its oldest waiter first, but does not reserve the resource for that waiter. New arrivals repeatedly enter first and acquire the resource. What does this reveal?
📖 Explanation: Selecting the oldest waiter for notification does not reserve the resource. A new arrival may enter first and repeatedly win the race, so fairness requires an admission policy that prevents overtaking.
Q31. A service records (time, newer requests completed while the oldest waiter remains blocked): (0,0); (1,2); (2,4); (3,6); (4,8); (5,10). What does this pattern indicate?
📖 Explanation: Newer work increases from zero to ten completed requests while the original waiter receives no service. This pattern shows repeated bypass, so a fair queue must prevent newcomers from overtaking indefinitely.
Q32. A connection pool has two free connections. Its wait queue contains W1 requesting three connections, followed by W2 and W3 each requesting one. If the monitor enforces strict FIFO, what trade-off appears?
📖 Explanation: Strict FIFO leaves two free connections idle because the first request needs three; allowing later one-unit requests to pass improves utilization but can starve the large request without a fairness rule.
Q33. A banking service uses one global monitor for unrelated accounts, so a slow operation on account A blocks updates to account B. Which redesign can improve concurrency without abandoning protection?
📖 Explanation: A global monitor serializes unrelated account operations. Separate protection allows independent accounts to proceed concurrently, but transfers touching multiple accounts need a consistent lock order to avoid deadlock between transactions.
Q34. A service never corrupts shared data, but some clients wait indefinitely while others continue receiving service. Which conclusion is most accurate?
📖 Explanation: Correct mutual exclusion establishes a safety property by preventing conflicting concurrent access. It does not ensure every waiting client eventually runs; admission and scheduler policies determine liveness and starvation risk.
Q35. A logging thread reads monitor-protected fields without entering the monitor. It sometimes reports combinations that never existed together. What is the best correction?
📖 Explanation: The logger ignores the monitor's synchronization boundary, so it can read fields on opposite sides of an update. Expose a protected snapshot method or apply the same locking discipline to logging.
Q36. A monitor returns a direct reference to its internal mutable resource table. A client changes the table without calling any monitor procedure. What design principle was lost?
📖 Explanation: Returning a direct reference to internal mutable state lets callers change it without executing monitor procedures. Return an immutable copy or expose controlled operations so every change preserves the monitor's invariants.