同一方向的车可连续过桥,当某一方向有车过桥时,另一方向的车必须等待,当某一方向无车过桥时,另一方向的
车可以过桥。桥上不允许两车交会,但允许同方向多辆车依次通行(即桥上可以有多个同方向的车) 写了昨天一天也不对,求各路大神帮助
可以参考这篇文章
链接:https://www.nowcoder.com/questionTerminal/f087aa15f6cf403895218d187af6d932
来源:牛客网
Var SA,SB,mutex:semaphore:=1,1,1;
CountA,countB:integer:=0,0:
begin
parbegin
process A: begin
wait(SA);
if(countA=0) then wait(mutex);
countA:=countA+1;
signal(SA);
过独木桥;
wait(SA);
countA:=countA-1;
if (countA=0) then signal(mutex);
signa(SA);
end
process B: begin
wait(SB);
if(countB=0) then wait(mutex);
countB:=countB+1;
signal(SB);
过独木桥;
wait(SB);
countB:=countB-1;
if (countB=0) then signal(mutex);
signa(SB);
end
parend
end