var count:integer:=0;
mutex,sofa,empty,full:=semaphore:=1,N,1,0;
cut,payment,receipt:semaphore:=0,0,0;
begin
parbegin
guest:begin
wait(mutex);
if(count>N) then
begin
signal(mutex);
离开理发店;
end
else
begin
count:=count+1;
if(count>1) then
begin
wait(sofa);
在沙发上就座;
wait(empty);
从沙发上起来;
signal(sofa);
end
else
wait(empty);
在理发椅上就座;
signal(full);
理发;
付费:
signal(payment);
wait(receipt);
从理发椅上起来;
signal(empty);
wait(mutex);
count:=count-1;
signal(mutex);
离开理发店:
end
end
barber:begin
repeat
wait(full);
替顾客理发:
wait(payment);
收费;
signal(recipt);
until false
end
parend
end