Suppose that a database is to be shared among several concurrent processes. Some of these processes may want only to read the database, whereas others may want to update (that is, to read and write) the database. We distinguish between these two types of processes by referring to the former as readers and to the latter as writers. Obviously, if two readers access the shared data simultaneously, no adverse effects will result. However, if a writer and some other process (either a reader or a writer) access the database simultaneously, chaos may ensue.
To ensure that these difficulties do not arise, we require that the writers have exclusive access to the shared database while writing to the database. This synchronization problem is referred to as the readers writers problem.
Readers-writers problem has two variants.
The second readers writers problem requires that, once a writer is ready, that writer perform its write as soon as possible. In other words, if a writer is waiting to access the object, no new readers may start reading.
In this case readers may strarve.
We want to write a code for the second reader-writer problem such that starvation does't occur.
Implementation Details:
We have 2 writers and 4 readers. The writer thread has a function for writing on the data structure and the reader thread has a funtion for reading from it. Each writer thread writes its own thread id in the data strcture. In the beggining of the program all of the thread are made and run at the same time.
Output Example:
reader01 reads o writer02 writes 4536 readero3 reads 4536 reader04 reads 4536 writero1 writes 4541 readero2 reads 4541