记录型信号量解决读者写者问题,会的话帮帮忙嘛
1个回答
展开全部
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
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
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询