如何用信号量机制描述读者写者问题? 20
1个回答
展开全部
设置互斥信号量wmutex 表示写者间、读者和写者间互斥
用readcount变量来记录读者数
Var rmutex,wmutex: semaphore:=1,1 ;
readcount :integer :=0 ;
begin
parbegin
reader:begin
repeat
P(rmutex)
if readcount=0 then P(wmutex);
readcount=readcount+1;
V(rmutex)
read text
P(rmutex)
readcount=readcount+1;
if readcount=0 then V(wmutex);
V(rmutex)
until false
writer:begin
repeat
P(wmutex);
write text;
V(wmutex);
until false
end
parend
end
用readcount变量来记录读者数
Var rmutex,wmutex: semaphore:=1,1 ;
readcount :integer :=0 ;
begin
parbegin
reader:begin
repeat
P(rmutex)
if readcount=0 then P(wmutex);
readcount=readcount+1;
V(rmutex)
read text
P(rmutex)
readcount=readcount+1;
if readcount=0 then V(wmutex);
V(rmutex)
until false
writer:begin
repeat
P(wmutex);
write text;
V(wmutex);
until false
end
parend
end
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询