About Reader-Writer Problem


A data object is to be shared among several concurrent processes. Some of these processes may want to only to read the content of the shared object, whereas others may want to update the shared object. We distinguish between these two types of processes by referring to those processes that are interested in only reading as readers, and to the rest as writers. Obviously, if two readers access the shared data object simultaneously, no adverse effects will result.

The readers-writers problem has several variations, all involving priorities. The simplest one, referred to as the first readers-writers problem, requires that no reader will be kept waiting unless a writer has already obtained permission to use the shared object. The second readers-writers problem requires that, once a writer is ready, that writer performs its write as soon as possible. Here, I adopt the first readers-writers problem. At a given time, there is only one writer and any number of readers. When a writer is writing, the readers can not enter into the database. The readers need to wait until the writer finishes to write on the database. Once a reader succeed in reading the database, subsequent readers can enter into the critical section (in this case, say, database) without waiting for the precedent reader finish to read. On the other hand, a writer who arrives later than the reader who is reading currently is required to wait the last reader finish to read. Only when the last reader finish reading, the writer can enter into the critical section and is able to write on the database.

To implement this algorithm, we need two kinds of semaphore, that is wrt and mutex. The former plays a role of lock for the database itself. Readers or writers can enter into the database only if they have this semaphore. The later semaphore is used to lock the critical section prier and posterior to reading: we need them to increase and decrease the number of readers in database.