Readers-Writers Problem
The Readers-Writers Problem is a classic example of a synchronization problem faced in computing, specifically in the context of shared resources. The problem can be explained as follows:
There are a shared resource (like a file or a database) and multiple processes that need access to this resource.
There are two kinds of processes: readers and writers.
Readers only read the data from the resource and do not perform any updates.
Writers can read and modify the data.
The critical constraint is that while a writer is writing to the resource, no other writer or reader should be able to access the resource. This is to prevent data inconsistency.
However, multiple readers can simultaneously read the resource without any issues, as long as there is no writer writing to it.
Solving Using Mutexes and Semaphores
We use mutexes and semaphores to synchronize access to the resource. A mutex ensures mutual exclusion, i.e., only one process can access the critical section of code at a time. Semaphores are used to control access, specifically a binary semaphore (which acts like a mutex) and a counting semaphore.
The solution involves using two semaphores:
A binary semaphore (often referred to as a
write_lock
) for writers to ensure mutual exclusion while writing.A counting semaphore to keep track of the number of readers currently accessing the resource.
Pseudo Code
Writer Pseudo Code
do {
wait(write_lock); // Acquire the write lock
// Perform writing...
signal(write_lock); // Release the write lock
} while (true);
Reader Pseudo Code
do {
wait(mutex); // Acquire mutex to update reader count
read_count++;
if (read_count == 1) {
wait(write_lock); // If this is the first reader, lock the write lock
}
signal(mutex); // Release mutex
// Perform reading...
wait(mutex); // Acquire mutex to update reader count
read_count--;
if (read_count == 0) {
signal(write_lock); // If this is the last reader, release the write lock
}
signal(mutex); // Release mutex
} while (true);
Explanation
Writer Process:
The writer acquires the
write_lock
before starting to write. This ensures that no other writer or reader can access the resource while writing is in progress.After writing, it releases the
write_lock
, allowing other processes (readers or writers) to access the resource.
Reader Process:
Each reader process acquires the
mutex
to safely increment the count of readers (read_count
).If it's the first reader, it also acquires the
write_lock
to prevent writers from accessing the resource while reading is in progress.After reading, the reader decrements
read_count
and checks if it's the last reader. If it is, it releases thewrite_lock
, allowing writers to access the resource.The
mutex
is used to ensure that the update ofread_count
is mutually exclusive.
This solution ensures that writers have exclusive access to the resource while writing, and multiple readers can read concurrently, as long as no writer is writing. The key is to prevent a writer from writing when there are readers reading and vice versa.