---
title: "Readers-writers problem with semaphore"
subject: "Operating System"
chapter: 3
key: "semester_6/operating_system:readers-writers"
source: https://www.bctengineer.com/q/semester_6/operating_system/readers-writers
site: BCT Engineering
---

# Readers-writers problem with semaphore

Operating System · Chapter 3: Process Communication and Synchronization

- Asked in: year not given
- Video: [The Readers Writers Problem](https://www.youtube.com/watch?v=p2XDhW5INOo) — Neso Academy
- Answer checked by Claude Opus 5.5 (high effort) on 2026-10-14
- Page: https://www.bctengineer.com/q/semester_6/operating_system/readers-writers
- Whole chapter: https://www.bctengineer.com/md/semester_6/operating_system/chapter-3

## Question

Explain the Readers-writers problem. Also explain how this problem can be solved using semaphore.

## Short answer

The readers-writers problem ensures many readers can read a shared resource simultaneously, but a writer requires exclusive access. A semaphore-based solution uses a mutex to protect the reader count rc, and a db semaphore. The first reader locks db, subsequent readers enter; the last reader unlocks db, allowing waiting writers. This favors readers, possibly starving writers.

## Answer

#### The problem
A shared database (or file) is used by many processes. **Readers** only read; **writers** read and modify it. The rules are:
- Many readers may read at the same time (reading does not change the data).
- A writer must have exclusive access: no other reader or writer may be active while it writes.

Without control, a reader could see half-written data, or two writers could corrupt the data.

#### Solution using semaphores (first readers-writers problem)
Two semaphores and a counter are used: `mutex` (initially 1) protects the reader count `rc`; `db` (initially 1) gives exclusive access to the database.

```c
semaphore mutex = 1;   /* protects the variable rc */
semaphore db    = 1;   /* controls access to the database */
int rc = 0;            /* number of readers currently reading */

void reader() {
    while (TRUE) {
        down(&mutex);
        rc = rc + 1;
        if (rc == 1) down(&db);   /* first reader locks out writers */
        up(&mutex);
        read_database();
        down(&mutex);
        rc = rc - 1;
        if (rc == 0) up(&db);     /* last reader lets writers in */
        up(&mutex);
        use_data_read();
    }
}
void writer() {
    while (TRUE) {
        think_up_data();
        down(&db);                /* exclusive access */
        write_database();
        up(&db);
    }
}
```

- The first reader locks `db`; later readers enter freely. The last reader releases `db`.
- A writer must get `db`, so it waits until all readers have left.
- A drawback: readers have priority, so a steady stream of readers can **starve** writers. A writer-priority version (second problem) makes new readers wait if a writer is waiting, but then readers can starve.

## Check yourself

The answer's key points as multiple-choice questions, with the correct option and why.

1. Which access rule applies in the readers-writers problem?
   A. Only one reader at a time
   B. Readers must wait for all writers
   C. Many readers may read together, but a writer needs exclusive access
   D. Writers may write together with readers
   Answer: C. Reading does not change data, so readers can share; writers need exclusivity. (Key point: Reader/writer rules)

2. What is the semaphore mutex used for in the solution?
   A. Giving exclusive access to the database
   B. Counting writers
   C. Protecting the reader count rc
   D. Blocking readers forever
   Answer: C. mutex protects rc; db controls database access. (Key point: Semaphore roles)

3. Which reader performs down(&db)?
   A. The last reader
   B. Every reader
   C. Only writers
   D. The first reader (when rc becomes 1)
   Answer: D. The first reader locks out writers; later readers enter freely and the last reader does up(&db). (Key point: First and last reader)

4. What is the drawback of this reader-priority solution?
   A. Readers cannot read concurrently
   B. A steady stream of readers can starve writers
   C. Deadlock always occurs
   D. Writers can read and write simultaneously
   Answer: B. While rc stays above 0, db is never released for writers. (Key point: Starvation)

## Related in Operating System

- [Dining philosophers problem and solution](https://www.bctengineer.com/question.md?key=semester_6%2Foperating_system%3Adining-philosophers) — Operating System, chapter 3, asked 2×
- [Producer-consumer using semaphore](https://www.bctengineer.com/question.md?key=semester_6%2Foperating_system%3Apc-semaphore-mutual-exclusion) — Operating System, chapter 3, asked 2×
- [Producer-consumer: semaphore and messages](https://www.bctengineer.com/question.md?key=semester_6%2Foperating_system%3Apc-semaphore-message-passing) — Operating System, chapter 3
- [Monitor; dining philosophers with semaphore](https://www.bctengineer.com/question.md?key=semester_6%2Foperating_system%3Amonitor-dining-philosopher) — Operating System, chapter 3

## Related in other subjects

- [Sports car dashboard coupling problem](https://www.bctengineer.com/question.md?key=semester_5%2Finstrumentation_ii%3Asports-car-coupling) — Instrumentation II, chapter 6
- [Near-far problem in CDMA](https://www.bctengineer.com/question.md?key=semester_5%2Fdata_communication%3Acdma-near-far) — Data Communication, chapter 6
