I got some questions about semaphores.
here is a reader/writer algorithm.
Semaphore x =1, y=1,z=1,w=1,r=1;
int nread=0,nwrite=0;
Reader:
Writer:Code:for(;;) { P(z); P(r); P(x); if (++nread == 1) P(w); V(x); V(r); V(z); READ P(x); if (--nread == 0) V(w); V(x); }
I dont understand why so many semaphores are needed. what if I take away sempaphore z and P(z) and V(z)? Will reader take precedence over writer?Code:for(;;) { P(y); if (++nwrite == 1) P(r); V(y); P(w); WRITE V(w); P(y); if (--nwrite == 0) V(r); V(y); }
[code][/code]tagged by Salem



LinkBack URL
About LinkBacks


