Readers/Writers Workbench
Monitor-based Solution
The readers/writers (RW) problem is a classic synchronization problem. A set of processes called readers and a set of processes called writers share a common database (DB). The readers just read the value stored in the DB and the writers change the value of the DB. The reader executes an infinite loop in which it reads the value of the database and the writer executes an infinite loop in which it creates a new value and stores it into the DB. The following important conditions have to be satisfied
by the readers and writers:
- At most one writer can be updating the DB at any time. While a writer is updating the DB, no readers are allowed to read the contents of the DB.
- One or more readers may be reading the DB at the same time. While there are readers reading the DB, no writers are allowed to modify the value of the DB.
This workbench displays a solution to the RW problem using a monitor. The monitor has four procedures: StartRead, EndRead, StartWrite, and EndWrite. Before reading a reader must call StartRead and after reading it must call EndRead. Before writing, a writer must call StartWrite and after writing, a writer must call EndWrite. The monitor has two condition variables: OkToRead and OkToWrite. The monitor variable busy indicates if a writer is updating the DB and the monitor variable ReaderCount counts the number of readers in the DB. The workbench shows three readers (portrayed as books) and two writers (portrayed as pencils). Colored bullets indicate the position in the reader or writer code for each process. A box labeled DB shows the value of the DB. Readers and writers enter this box when they are
allowed to read/write the DB. You can execute the workbench in a step-by-step mode by pushing the STEP button for each individual process or by moving each process one step forward (STEP ALL button). You can also
execute the workbench in a continuous mode by pushing the CONTINUE button. You will see controls that allow you to change the animation speed.
Workbench