Actually the first three lines of code is for writers and remaining code is for readers.
writers(){
wait (wrt);
writing is performed;
signal (wrt);
}
readers(){
wait (mutex) line 1
readcount = readcount + 1 line 2
if readcount = 1 then S1 line 3
S2 line 4
reading is performed line 5
S3 line 6
readcount = readcount - 1 line 7
if readcount = 0 then S4
signal (mutex)
}
A) signal (mutex), wait (wrt), signal (wrt), wait (mutex)
writers:-
wait (wrt)
writing is performed
signal (wrt)
readers:-
wait (mutex)
readcount = readcount + 1
if readcount = 1 then signal (mutex)
wait (wrt)
reading is performed
signal (wrt)
readcount = readcount - 1
if readcount = 0 then wait (mutex)
signal (mutex)
In readers sections, at line 7 readcount is shared variable between readers but its not getting protected by any lock, so lets consdier a case where there are n readers wants to read
then
- n readers executes wait(mutex) only one of them we will enter all other n-1 processes will get blocked/waiting state.
- now the process which has entered updates read count to 1 then satifies then IF condition so executes signal so it gonna wakeup a process from waiting list [ lets say its P2]
- now for P2 it updates read count to 2 and as of now value of mutex=0
- till signal(wrt) both P1 and P2 executes succesfully, here comes twist , lets take sequential case first P1 then P2 so now P1 changes readcount to 1 then preempts here then P2 changes read count to 0 and satifies IF condition so it calls wait(Mutex) as mutex value= 0 it gets blocked
- P1 resumes its execution as read count is 0, p1 also calls wait(mutex) so P1 also gets blocked
So n readers got blocked its deadlock and this code allows atmost one reader to read, this code wont allow more than one readers to read at same time which is not desirable so this option is incorrect
so its not gonna work.
option B) signal (wrt), signal (mutex), wait (mutex), wait (wrt)
writers:-
wait (wrt)
writing is performed
signal (wrt)
readers:-
wait (mutex)
readcount = readcount + 1
if readcount = 1 then signal (wrt)
signal (mutex),
reading is performed
wait (mutex)
readcount = readcount - 1
if readcount = 0 then wait (wrt)
signal (mutex)
consider case where a writer and reader wants to access critical section,
now
1)Writer calls wait(wrt) now value of wrt=0, and writer perfoms its respective operations
- reader calls wait(mutex) so its get insider and updates readcount to 1 and calls signal(wrt) then calls signal(mutex), it going to perform read operation now which is not a desirable result
so option B is also incorrect.
option D) signal (mutex), wait (mutex), signal (mutex), wait (mutex)
writers:-
wait (wrt)
writing is performed
signal (wrt)
readers:-
wait (mutex)
readcount = readcount + 1
if readcount = 1 then signal (mutex)
wait (mutex),
reading is performed
signal (mutex)
readcount = readcount - 1
if readcount = 0 then wait (mutex)
signal (mutex)
Here also same issue both reader and writer perform there operations at same time which is not desirable
so Option C is correct one.