亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

根據記錄型信號量的特點,生產者進程怎么被阻塞怎么被喚醒?

根據記錄型信號量的特點,生產者進程怎么被阻塞怎么被喚醒?

C
哆啦的時光機 2023-03-03 17:13:35
Var mutex,empty,full:semaphore:=1,n,0;buffer:array[0,...,n-1]of item;in,out:integer:=0,0;beginparbeginproceducer:beginrepeat...producer an item nextp;...wait(empty);wait(mutex);buffer(in):=nextp;in:=(in+1)mod n;signal(mutex);signal(full);until false;endconsumer:beginrepeatwait(full);wait(mutex);nextc:=buffer(out);out:=(out+1)mod n;signal(mutex);signal(empty);consumer the item in nextc;until false;endparendend我尤其不明白nextc是什么類型,只能存放一個元素還是一個隊列?
查看完整描述

1 回答

?
DIEA

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語句,所以進程不斷測試緩沖池是否有空緩沖區,以及緩沖池是否有其他進程使用。若兩個條件不滿足,則進入阻塞隊列等待。若某一時刻兩個條件都能滿足,則能喚醒該進程。


查看完整回答
反對 回復 2023-03-06
  • 1 回答
  • 0 關注
  • 97 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號