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:

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