Var rmutex, wmutex:semaphore∶=1,1;
Readcount:integer∶=0;
begin
parbegin
Reader:begin
repeat
wait(rmutex);
if readcount=0 then wait(wmutex);
Readcount∶=Readcount+1;
signal(rmutex);
…
perform read operation;
…
wait(rmutex);
readcount∶=readcount-1;
if readcount=0 then signal(wmutex);
signal(rmutex);
until false;
end
writer:begin
repeat
wait(wmutex);
perform write operation;
signal(wmutex);
until false;
end
parend
end
2. 利用信号量集机制解决读者-写者问题
Var RN integer;
L, mx:semaphore∶=RN,1;
begin
parbegin
reader:begin
repeat
Swait(L,1,1);
Swait(mx,1,0);
…
perform read operation;
…
Ssignal(L,1);
until false;
end
writer:begin
repeat
Swait(mx,1,1; L,RN,0);
perform write operation;
Ssignal(mx,1);
until false;
end
parend
end