Question

I am having trouble understanding why the first readers-writers problem can starve write processes, i.e.: how does the code provide reader processes with priority? Should the writer process not be able to gain a lock when one of the reader processes perform a signal(wrt)? Is it that the list for the semaphores is structured (as I can see how writers would be starved by a steady stream of reader processes in a LIFO list) in a way to give priority to reader processes or am I misunderstanding something fundamental here?

semaphore wrt=1,mutex=1;
readcount=0;
writer()
{
    wait(wrt);
    //writing is done
    signal(wrt);
}

reader()
{
    wait(mutex);
    readcount++;
    if(readcount==1)
        wait(wrt);
    signal(mutex);
    ///Do the Reading
    ///(Critical Section Area)
    wait(mutex);
    readcount--;
    if(readcount==0)
       signal(wrt);
    signal(mutex);
}
Was it helpful?

Solution

If you always have 2 or more readers active, the signal(wrt) will never get called at the end of the reader block. New readers won't have readcount == 1 so they won't wait on wrt, but they'll increase the readcount. This makes unending read requests starve the writer thread. If the reader count ever gets to 0, then the wrt will be released and the writer can finally work. Until that point readers have priority.

This isn't a LIFO approach precisely, but rather priority queue where readers have priority.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top