Site icon i2tutorials

OS – Readers and Writers Problem

Readers and Writers Problem

 

The readers and writers problem is based on a situation where there are a number of readers and writers trying to access the same data, the reader is reading the data, and the writer is trying to write into it. 

For example, a file that numerous people have access to. 

If one person tries to edit the file, no other person should be reading or writing in the same file. As it may lead to faulty data, and the changes will not be visible to the person. 

However, if one person is reading a file, other people who have access to it can read it. 

 

Solution:

  1. When the reader has a priority over the writer.

Priority implies that no reader should wait if the file is open elsewhere for reading.

To implement the solution three variables are used. 

        

Functions for Semaphore:

 

Writer process:

 Step 1: The writer will have to request access for entry in the critical section.

Step 2: It will have to wait until and unless wait() gives a true value.

Step 3: The writer exits the critical section.

 

Reader process:

Step 1: The reader requests access to the critical section.

Step 2: When allowed, it will:

Step 3: After completion, it exits the critical section and will signal wrt semaphore to allow writers. 

 

Reference

Readers and Writers Problem

Exit mobile version