The Reader-Writer Problem in Operating System

In the last blog, we learned about the Producer-Consumer problem in Operating System. In this blog, we will learn about the Reader-Writer problem in Operating System which is used for process synchronization.

If you have no idea about the process synchronization, then learn from here. Also, learn about semaphore from here. Now, you are done with the prerequisites of the blog, so let's get started with the reader-writer problem.

What's the problem?

In an Operating System, we deal with various processes and these processes may use files that are present in the system. Basically, we perform two operations on a file i.e. read and write. All these processes can perform these two operations. But the problem that arises here is that:

  • If a process is writing something on a file and another process also starts writing on the same file at the same time, then the system will go into the inconsistent state. Only one process should be allowed to change the value of the data present in the file at a particular instant of time.
  • Another problem is that if a process is reading the file and another process is writing on the same file at the same time, then this may lead to dirty-read because the process writing on the file will change the value of the file, but the process reading that file will read the old value present in the file. So, this should be avoided.

The solution

We can solve the above two problems by using the semaphore variable(learn more about semaphore from here). The following is the proposed solution:

  • If a process is performing some write operation, then no other process should be allowed to perform the read or the write operation i.e. no other process should be allowed to enter into the critical section(learn more about critical section from here).
  • If a process is performing some read operation only, then another process that is demanding for reading operation should be allowed to read the file and get into the critical section because the read operation doesn't change anything in the file. So, more than one reads are allowed. But if a process is reading a file and another process is demanding for the write operation, then it should not be allowed.

So, we will use the above two concepts and solve the reader-writer problem with the help of semaphore variables. The following semaphore variables will be used in our solution:

  • Semaphore "writer": This semaphore is used to achieve the mutual exclusion property. It is used by the process that is writing in the file and it ensures that no other process should enter the critical section at that instant of time. Initially, it will be set to "1".
  • Semaphore "mutex": This semaphore is used to achieve mutual exclusion during changing the variable that is storing the count of the processes that are reading a particular file. Initially, it will be set to "1".

Apart from these two semaphore variables, we have one variable readerCount that will have the count of the processes that are reading a particular file. The readerCount variable is initially initialized to 0.

We will also use two function wait() and signal(). The wait() function is used to reduce the value of a semaphore variable by one and the signal() function is used to increase the value of a semaphore variable by one.

The following is the pseudo-code for the process that is writing something in the file:

wait(writer)
  ...
  write operation
  ...
signal(writer)

The above code can be summarized as:

  • The wait(writer) function is called so that it achieves the mutual exclusion. The wait() function will reduce the writer value to "0" and this will block other processes to enter into the critical section.
  • The write operation will be carried and finally, the signal(writer) function will be called and the value of the writer will be again set to "1" and now other processes will be allowed to enter into the critical section.

This is how we can deal with the process of doing the write operation. Now, let's look at the reader problem.

The following is the pseudo-code for the process that is reading something from the file:

wait(mutex)              -----  
readerCount++                | 
if(readerCount == 1)         |--- changing the readerCount
    wait(writer)             |
signal(mutex)            -----

    ...
    read operation

wait(mutex)              -----
readerCount--                |
if(readerCount == 0)         |--- changing the readerCount
    signal(writer)           |
signal(mutex)            -----

The above code can be summarized as:

  • We are using the mutex variable to change something in the readerCount variable. This is done because if some process is changing something in the readerCount variable, then no other process should be allowed to use that variable. So, to achieve mutual exclusion, we are using the mutex variable.
  • Initially, we are calling the wait(mutex) function and this will reduce the value of the mutex by one. After that, the readerCount value will be increased by one.
  • If the readerCount variable is equal to "1" i.e. the reader process is the first process, in this case, no other process demanding for write operation will be allowed to enter into the critical section. So, the wait(writer) will be called and the value of the writer variable will be decreased to "0" and no other process demanding for write operation will be allowed.
  • After changing the readerCount variable, the value of the mutex variable will be increased by one, so that other processes should be allowed to change the value of the readerCount value.
  • The read operation by various processes will be continued and after that when the read operation is done, then again we have to change the count the value of the readerCount and decrease the value by one.
  • If the readerCount becomes "0", then we have to increase the value of the writer variable by one by calling the signal(writer) function. This is done because if the readerCount is "0" then other writer processes should be allowed to enter into the critical section to write the data in the file.

So, this is how we can solve the reader-writer problem. Hope you learned something new today.

That's it for this blog.

Do share this blog with your friends to spread the knowledge. Visit our YouTube channel for more content. You can read more blogs from here.

Keep Learning :)

Team AfterAcademy!