1 回答

TA貢獻1820條經驗 獲得超2個贊
Var mutex,empty,full:semaphore:=1,n,0; // 定義三個信號量
buffer:array[0,...,n-1]of item; // 定義緩沖池,容量為n
in,out:integer:=0,0;
begin
parbegin
proceducer:begin // 生產者
repeat
.
.
.
producer an item nextp; // 生產一個產品
.
.
.
wait(empty); // 申請一個空緩沖區
wait(mutex); // 申請緩沖池的使用權
buffer(in):=nextp; // 將產品放入緩沖池中
in:=(in+1)mod n; // 下一個空緩沖區地址
signal(mutex); //釋放緩沖池使用權
signal(full); // 釋放一個滿緩沖區
until false;
end
consumer:begin
repeat
wait(full);
wait(mutex);
nextc:=buffer(out);
out:=(out+1)mod n;
signal(mutex);
signal(empty);
consumer the item in nextc;
until false;
end
parend
end
nextp 應該是next proceducer的意思吧
nextc 應該是next consumer
貌似也不是什么變量,屬于語言描述而已
下面的消費者也是差不多的。
至于生產者進程如何被阻塞和喚醒,因為程序中有一個 repeat語句,所以進程不斷測試緩沖池是否有空緩沖區,以及緩沖池是否有其他進程使用。若兩個條件不滿足,則進入阻塞隊列等待。若某一時刻兩個條件都能滿足,則能喚醒該進程。
- 1 回答
- 0 關注
- 97 瀏覽
添加回答
舉報