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.