Second Solution Algorithm for Writer Readers

It is very difficult for me to understand the second algorithm of the problem of readers and writers. I understand the general concept that authors will get priority over readers (readers may starve). I even understand the conditional implementation variable of this algorithm. Read / write records in C ++ . However, implementing semaphore and mutex makes no sense to me. This is an example from Wikipedia:

int readcount, writecount; (initial value = 0)
semaphore mutex 1, mutex 2, mutex 3, w, r ; (initial value = 1)

READER
  P(mutex 3);
    P(r);
      P(mutex 1);
        readcount := readcount + 1;
        if readcount = 1 then P(w);
      V(mutex 1);
    V(r);
  V(mutex 3);

  reading is done

  P(mutex 1);
    readcount := readcount - 1;
    if readcount = 0 then V(w);
  V(mutex 1);


WRITER
    P(mutex 2);
      writecount := writecount + 1;
      if writecount = 1 then P(r);
    V(mutex 2);

  P(w);
    writing is performed
  V(w);

  P(mutex 2);
    writecount := writecount - 1;
    if writecount = 0 then V(r);
  V(mutex 2);

[http://en.wikipedia.org/wiki/Readers-writers_problem][2]

I do not understand that the three semaphores (mutex 3, r and mutex 1) are in read lock. Isn't one semaphore enough for reading?

+5
source share
2 answers

mutex 1 readcount; mutext 2 writecount; mutex r mutext w .

1) , :

mutex 2 writercount () , writercount ( mutex 2), , (writercount==1), true, mutex r - (writercount > 1) r.

mutex w () .

(writecount==1) mutex r, .

2) , :

mutex 3 ; mutex r (, r ); mutex 1, readcount ( , ), (readercount == 1), mutex w ( ).

, (, mutex w , )

(w), .


, , , ( p), , . , mutex 3 mutex r, r, .

+8
+4

All Articles