
The semaphore w is used by the first reader which enters the critical section and the last reader which exits the critical section. When a reader wants to access the resource, first it increments the read_count value, then accesses the resource and then decrements the read_count value. On the other hand, in the code for the reader, the lock is acquired whenever the read_count is updated by a process. After performing the write operation, it increments w so that the next writer can access the resource. As seen above in the code for the writer, the writer just waits on the w semaphore until it gets a chance to write to the resource. The code for the writer process looks like this: while(TRUE)Īnd, the code for the reader process looks like this: while(TRUE) Instead of having the process to acquire lock on the shared resource, we use the mutex m to make the process to acquire and release lock whenever it is updating the read_count variable. A value of 1 is given initially to m and w. The variable read_count is initialized to 0. An integer variable read_count is used to maintain the number of readers currently accessing the resource. Here, we use one mutex m and a semaphore w.
If a writer wants to write to the resource, it must wait until there are no readers currently accessing that resource.
A writer cannot write to the resource if there are non zero number of readers accessing the resource at that time.įrom the above problem statement, it is evident that readers have higher priority than writer. When a writer is writing data to the resource, no other process can access the resource. Any number of readers can read from the shared resource simultaneously, but only one writer can write to the shared resource. There are two types of processes in this context. There is a shared resource which should be accessed by multiple processes. There are many variants of this problem, one of which is examined below. Readers writer problem is another example of a classic synchronization problem.
Longest Remaining Time First Scheduling.