What is the readers-writers problem?

Why Interviewers Ask This

This is a classic screening question for Operating Systems roles. Hiring managers ask it early in interviews to gauge your baseline understanding and determine if you can communicate technical concepts clearly.

Answer

The Readers-Writers problem synchronizes access to a shared resource where multiple readers can read simultaneously (safe — no modification) but writers need exclusive access (modifying means no readers or writers can access concurrently). Requirements: multiple readers can read simultaneously; only one writer at a time; readers and writers are mutually exclusive; no starvation. First readers-writers solution (readers priority): Semaphore mutex = 1; // Protect readCount Semaphore wrt = 1; // Writer lock int readCount = 0; // Reader: wait(mutex); readCount++; if (readCount == 1) wait(wrt); // First reader locks signal(mutex); READ_DATA(); wait(mutex); readCount--; if (readCount == 0) signal(wrt); // Last reader unlocks signal(mutex); // Writer: wait(wrt); // Exclusive access WRITE_DATA(); signal(wrt);. Problem: writers can starve if readers keep arriving (there's always a reader, so wrt is never released for the writer). Second readers-writers (writers priority): writers are given preference when waiting — complex implementation preventing new readers when a writer is waiting. Real-world implementations: Java ReadWriteLock: ReadWriteLock lock = new ReentrantReadWriteLock(); lock.readLock().lock(); try { readData(); } finally { lock.readLock().unlock(); } lock.writeLock().lock(); try { writeData(); } finally { lock.writeLock().unlock(); }. Used in: database systems (many readers, few writers), cache systems, configuration management.

Pro Tip

If you're unsure about a detail, say so honestly and explain your reasoning. Interviewers respect candidates who can think through uncertainty rather than bluffing.