Readers Writers Problem – Critical Section Problem

  • August 28, 2021
  • Aashish Mishra
  • The readers-writers problem is a classical problem of process synchronization, it relates to a data set such as a file that is shared between more than one process at a time.
  • Among these various processes, some are Readers – which can only read the data set; they do not perform any updates, some are Writers – can both read and write in the data sets.
  • The readers-writers problem is used for managing synchronization among various reader and writer process so that there are no problems with the data sets, i.e. no inconsistency is generated.

Let’s understand with an example – If two or more than two readers want to
access the file at the same point in time there will be no problem. However,
in other situations like when two writers or one reader and one writer wants
to access the file at the same point of time, there may occur some
problems, hence the task is to design the code in such a manner that if one
reader is reading then no writer is allowed to update at the same point of
time, similarly, if one writer is writing no reader is allowed to read the file at
that point of time and if one writer is updating a file other writers should not
be allowed to update the file at the same point of time. However, multiple
readers can access the object at the same time.

The solution of readers and writers can be implemented using binary
semaphores.

We use two binary semaphores “write” and “mutex”, where binary semaphore
can be defined as:

Semaphore: A semaphore is an integer variable in S, that apart from
initialization is accessed by only two standard atomic operations – wait and
signal.

Solution for Reader:

static int readcount = 0;
wait (mutex);
readcount ++; // on each entry of reader increment readcount
if (readcount == 1)
{
wait (write);
}
signal(mutex);
–READ THE FILE?
wait(mutex);
readcount –; // on every exit of reader decrement readcount
if (readcount == 0)
{
signal (write);
}
signal(mutex);

In the above code of reader, mutex and write are semaphores that have an
initial value of 1, whereas the readcount variable has an initial value as 0.
Both mutex and write are common in reader and writer process code, semaphore mutex ensures mutual exclusion and semaphore write handles the writing mechanism.

The readcount variable denotes the number of readers accessing the file
concurrently. The moment variable readcount becomes 1, wait operation is used to write semaphore which decreases the value by one. This means

that a writer is not allowed how to access the file anymore. On completion of the read
operation, readcount is decremented by one. When readcount becomes 0, the
signal operation which is used to write permits a writer to access the file.

Solution for Writer:

wait(write);
WRITE INTO THE FILE
signal(write);

If a writer wishes to access the file, wait operation is performed on write
semaphore, which decrements write to and no other writer can access the
file. On completion of the writing job by the writer who was accessing the file,
the signal operation is performed on write.