Multiple processes want to read or write a shared resource. Readers can access concurrently, but writers need exclusive access. We need to coordinate them.
A synchronization problem where multiple readers can read simultaneously, but a writer requires exclusive access (no readers or other writers).
- Readers: increment readCount (atomically), if first reader, wait on writeLock
- Readers: read data, decrement readCount, if last reader, signal writeLock
- Writers: wait on writeLock (exclusive access), write, signal writeLock
- Mutex: protects readCount variable
- Readers can proceed concurrently
- Writers need exclusive access
- Starvation possible: writers may starve if readers keep coming
- Uses readCount, mutex, writeLock semaphores
- Built from: Semaphore, Producer-Consumer Problem
- Builds into: Race Condition
- Related: Mutex, Wait Operation
- Contrasts with: Producer-Consumer (readers concurrent vs producer-consumer serialized)
- Writer starvation: readers keep arriving, writer never gets access
- Reader starvation: writer locks resource, new readers blocked
- readCount must be protected by mutex (it’s a shared variable)