0
votes

I am studying the solution to First Readers - Writers Problem from the book Operating System Concepts (9th edition) and it describes:

First readers–writers problem, requires that no reader be kept waiting unless a writer has already obtained permission to use the shared object. In other words, no reader should wait for other readers to finish simply because a writer is waiting.

And what I have understood from this is that if there is any reader running and a writer comes; then that writer would be blocked until reader has completed. But during the completion of first reader if there comes another reader (or multiple readers), then that (those) reader(s) will be given priority over writer.

First of all please correct me if I am wrong here. But if am right, then what I have understood is that, the code for Reader does not guarantee this.

Code for reader-writer is given below:

//data structures
semaphore rw_mutex = 1;
semaphore mutex = 1;
int read_count = 0;

//Code for writer
do {
   wait(rw_mutex);
   . . .
   /* writing is performed */
   . . .
   signal(rw_mutex);
} while (true);

//Code for reader
do {
   wait(mutex);            //Line 01
   read_count++;

   if (read_count == 1)
      wait(rw_mutex);      //Line 02

   signal(mutex);
   . . .
   /* reading is performed */
   . . .

   wait(mutex);            //Line 03
   read_count--;

   if (read_count == 0)
      signal(rw_mutex);    //Line 04

   signal(mutex);
} while (true);

Now suppose following sequence of events occurs:

  • Suppose first reader comes and it blocks the writer on the line mentioned as Line 02 in comments
  • Then a writer comes which is waiting for rw_mutex
  • Then first reader is executing the code line mentioned as Line 03 in comments in code and it has locked the mutex semaphore
  • At the same time second reader comes and it starts waiting on line mentioned as Line 01 in comments in code
  • Now when the first reader executes Line 04; it releases the lock on rw_mutex and the writer which was waiting in its while loop is unlocked now and starts executing
  • The second reader will be unlocked when the first reader executes the line after Line 04 that signals mutex semaphore

Now if we see the overall flow, then the writer runs before the second reader. So does the code logic work the same as described above?

Please correct me if I am wrong.

1
Where did this code come from? - user3344003
This code is taken from the book mentioned (i.e. Operating System Concepts - 9th edition) - swdeveloper
That's the problem. There are most questions posted from nonsense from that book than from any other source. That code makes no sense. - user3344003

1 Answers

0
votes

Yes the code will work same as you have understood. You can refer wiki page here , which have similar pseudo code which you have included in question.

Writer thread will obtain the rw_mutex as soon as it is freed by Reader-Thread1. But as you explained in your use-case, there will be nothing wrong with that.

In First-reader writer problem, writers can be starved if too many read operations are in progress.