猿问

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

Var mutex,empty,full:semaphore:=1,n,0;
buffer:array[0,...,n-1]of item;
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

我尤其不明白nextc是什么类型,只能存放一个元素还是一个队列?

哆啦的时光机
浏览 126回答 1
1回答

DIEA

Var mutex,empty,full:semaphore:=1,n,0; // 定义三个信号量buffer:array[0,...,n-1]of item; // 定义缓冲池,容量为nin,out:integer:=0,0;beginparbeginproceducer:begin // 生产者repeat...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;endparendendnextp 应该是next proceducer的意思吧nextc 应该是next consumer貌似也不是什么变量,属于语言描述而已下面的消费者也是差不多的。至于生产者进程如何被阻塞和唤醒,因为程序中有一个 repeat语句,所以进程不断测试缓冲池是否有空缓冲区,以及缓冲池是否有其他进程使用。若两个条件不满足,则进入阻塞队列等待。若某一时刻两个条件都能满足,则能唤醒该进程。
随时随地看视频慕课网APP
我要回答