FOR FREE MATERIALS

Reader Writer Problem 

 

Reader Writer Problem: Algorithm

reader-writer system consists of shared data, a number of readers that read the data, and a number of writers that update the shared data. This shared may lead to a race condition or data inconsistency problems.

 

To maintain data consistency there is a solution to the reader-writer problem which must satisfy the following condition:

# Any number of readers may simultaneously read the database.

# Only one writer at a time writes to the database.

# If a writer is writing to the file, no writer may read it.

 

Condition to enter into the database (Critical Section):

#  If one reader is in the database, then the writer is not allowed to update the database.

#  If the reader is in the database, then readers are allowed.

#  If the writer is in the database, then readers are not allowed.

# If the writer is in the database, then the writer is not allowed.

 

 

Analysis of Reader and Writer problem

 

When Reader enter into the database first

# Initially, DB = 1.

# Now, we can assume the reader enters the database first.

# Execute reader’s code first.

# Execute instruction down(mutex) i.e mutex = 0.

# Next instruction RC = RC + 1 = 1 and down(DB) i.e DB = 0.

# Now, up(mutex) then mutex = 1.

 

So, the first reader is in the database.

 

Now, we will check whether the writer can enter into the database or not (when 1st  reader is in the database right now) 

# So, we will execute writer code.

# Execute instruction down(DB)  but DB is already down i.e 0, hence the writer will be suspended.

 

Now, we will check whether a second reader can enter into the database or not (when the first reader is in the database right now)

# So, we execute the reader's code again.

# Execute line down(mutex) i.e mutex = 0 (Previous mutex was 1).

# Then execute next RC = RC + 1 = 2. 

# Now check the instruction (RC == 1) and it is false (so, not execute down(DB)).

# Execute next instruction up(mutex) so, mutex =  1 and open the database for the second reader which can enter the database.

 

So, in this way, more than one reader can enter into the database.

 

When the writer enters into the database first

# Now, we can assume the writer enters the database first.

# Execute writer’s code first.

# Execute instruction down(DB) i.e DB = 0 and writer is in the database.

 

Now, we will check while one writer is in the database, is the reader allowed into the database or not

# So, we execute the reader's code to check it.

# Execute instruction down(mutex) i.e, mutex = 0.

# Then next line RC = RC + 1 = 1.

# So, at next instruction (RC == 1) is true then execute next instruction. 

# down(DB) but DB already down by writer (who is right now inside the database). 

 

So, the reader will be blocked.

 

Now, we will check while one writer is in the database, is the other writer allowed into the database or not

# So, we execute the writer's code again.

# Execute instruction down(DB) but DB is already down by 1st writer. 

 

So, the writer will be suspended.