Readers/Writers Workbench
Semaphore-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 two semaphores: mutex and db. In addition, a variable called rc (for reader count) is shared by all readers. The mutex semaphore controls access to rc. The db semaphore is used to give access to the DB to a single writer or to several readers. Our 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