0
votes

It said in Wikipedia that the code below "adds the constraint that no thread shall be allowed to starve" and I can't understand it why there is no starvation. for example : what if there are many writers arrive before any readers and the first writer took a very long time to finish his writing then r maybe reach some big minus number lets say -12345 and after it readers begin to arrive along with writers, and somehow the operating system always choose writer to take the semaphore and not reader, in that case the readers would be starve, is that right or am I wrong ? link : readers-writers problem

look at the third readers-writers problem in the link.

code :

int readcount; // (initial value = 0)
semaphore mutex_rdcnt, r, w; // ( initial value = 1 ) 
//READER
    wait(r);
      wait(mutex_rdcnt);
        readcount++;
        if (readcount == 1)
          wait(w);
      signal(mutex_rdcnt);          
    signal(r);

    // reading is performed 

    wait(mutex_rdcnt);
      readcount--;
      if (readcount == 0)
        signal(w);
    signal(mutex_rdcnt);  
//WRITER
    wait(r);
    wait(w);

     // writing is performed

    signal(w);  
    signal(r);
1

1 Answers

0
votes

found the problem : Note that this solution can only satisfy the condition that "no thread shall be allowed to starve" if and only if semaphores preserve first-in first-out ordering when blocking and releasing threads. Otherwise, a blocked writer, for example, may remain blocked indefinitely with a cycle of other writers decrementing the semaphore before it can.