Reader Writer Problem


Today we will be discussing another classical synchronization problem that is Reader Writer Problem. After that we will see how to solve this problem using semaphores. So, let's start, shall we?

Problem

If we have a piece of text then we can perform 2 operations on that text - read or write. Here text the critical section. but for the reader process text is not actually a critical section because reader process can never clash if we two or more than 2 reader processes. So, more  than 2 reader processes can access the critical section. But the writer process can clash with another reader or writer process.
So we have to derive a solution such that if there is a reader in the critical section then another reader  process can enter but no writer process shall enter critical section and when writer process is in the critical section then no other reader or writer process shall be allowed to enter critical section.

Solution using Semaphores

Reader Writer Problem


We have used 3 variables wrt=1, mutex=1, readcount.
We know that when a writer is accessing critical section then another reader or writer cannot be allowed to access critical section.
1. When write operation is called then writer process is invoked. When wait(wrt) is called then wrt is decremented by 1 from 1 to 0. Then we perform write operation. After that we performed signal(wrt) which incremented wrt by 1 from 0 to 1.
2. So writer process works very normally because it is not checking whether the process coming from outside is reader or writer and it is only concerned with the current process.
3. Reader process looks a bit complex because when a reader process wants to enter critical section then there must be no writer process inside and when a reader process is inside critical section then only another reader process shall be allowed to enter critical section and no writer process shall be allowed to enter.
4. In reader process we have used a mutex semaphore to synchronize between reader processes. Readcount is a variable which keeps track of number of readers accessing critical section. But readcount is a shared variable hence more than one reader process cannot access readcount at a time.
5. So notice the first 5 lines of code for reader process in the picture above. These 5 lines ensures security that not more than 1 reader process shall access readcount at a time. When a reader process comes it will call wait(mutex) and decrement mutex from 1 to 0 and readcount will be incremented to 1. Then we have checked if(readcount==1) to check if the current reader process is the first reader process or not. Since this reader process is first process so wait(wrt) will be called and wrt is decremented to 0. Then signal(mutex) is performed and mutex is incremented to 1 again and reader process is now performing read operation.
6. Now no writer process can enter critical section because wrt is currently 0, but a new reader process can enter critical section. When another reader process comes it calls wait(mutex) and turns it to 0. Then readcount is incremented to 2 . When if condition is checked then this reader process will not be able to enter if statement because it is not the first reader process and it jumps to signal(mutex) which turn mutex to 1 again. After, that this new reader process enters critical section. Now we have 2 reader processes inside critical section which we wanted and no writer process is allowed to enter critical section.
7. Now when this reader process exits critical section then it calls wait(mutex) again and turns 1 to 0 and decrements readcount by 1 so readcount becomes 1 now. Now if condition is checked if this is the last reader process or not but we know that there is one more process inside critical section so this if condition will not be executed for this process. At last signal(mutex) is called which turns mutex ot 1 again.
8. When the last reader process exits critical section then it calls wait(mutex) again and turns 1 to 0 and decrements readcount by 1 so readcount becomes 0 now. Now if condition is checked if readcount==0 which is true so it means that there are no more reader processes left in the critical section so we can allow writer process to enter critical section. That's why signal(wrt) is called inside this if condition. At last signal(mutex) is called which turns mutex ot 1 again.


4 Comments

  1. what is the initial value of "readcount" ?

    ReplyDelete
    Replies
    1. Initial value of readcount is 0. It counts number of readers inside critical section at an instant of time.

      Delete
    2. Thanks. Detailed info helped a lot in understanding how it is working.

      Delete
  2. Very nice explanation. It cleared all my doubts!!!
    Thanks for posting

    ReplyDelete
Post a Comment
Previous Post Next Post