Read writerās Problem
Readers writer problem is an example of a classic synchronization problem.
The Problem Statement
There is a shared resource which should be accessed by multiple processes. There are two types of processes in this context. They areĀ
readerĀ and
Ā writer. Any number ofĀ
readersĀ can read from the shared resource simultaneously, but only one writer can write to the shared resource. When aĀ
writerĀ is writing data to the resource, no other process can access the resource. AĀ
writerĀ cannot write to the resource if there are non zero number of readers accessing the resource at that time.
a)
- Mutual Exclusion: When a Writer is writing data no Reader or other Writer can access the data to prevent more than one thread modifying the data.
- Hold and Wait:Ā A shared file for a Writer is locked and only released when modifying is done. Writers are allowed if there are no readers requesting it.
- No Preemption:Ā Readers cannot request data that is currently being modified by a Writer likewise if data has Readers Writers cannot request to write into the file.
- Circular Wait:Ā if many Writers want to modify the data they have to wait one after another in a circular manner until they all get a chance since this canāt be done simultaneously.
b.)Ā Ā
Readers Preference Solution
In this solution, preference is given to the writers. This is accomplished by forcing every reader to lock and release the readtry semaphore individually. The writers on the other hand don't need to lock it individually. Only the first writer will lock the readtry and then all subsequent writers can simply use the resource as it gets freed by the previous writer. The very last writer must release the readtry semaphore, thus opening the gate for readers to try reading.
No reader can engage in the entry section if the readtry semaphore has been set by a writer previously. The reader must wait for the last writer to unlock the resource and readtry semaphores.
