ReadersWriters Problem | Writers Preference Solution (original) (raw)

Last Updated : 30 Aug, 2025

The Readers-Writers Problem is a classic synchronization challenge in operating systems and concurrent programming. It describes a scenario where multiple threads or processes need to access a shared resource (like a database or file) simultaneously. Some of these threads are "readers" (they only read the data), and others are "writers" (they modify the data). The goal is to allow as much concurrent access as possible while preserving data consistency.

In simpler terms, the rules of this problem are:

This problem is important because it models real-world situations like managing a database with many simultaneous users. We want to maximize efficiency by letting readers run in parallel, but we must prevent conflicts with writers.

Read more about Reader-Writer Problem

There are two main preference solutions for the Readers-Writers problem: **Readers Preference, prioritizing readers (potentially starving writers), and **Writers Preference, prioritizing writers (potentially starving readers).

The Writers Preference Solution

There are multiple variations of the readers-writers problem, depending on who gets priority when both readers and writers are waiting. In the Writers Preference solution (also known as writers-priority), the system is designed to prioritize writers over readers.

The motivation for this approach comes from a drawback in the simplest (readers-preference) solution:

How Writers Preference Works

Implementation of Writers Preference Solution

To implement Writers Preference Solution we need to enforce the rule that no new reader may start when a writer is waiting. A common strategy is to use semaphores (or similar locking primitives) to control access.

**Following steps should be followed to implement Writers Preference Solution:

semaphore resource = 1; // actual shared data
semaphore readTry = 1; // gate that writers can close to block new readers
semaphore rmutex = 1; // protects read_count
semaphore wmutex = 1; // protects write_count
semaphore serviceQueue = 1; // turnstile to serialize arrival across readers & writers
int read_count = 0;
int write_count = 0;

// -------- Reader --------
void reader() {
// Get in the global line so we can't jump ahead of a waiting writer
wait(serviceQueue);
wait(readTry); // blocked if a writer has closed the gate
signal(serviceQueue); // let next arrival line up

wait(rmutex);
read_count++;
if (read_count == 1) wait(resource); // first reader locks the resource
signal(rmutex);

signal(readTry); // pass gate along (a queued writer will get it next)

// ---- Critical Section (read) ----
// ... read shared data ...

wait(rmutex);
read_count--;
if (read_count == 0) signal(resource); // last reader frees the resource
signal(rmutex);
}

// -------- Writer --------
void writer() {
wait(wmutex);
write_count++;
if (write_count == 1) wait(readTry); // first waiting writer closes the reader gate
signal(wmutex);

// Get in the same global line as readers so they can't cut ahead
wait(serviceQueue);
wait(resource); // exclusive access
signal(serviceQueue);

// ---- Critical Section (write) ----
// ... write shared data ...

signal(resource);

wait(wmutex);
write_count--;
if (write_count == 0) signal(readTry); // reopen gate for readers
signal(wmutex);
}

Limitations of the Writers Preference Solution

While the writers-preference solution effectively prevents writer starvation by ensuring that waiting writers eventually gain priority access, it introduces certain trade-offs: