100% Free Forever
AI-Powered Learning
Industry Expert Content
Certificates & Badges
Learn At Your Own Pace

The Readers-Writers Problem Explained

Learn the readers-writers problem — concurrent reads, exclusive writes, reader vs writer preference, and starvation.

mediumQ68 of 224 in Operating Systems Est. time: 6 minsLast updated:
Open Code Lab

Expected Interview Answer

The readers-writers problem is a synchronization challenge where multiple reader threads may safely access shared data concurrently, but a writer thread must have exclusive access with no readers or other writers present, and the classic solutions differ in whether they favor readers, writers, or fairness between the two.

A typical solution uses a mutex to protect a shared reader-count variable and a second lock (often a semaphore) guarding the actual resource: the first reader to arrive acquires the resource lock on behalf of all readers, subsequent readers just increment the count and proceed, and the resource lock is only released once the last reader decrements the count back to zero, while any writer must acquire the resource lock exclusively with no readers active. The 'first readers-writers problem' favors readers — a writer may starve if readers keep arriving — while the 'second readers-writers problem' favors writers, making new readers wait if a writer is waiting, which can starve readers instead. Production systems typically use a fairer, FIFO-ordered reader-writer lock that queues both reader and writer requests in arrival order to avoid starving either side, since pure reader-preference or writer-preference is rarely acceptable outside teaching examples. The key insight interviewers want is that this pattern models any resource read far more often than it is written, like a cache or a configuration store, and correctness plus starvation-avoidance both matter.

  • Allows high read concurrency, since reads do not conflict with each other
  • Guarantees write correctness via exclusive access during writes
  • Models a very common real workload: read-heavy shared data structures
  • Highlights the starvation tradeoff between reader-preference and writer-preference designs

AI Mentor Explanation

The readers-writers problem is like a scoreboard that many spectators can look at simultaneously without any conflict, but only one groundskeeper can physically update the numbers at a time, and no spectator should be reading it mid-update. A reader-preference approach lets spectators keep glancing at the board freely, even if it means the groundskeeper waits a long time for a gap; a writer-preference approach instead makes new spectators wait once the groundskeeper is queued up to update it. A fair stadium alternates fairly so neither spectators nor the groundskeeper are stuck waiting forever.

Step-by-Step Explanation

  1. Step 1

    Reader arrives

    A reader locks a counter mutex, increments the reader count, and if it is the first reader, locks the resource against writers.

  2. Step 2

    Concurrent reading

    Subsequent readers increment the counter and proceed without blocking each other.

  3. Step 3

    Last reader exits

    The final reader to leave decrements the counter to zero and releases the resource lock, allowing a writer in.

  4. Step 4

    Writer access

    A writer acquires the resource lock exclusively, blocking all readers and other writers until it releases.

What Interviewer Expects

  • Clear statement of the constraint: concurrent readers, exclusive writers
  • Correct description of the reader-count + resource-lock pattern
  • Ability to contrast reader-preference vs writer-preference and their starvation risks
  • Awareness that fair, FIFO-ordered locks are preferred in real systems

Common Mistakes

  • Forgetting readers still need a mutex to safely update the shared counter
  • Assuming reader-preference is always the “default” or only correct approach
  • Not recognizing writer starvation as a real risk under reader-preference
  • Confusing this with plain mutual exclusion, ignoring that reads can be concurrent

Best Answer (HR Friendly)

The readers-writers problem is about letting many people read shared data at the same time safely, while making sure only one person can update it at a time and nobody reads it half-changed. The tricky part is deciding who gets priority when both readers and a writer are waiting, since always favoring readers can leave a writer waiting forever, and always favoring writers can do the same to readers, so most real systems use a fair, first-come-first-served approach instead.

Code Example

Reader-preference readers-writers using two locks
pthread_mutex_t count_mutex = PTHREAD_MUTEX_INITIALIZER;
pthread_mutex_t resource     = PTHREAD_MUTEX_INITIALIZER;
int reader_count = 0;

void start_read(void) {
    pthread_mutex_lock(&count_mutex);
    if (++reader_count == 1)
        pthread_mutex_lock(&resource);   /* first reader locks out writers */
    pthread_mutex_unlock(&count_mutex);
}

void end_read(void) {
    pthread_mutex_lock(&count_mutex);
    if (--reader_count == 0)
        pthread_mutex_unlock(&resource); /* last reader releases writers */
    pthread_mutex_unlock(&count_mutex);
}

void write_data(void) {
    pthread_mutex_lock(&resource);       /* exclusive access */
    /* ... write ... */
    pthread_mutex_unlock(&resource);
}

Follow-up Questions

  • How does the second readers-writers problem prevent writer starvation?
  • How would you implement a fair, FIFO reader-writer lock?
  • What real-world data structures benefit from a readers-writers lock?
  • How does this problem relate to the producer-consumer problem?

MCQ Practice

1. In the readers-writers problem, which access pattern is allowed concurrently?

Multiple readers may safely access shared data at the same time since none of them modify it.

2. What risk does the classic reader-preference solution introduce?

If readers always get priority, a writer can wait indefinitely as long as new readers keep arriving.

3. Why does the first reader need to acquire the resource lock?

The first reader locks the resource for the whole group of readers; only the last reader to leave releases it, keeping writers out during any active reads.

Flash Cards

What is the readers-writers problem?Allowing concurrent reads of shared data while writes require exclusive access.

How is writer exclusivity enforced?The first reader locks the resource; the last reader unlocks it; writers lock it exclusively.

Reader-preference risk?Writer starvation if readers keep arriving continuously.

What do production systems typically use?A fair, FIFO-ordered reader-writer lock to avoid starving either side.

1 / 4

Continue Learning