861 views

Synchronization in the classical readers and writers problem can be achieved through use of semaphores. In the following incomplete code for readers-writers problem, two binary semaphores mutex and wrt are used to obtain synchronization

wait (wrt)
writing is performed
signal (wrt)
wait (mutex)
if readcount = 1 then S1
S2
S3
if readcount = 0 then S4
signal (mutex)

The values of S1, S2, S3, S4, (in that order) are

1. signal (mutex), wait (wrt), signal (wrt), wait (mutex)
2. signal (wrt), signal (mutex), wait (mutex), wait (wrt)
3. wait (wrt), signal (mutex), wait (mutex), signal (wrt)
4. signal (mutex), wait (mutex), signal (mutex), wait (mutex)
asked | 861 views

S1: if readcount is1 i.e some reader is reading, DOWN on wrt so that no writer can write.

S2:After readcount has been updated , UP on mutex.

S3:DOWN on mutex to update readcount

S4: If readcount is zero i.e no reader is reading ,UP on wrt .Allow writer to Write

answered by (7.3k points)
selected

Answer is C :

The reason for it since they said classical Reader Writer problem it mean First Reader Writer problem .

The first Reader Writer problem says that : At any time, on a shared resources multiple reader can read it. In between if any writer comes then it wont be allowed unless last reader complete its reading. (so writer starves here )

So following the above thing :

S1= Wait(wrt) implies that as soon as the first reader come, it should lock resources so that no writer can access it.

S2 = signal (mutex ) implies that mutex is made to 1, so other reader can entry into entry section. Multiple reading are allowed. (only 1 at a time can be in entry section )

S3 = wait (mutex) implies that at a time only one process can be in Exit section and semaphore mutex is used to implement it (only 1 at a time can be in exit section)

S4 = signal (wrt ) implies that when last reader has completed reading, it should unlock resources so that any writer waiting for it can use it .

The above values of S1, S2, S3, and S4 are all nothing but implementing first reader writer problem :)

answered by (1.3k points)
+1 vote

The question has mixed up code for writers and readers. The first three lines should be seperate from the rest of the code or else option c doesn't make sense. Because according to option C if we executed wait on wrt at s1 we do not release it until readcount is zero i.e. all the reader processes have moved out.

If those first three lines are not seperate and exclusively for writers then option c is not going to allow multiple readers to read database or file at the same time. Here is how: Suppose a reader process R1 came in and executed wait on wrt at S1. Now another reader say R2 comes in and in line 1, tries to acquire wrt. R1 is yet to release wrt and hence R2 goes into a spinlock. A reader has prevented another reader from entering so this is a clear violation of required solution of reader writer problem which says any number of readers can read db at a time if only readers are accessing db and there is no writer.

If the first three lines are taken away from this code and give only to writer processes and rest of code is kept only for reader processes then option c works. Something as follows...

void writer {

wait (wrt)
writing is performed
signal (wrt)

}

wait (mutex)
if readcount = 1 then S1
S2
S3
if readcount = 0 then S4
signal (mutex)

}

I guess a simple printing mistake or copy paste mistake on part of the poster but still...

answered by (73 points)

+1 vote