Sub topic 13
The Readers-Writers Problem
Assume you know processes, threads, context switches, and semaphore operations. This chapter uses wait to decrement a semaphore, blocking when its value is zero, and signal to increment it and possibly wake a waiting thread.
The shared-data rule
The readers-writers problem models a shared object such as a database file:
- Several readers may access the data at the same time.
- A writer must have exclusive access.
- A reader must not access the data while a writer is changing it.
- Writers must eventually get access if readers keep arriving.
Readers can share access because they do not modify the object. Writers cannot share access with readers or with other writers.
| Access pattern | Allowed? | Reason |
|---|---|---|
| Reader with reader | Yes | Both only inspect the data |
| Reader with writer | No | The reader might observe partial changes |
| Writer with writer | No | Their updates could interfere |
A reader-preference solution
A common solution uses two semaphores and a counter:
rwprotects the shared data from writers and from the group of readers.mutexprotects thereadcountvariable.readcountrecords how many readers are currently reading.
semaphore rw = 1;
semaphore mutex = 1;
int readcount = 0;
void writer(void) {
wait(rw);
write_data();
signal(rw);
}
void reader(void) {
wait(mutex);
readcount++;
if (readcount == 1)
wait(rw);
signal(mutex);
read_data();
wait(mutex);
readcount--;
if (readcount == 0)
signal(rw);
signal(mutex);
}The first reader locks rw. This prevents writers from entering while at least one reader is active. Later readers do not need to lock rw; they only update readcount while holding mutex.
The last reader releases rw. A writer can then enter, perform its update, and release rw.
Tracing the algorithm
Suppose no one is using the data:
- Reader A increments
readcountfrom 0 to 1. - Reader A waits on
rw, sorwbecomes unavailable. - Reader B increments
readcountfrom 1 to 2. - Reader B skips
wait(rw)and begins reading. - Reader A finishes and decrements the count to 1.
- Reader B finishes and decrements the count to 0.
- Reader B signals
rw, allowing a writer to proceed.
A writer waits directly on rw, so it cannot enter while any reader holds the shared access.
Starvation and fairness
This solution gives readers preference. A writer can starve if new readers repeatedly arrive before the current reader group becomes empty. Each new reader may join before the writer obtains rw.
A fair solution usually adds a queue or turnstile. Every reader and writer passes through that ordering mechanism, so a waiting writer cannot be bypassed forever. The trade-off is lower concurrency because readers may wait even when another reader is already active.
Exercise
In the reader-preference solution, can a writer enter while readcount is 3? What event allows it to enter?
Show answer
No. The first reader holds rw while readcount is positive. The writer can enter only after the last reader decrements readcount to 0 and signals rw.