AfterAcademy Tech
•
17 Nov 2019

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.
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:
We can solve the above two problems by using the semaphore variable(learn more about semaphore from here). The following is the proposed solution:
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:
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:
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:
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!
AfterAcademy Tech
In this blog, we will learn what an Operating System is and what are the goals of an Operating System. We will also learn the functionalities of an Operating System that helps in achieving the goal of the OS.

AfterAcademy Tech
In this blog, we will learn about the Producer-Consumer problem in Operating System and we will also learn how to solve this problem. It is used in multi-process synchronization.

AfterAcademy Tech
In this blog, we'll learn about the Network Operating System and its various features. We'll also see the types of Network Operating System, their advantages, and disadvantages.

AfterAcademy Tech
In this blog, we will learn what is Spooling in Operating System and what are the advantages of using Spooling. We will also see how we can use Spooling to imporve the performance of the system.
