📝 The Readers Writers Problem in Process Synchronization (36 MCQs)
Concurrent Reading and Exclusive Writing of Shared Data
The readers-writers problem concerns synchronization when multiple processes or threads share a data object that supports both reading and writing. Multiple readers may access the data simultaneously because they do not modify it, but a writer requires exclusive access to prevent inconsistent updates. Different readers-writers algorithms establish priorities between readers and writers to control access and waiting behaviour.
Why Reading Can Be Shared but Writing Requires Exclusion
Allowing every access to occur under exclusive locking prevents conflicting updates but unnecessarily restricts simultaneous reading. Conversely, permitting reading and writing together can expose incomplete or inconsistent data. The synchronization design must therefore allow compatible read operations to overlap while preventing writers from modifying the object during active reads or competing writes.
Reader Counts, Writer Locks, and Admission Policies
A common semaphore solution maintains a reader count protected by a mutex. The first reader acquires a shared resource semaphore, preventing writers from entering; subsequent readers join without repeating that acquisition. When the last reader exits, it releases the resource semaphore. Writers acquire the resource semaphore exclusively before updating the shared data.
Three Readers Competing with One Writer
Suppose readers R1, R2, and R3 access a shared database record while writer W requests an update. The readers may proceed together if the policy permits concurrent reading. W must wait until the active readers finish. After the final reader releases the resource, W obtains exclusive access and completes its modification.
Reader Throughput, Writer Starvation, and Fairness
Readers-writers synchronization improves throughput for read-intensive workloads such as databases and shared configuration services. Reader-preference solutions may starve writers when new readers continually arrive. Writer-preference policies reduce writer delays but may postpone readers. Fair solutions attempt to balance both groups, although queue management and workload patterns influence actual performance.
Misconceptions About Reader and Writer Access
'Readers always require exclusive access' is incorrect because compatible readers can share access. 'A writer can safely modify data while readers are active' is incorrect because readers may observe inconsistent state. 'Every readers-writers solution guarantees equal waiting time' is incorrect because fairness depends on the admission policy.
Exam Focus: Comparing Readers-Writers Synchronization Policies
Trace reader-count changes and identify when the first reader acquires and the last reader releases the resource lock. Compare reader-preference, writer-preference, and fair approaches. Questions may examine simultaneous reading, exclusive writing, semaphore ordering, starvation, and how continuous arrivals from one group affect waiting in the other group.
Key Terms of the Readers-Writers Problem
- Reader - Process accessing data without modification.
- Writer - Process modifying shared data.
- Reader count - Number of active readers.
- Writer preference - Policy prioritising waiting writers.
- Reader preference - Policy favouring reader admission.
- Fairness - Balanced opportunity for access.
📝 All The Readers Writers Problem in Process Synchronization MCQs
Q1. A shared configuration database serves many queries and occasional updates. Which access rule preserves correctness while allowing compatible work to overlap?
📖 Explanation: Read-only operations can safely overlap, but a modification must be isolated from every other access to preserve a consistent state. The rule permits reader concurrency while making writer access exclusive.
Q2. Two analytics threads read the same immutable table while no update is running. Why can a correct readers-writers design admit both at once?
📖 Explanation: Both operations only inspect unchanged data, so their execution does not create conflicting modifications. The synchronization rule can therefore share the read mode without weakening the exclusion required for writers.
Q3. A shared inventory service has two update requests. What must be true while the first writer holds the protected resource?
📖 Explanation: A writer must have exclusive access for the entire protected update. Preemption does not release ownership, and splitting the update into fields does not authorize overlapping access unless those fields use independent protection.
Q4. A financial record stores balance and transaction_count. A reader sees the old balance but the new transaction_count during an update. What has gone wrong?
📖 Explanation: The two fields form one logical state, so observing them across different stages of a write can violate the application invariant. Exclusive writer access must prevent readers from seeing intermediate combinations.
Q5. A monitoring operation reads a database row but also increments a shared global statistics counter. How should its synchronization needs be classified?
📖 Explanation: Although the row access is read-only, incrementing a shared counter modifies shared state. Concurrent increments can be lost unless protected, so the operation needs write-style coordination for that counter to remain correct.
Q6. A writer owns an exclusive lock and is descheduled for 8 milliseconds. A reader arrives during that interval. What is the correct action?
📖 Explanation: Lock ownership persists while a task is descheduled. Allowing a reader into the protected object would overlap with the unfinished write, so the reader must wait for an explicit release.
Q7. In a reader-preference semaphore solution, read_count starts at zero. Reader R1 enters alone. Which sequence is required?
📖 Explanation: The first reader closes the resource to writers by acquiring its semaphore. Later readers join the active group without acquiring it again, while the count records how many readers must finish before release.
Q8. A reader-count mutex protects read_count. R1 and R2 both enter, making the count two. R1 exits first. What should the protocol do?
📖 Explanation: R2 remains active after R1 exits, so a writer must still be excluded. The count becomes one, and only the transition from one active reader to zero releases the shared resource semaphore.
Q9. In a correct first-reader/last-reader protocol, read_count is three. Two readers finish one after another. What is the count after both exits, and is the resource available to a writer?
📖 Explanation: Each completed reader decrements the protected count once. Starting from three, two exits leave one active reader, so the last-reader condition has not occurred and the writer must continue waiting.
Q10. A student releases the database semaphore whenever any reader exits. Which correction fixes the protocol?
📖 Explanation: The flawed step releases the resource while other readers may still be using it. The correct condition is the last reader transition, when the protected count falls from one to zero.
Q11. The following observations are recorded: time 0 has 2 active readers; time 1 has 3; time 2 has 2; time 3 has 1; time 4 has 0; time 5 has 0. A writer has waited since time 1. At what earliest listed time can it acquire the resource, assuming no other request wins first?
📖 Explanation: The writer needs the active-reader count to reach zero. The listed counts remain positive through time 3 and first become zero at time 4, which is the earliest point when reader activity no longer blocks it.
Q12. Two readers update read_count without holding its protecting mutex. Both read the value 4, each computes 5, and each stores 5. What is the resulting defect?
📖 Explanation: Both readers use the same stale value and store the same incremented result. One increment is lost, so the count underreports active readers and can cause premature release of the resource semaphore.
Q13. Which policy best matches the first readers-writers problem?
📖 Explanation: Reader preference allows new readers to join an existing reader group rather than waiting merely because a writer is queued. This improves reader access but can keep writers waiting for a long time.
Q14. A library service uses writer preference: as soon as a writer waits, new readers are stopped from entering. Which risk does this policy reduce most directly?
📖 Explanation: Closing reader admission when a writer queues prevents later readers from extending the active group indefinitely. Existing readers finish, after which the writer can obtain exclusive access, reducing writer starvation risk.
Q15. A reader-preference database admits new readers whenever at least one reader is active. Readers keep arriving so the active count never reaches zero. What can happen to a waiting writer?
📖 Explanation: The writer needs the reader group to drain completely. Continuous reader admission can prevent the count from reaching zero, so the writer may wait without bound while readers continue completing work.
Q16. A strict writer-preference policy repeatedly serves waiting writers before admitting new readers. If writers continue to arrive, what risk may readers face?
📖 Explanation: Writer preference protects waiting writers by delaying new readers, but sustained writer arrivals can repeatedly postpone reader admission. Mutual exclusion may remain correct even while readers experience starvation under sustained arrivals.
Q17. A trace shows readers completing continuously while one writer remains blocked and no writer owns the resource. How should this situation be classified?
📖 Explanation: Progress by readers shows that the system is not globally stuck. The writer is being bypassed indefinitely by the admission policy, which is starvation rather than proof of deadlock by itself.
Q18. A design intends to give writers priority, but it checks waiting_writers and then admits a reader without protecting the check and admission as one operation. What is the main flaw?
📖 Explanation: The separate check and admission create a race window. A writer can queue after the reader sees no waiting writers, yet before it enters, defeating the intended priority rule unless admission is coordinated atomically.
Q19. An application changes from read-heavy traffic to frequent writes. Which conclusion is most justified before changing its lock policy?
📖 Explanation: A policy that performed well under read-heavy traffic may delay writers under a changed workload. Measuring both throughput and wait times identifies whether a writer-aware or fair policy is warranted.
Q20. A system claims that its readers-writers lock is fair but provides no queueing rule or starvation guarantee. Which statement should be treated cautiously?
📖 Explanation: Safety properties do not automatically establish fairness. Without a specified admission or queueing guarantee, a lock may favor one class under load, so eventual service for every waiter cannot be assumed.
Q21. A fair turnstile is used by both reader and writer arrivals. What is its main role?
📖 Explanation: A common admission point can preserve arrival order or a defined queueing rule. It limits overtaking by later requests, while a separate resource mechanism still enforces concurrent-reader and exclusive-writer access.
Q22. A fair queue contains R1, then W1, then R2 and R3. R1 finishes. Under FIFO admission with consecutive readers allowed as a group, who should be admitted next?
📖 Explanation: FIFO admission honors the earliest waiting request. Since W1 is ahead of R2 and R3, it receives the next exclusive turn after R1 finishes; later readers cannot overtake it in the queue.
Q23. A reader releases its shared lock and immediately requests it again. A writer is already queued in a fair system. What should determine whether the reader reenters?
📖 Explanation: Fairness depends on the lock's admission rule, not on a requester's previous ownership. A new read request should not automatically jump ahead of a writer already waiting in the queue.
Q24. A request timeline is recorded as follows: time 0, R1 enters; time 1, W1 queues; time 2, R2 queues; time 3, R3 queues; time 4, R1 exits. Under a fair FIFO policy, who is next?
📖 Explanation: W1 entered the waiting queue before R2 and R3. A fair FIFO policy preserves that order, so once R1 releases the resource, W1 is next rather than a later reader.
Q25. A writer-priority implementation closes a reader gate when a writer begins waiting. Why must the gate be reopened after the relevant writers finish?
📖 Explanation: The gate should reflect the presence of waiting writers. Leaving it closed after the writer queue empties can block readers despite an available resource, producing unnecessary delay or a liveness defect.
Q26. The measured results for a proposed policy are: read throughput 900 operations/s; writer throughput 2 operations/s; maximum writer wait 40 seconds; occasional writer wait above 2 minutes. Which assessment is strongest?
📖 Explanation: High read throughput does not establish balanced service. Very long and growing writer waits may reveal reader bias, so fairness and tail latency should be assessed alongside aggregate throughput during sustained operation.
Q27. A service admits consecutive readers together but stops adding later readers when a writer reaches the front of the queue. Why is this useful?
📖 Explanation: Grouping readers that are already ahead in the queue retains concurrency among compatible operations. Stopping later readers from extending the group lets the writer eventually receive its exclusive turn under ongoing arrivals.
Q28. A student updates read_count outside its mutex. Two readers each observe 6, add one, and store 7. Which correction is required?
📖 Explanation: The flawed step is the unprotected read-modify-write sequence, which loses one increment. The correct step is to hold the count mutex across reading, changing, and storing the shared value by every reader.
Q29. A reader-priority implementation allows every exiting reader to signal the resource semaphore. What is the likely consequence when three readers are active?
📖 Explanation: The flawed step is signaling on every reader exit. The correct step is to release the resource only when the protected count reaches zero, ensuring no reader remains inside when a writer enters.
Q30. A writer checks that no readers are active, then pauses before acquiring exclusive access. A reader enters during the pause. Which design defect does this expose?
📖 Explanation: The flawed sequence separates checking availability from claiming the resource, allowing a reader to enter in between. The check and acquisition must be coordinated by the synchronization protocol to prevent overlapping access.
Q31. A read-mostly service records these points: reader arrival rate and mean writer wait are (1, 2), (2, 4), (3, 7), (4, 12), (5, 20), and (6, 31), where rate is requests per second and wait is seconds. What trend is shown?
📖 Explanation: The wait values rise from 2 to 31 seconds as the arrival rate increases, with larger increments at higher rates. This pattern is consistent with growing writer delay under a reader-biased admission policy.
Q32. A reader holds a shared lock and requests an exclusive upgrade while another reader holds a shared lock and also requests an upgrade. What can happen in an implementation that retains both shared locks while waiting?
📖 Explanation: Each upgrader retains a shared hold that prevents the other from obtaining exclusivity. If both wait without releasing or using atomic upgrade support, neither can proceed, creating a possible deadlock.
Q33. A database uses one global exclusive mutex for every query and update. Which change can improve read-heavy concurrency while preserving safe writes?
📖 Explanation: A global mutex serializes even compatible reads. A read-write lock can admit multiple readers in shared mode while retaining exclusive writer access, provided its count and admission protocol are implemented correctly.
Q34. A shared service protects independent records. Two tasks access different records, while a third updates the same record as one of them. What is the main benefit and requirement of per-record locks?
📖 Explanation: Fine-grained locks reduce unnecessary contention between unrelated records. Each record still needs correct reader-writer exclusion, and operations spanning several records require a consistent lock ordering to avoid deadlock when operations share structures.
Q35. A trace begins with read_count = 0. R1 enters, R2 enters, R1 exits, R3 enters, R2 exits, and R3 exits. How many times should the first-reader/last-reader protocol acquire and release the resource semaphore?
📖 Explanation: The count follows 0 to 1 to 2 to 1 to 2 to 1 to 0. Only the first transition acquires the resource semaphore and only the final transition releases it, giving one of each.
Q36. A writer-preference service has one active reader, one waiting writer, and three later readers. The active reader finishes. Which sequence best preserves the stated policy?
📖 Explanation: Writer preference blocks new readers once a writer is waiting. When the active reader leaves, the queued writer should receive exclusive access before later readers, after which admission can be reconsidered.