Here is the code for the reader process (workable code)
reader(){
while(true){
P(mutex);
readerCounter++;
if((readerCount == 1)
P(OKtoaccessDB);
V(mutex);
accessDB;
readerCounter--;
if(readerCounter == 0)
V(OKtoaccessDB);
V(mutex);
}
}
And here is the process for writer;
writer(){
while(true){
P(OKtoacessDB);
accessDB;
V(OKtoacessDB);
}
}
What would be the outcome if replacing (in reader)
From:
if(readerCounter == 0)
V(OKtoacessDB);
V(mutex);
to :
if(readerCounter == 0){
V(OKtoaccessDB);
V(mutex);
}
Thank you!
V(mutex); mutex is locked to exclusively access the readerCounter. If you change the code like this:
You'll release the mutex only when readerCounter is zero. In all other cases the reader will exit without unlocking the mutex. That essentially means your first reader exiting the read call when readerCounter was not zero will go away without unblocking mutex. This will block all the subsequent readers to progress.