Var rmutex,wmutex: semophore:=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
reader and writer
最新推荐文章于 2021-11-27 13:18:19 发布