Reader Writer Problem in OS With Example

In this tutorial, we will learn about readers writers problem in os (or reader writer problem in os). We will see what is it and how is it solve using semaphores.

Reader Writer problem in os is an example of classic synchronization problem. This problem is useful for modeling processes which are competing or a limited shared resource.

Problem Statement

A resource is shared among many processes, each belonging to one of two processes. They are either reader or writer. In this problem, any number of readers can read from the shared resource simultaneously, but at a time only one writer can write to the shared resource. Also, when a writer is writing data to the resource, at that time no other process can access the resource. Readers do not write, readers only read. If a process is writing, no other process can read it.

Now, we will see how to solve this readers writers problem –

Solution of Reader Writer Problem With Semaphore

In this solution, readers has higher priority than writer. It means no reader should wait if shared resource is opened for reading.

Following variables have been used in the solution –

  • We are using two semaphores –

    Wrt: It is used for both readers and writers.
    Mutex: It controls access to the reader count. It is used to ensure mutual exclusion.

  • readcount: It is a variable that represents the number of reading process to access data.

Following functions have also been used for semaphores –

  • wait(): It decrements the semaphore value.
  • signal(): It increments the semaphore value.

Code for Writer Process

do{
// Writer requests to enter in the Critical Section 
wait(Wrt);
//Writer perform write operations 
// Writer leaves the Critical Section
signal(Wrt); 
} while(true);

Explanation:
Writer requests the entry to the Critical Section. It will enter when wait() gives a true value. It enters and performs operation. After performing the write operation, it increments Wrt so that next writer can access the resource. It exists the Critical Section. If it doesn’t allow, it will keep waiting for the signal call.

Code for Reader Process

do{
// Reader wants to enter in Critical Section
wait(Mutex);

// readcount is now increased by 1
readcount++; 

// Here, we are making sure that no writer can access critical section even when there is atleast one reader 
// accessing the critical section. Writer will have to wait in this scenario.
If(readcount == 1)
  wait(Wrt);

// Other readers can enter when current reader is inside the Critical Section
signal(Mutex); 

//Read operation
wait(Mutex); //reader wants to leave 

readcount--;

// Checking if there is any reader present in the Critical Section. If no i.e. reacount == 0, then, 
// writer can access the critical section.
If (readcount == 0)
  signal(Wrt); // writers can enter

signal(Mutex); // reader leaves 
} while (true);

Explanation:
When a reader wants to access the resource, At first it increments readcount value. Then, access the resource. After that, it decrements the readcount value. The semaphore Wrt is used by the first reader (which enters in the Critical Section) and last reader (which leaves the Critical Section). This is because when the first readers enter in the Critical Section, the writer can not enter in the Critical Section. Only new reader can access the critical section now.
Similarly, when the last reader leaves the Critical Section, it signals the Wrt semaphore. This is because there are no readers that is using the critical section now. So, a writer can access the resource now.

That’s end of tutorial on reader writer problem in os.

Leave a Reply