Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Readers-Writers Problem
The readers-writers problem relates to an object such as a file that is shared between multiple processes. Some of these processes are readers i.e. they only want to read the data from the object and some of the processes are writers i.e. they want to write into the object.
The readers-writers problem is used to manage synchronization so that there are no problems with the object data. For example − If two readers access the object at the same time there is no problem. However if two writers or a reader and writer access the object at the same time, there may be problems.
To solve this situation, a writer should get exclusive access to an object i.e. when a writer is accessing the object, no reader or writer may access it. However, multiple readers can access the object at the same time.
Synchronization Rules
Multiple readers can read the shared object simultaneously.
Only one writer can write to the shared object at a time.
No reader and writer can access the shared object simultaneously.
Writers have exclusive access − no other process can access the object while a writer is active.
This can be implemented using semaphores. The codes for the reader and writer process in the reader-writer problem are given as follows −
Reader Process
The code that defines the reader process is given below −
wait (mutex);
rc ++;
if (rc == 1)
wait (wrt);
signal(mutex);
/* READ THE OBJECT */
wait(mutex);
rc --;
if (rc == 0)
signal (wrt);
signal(mutex);
In the above code, mutex and wrt are semaphores that are initialized to 1. Also, rc is a variable that is initialized to 0. The mutex semaphore ensures mutual exclusion and wrt handles the writing mechanism and is common to the reader and writer process code.
The variable rc denotes the number of readers accessing the object. As soon as rc becomes 1, wait operation is used on wrt. This means that a writer cannot access the object anymore. After the read operation is done, rc is decremented. When rc becomes 0, signal operation is used on wrt. So a writer can access the object now.
Writer Process
The code that defines the writer process is given below −
wait(wrt); /* WRITE INTO THE OBJECT */ signal(wrt);
If a writer wants to access the object, wait operation is performed on wrt. After that no other writer can access the object. When a writer is done writing into the object, signal operation is performed on wrt.
Step-by-Step Execution
| Scenario | Action | rc Value | wrt Status | Result |
|---|---|---|---|---|
| First reader arrives | wait(mutex), rc++, wait(wrt) | 1 | Blocked | Writer blocked |
| Second reader arrives | wait(mutex), rc++ | 2 | Blocked | Reader proceeds |
| First reader finishes | rc--, signal(mutex) | 1 | Blocked | Writer still blocked |
| Last reader finishes | rc--, signal(wrt) | 0 | Available | Writer can proceed |
Key Points
Reader preference − This solution gives preference to readers over writers.
Writer starvation − If readers keep arriving, writers may wait indefinitely.
Mutual exclusion − The
mutexsemaphore protects therccounter.Resource control − The
wrtsemaphore controls access to the shared object.
Conclusion
The readers-writers problem demonstrates how to manage concurrent access to shared resources using semaphores. While multiple readers can access the resource simultaneously, writers require exclusive access. This solution prioritizes readers but may lead to writer starvation in high-read scenarios.
