badcave.blogg.se

Reader writer code
Reader writer code








reader writer code

Now, we will check while one writer is in the database, is the reader allowed into the database or not # Execute instruction down(DB)i.e DB= 0 and writer is in the database. # Now, we can assume the writer enters the database first. When the writer enters into the database first So, in this way, more than one reader can enter into the database. # Execute next instruction up(mutex)so, mutex= 1 and open the database for the second reader which can enter the database. # Now check the instruction ( RC = 1) and it is false (so, not execute down(DB)). # Execute line down(mutex)i.e mutex = 0(Previous mutex was 1). # So, we execute the reader's code again. 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) # Execute instruction down(DB) but DB is already down i.e 0, hence the writer will be suspended. Now, we will check whether the writer can enter into the database or not (when 1st reader is in the database right now)

reader writer code

# Next instruction RC= RC+ 1 = 1 and down(DB)i.e DB= 0. # Execute instruction down(mutex)i.e mutex= 0. # Now, we can assume the reader enters the database first. When Reader enter into the database first # If the writer is in the database, then the writer is not allowed. # If the writer is in the database, then readers are not allowed. # If the reader is in the database, then readers are allowed.

reader writer code

# If one reader is in the database, then the writer is not allowed to update the database. # If a writer is writing to the file, no writer may read it.Ĭondition to enter into the database (Critical Section): # Only one writer at a time writes to the database.

reader writer code

# Any number of readers may simultaneously read the database. To maintain data consistency there is a solution to the reader-writer problem which must satisfy the following condition: This shared may lead to a race condition or data inconsistency problems. A 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.










Reader writer code