为了账号安全,请及时绑定邮箱和手机立即绑定

根据记录型信号量的特点,生产者进程怎么被阻塞怎么被唤醒?

根据记录型信号量的特点,生产者进程怎么被阻塞怎么被唤醒?

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条经验 获得超3个赞

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 关注
  • 125 浏览

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号