begin repeat ?
producer an item in nextp; wait(mutex);
wait(full); /* 应为wait(empty),而且还应该在wait(mutex)的前面 */ buffer(in):=nextp;
/* 缓冲池数组游标应前移: in:=(in+1) mod n; */ signal(mutex); /* signal(full); */ until false; end
consumer: begin repeat
wait(mutex);
wait(empty); /* 应为wait(full),而且还应该在wait(mutex)的前面 */ nextc:=buffer(out);
out:=out+1; /* 考虑循环,应改为: out:=(out+1) mod n; */ signal(mutex);/* signal(empty); */ consumer item in nextc; until false; end
27.试利用记录型信号量写出一个丌会出现死锁的哲学家迚餐问题的算法. 答:Var chopstick:array[0,?,4] of semaphore;
所有信号量均被初始化为1,第i 位哲学家的活动可描述为: Repeat
Wait(chopstick[i]);
Wait(. chopstick[(i+1) mod 5]); ? Ea.t ; ?
Signal(chopstick[i]);
Signal(chopstick[(i+1) mod 5]) Ea.t ; ? Think; 11
Until false;
28.在测量控制系统中的数据采集仸务,把所采集的数据送一单缓冲区;计算仸务从该单
缓冲中叏出数据迚行计算.试写出利用信号量机制实现两者共享单缓冲的同步算
法。 答:
a. Var mutex, empty, full: semaphore:=1, 1, 0; gather: begin repeat ??
gather data in nextp; wait(empty); wait(mutex); buffer:=nextp; signal(mutex); signal(full); until false; end
compute: begin repeat ??
wait(full); wait(mutex); nextc:=buffer; signal(mutex); signal(empty);
compute data in nextc; until false; end
b. Var empty, full: semaphore:=1, 0; gather: begin repeat ??
gather data in nextp; wait(empty); buffer:=nextp; signal(full); until false; end
compute: begin repeat ??
wait(full); nextc:=buffer;
signal(empty);
compute data in nextc; until false; end
29.画图说明管程由哪几部分组成,为什么要引入条件发量? 答:管程由四部分组成:①管程的名称;②局部于管程内部的共享数据结构说明;③对该数据结构进行操作的一组过程;④对局部于管程内部的共享数据设臵初始值的语句;
当一个进程调用了管程,在管程中时被阻塞或挂起,直到阻塞或挂起的原因解除,而在此期间,如果该进程不释放管程,则其它进程无法进入管程,被迫长时间地等待。为了解决这个问题,引入了条件变量condition。 30.如何利用管程来解决生产者不消费者问题?
答:首先建立一个管程,命名为ProclucerConsumer,包括两个过程:
(1)Put(item)过程。生产者利用该过程将自己生产的产品放到缓冲池,用整型变
量count 表示在缓冲池中已有的产品数目,当count≥n 时,表示缓冲池已满,生产者须 等待。
(2)get(item)过程。消费者利用该过程从缓冲池中取出一个产品,当count≤0
时,表示缓冲池中已无可取的产品,消费者应等待。 PC 管程可描述如下:
type producer-consumer =monitor Var in,out,count:integer;
buffer:array[0,…,n-1]of item; notfull,notempty:condition; procedure entry dot(item) begin
if count>=n then not full.wait; buffer(in):=nextp; in:=(in+1)mod n; count:=count+1;
if notempty.queue then notempty.signal; end
procedure entry get(item) begin
if count<=0 then not full.wait; nextc:=buffer(out); out:=(out+1)mod n; count:=count-1;
if notfull.quene then notfull.signal; end
begin in:=out:=0; count:=0
end
在利用管程解决生产者一消费者问题时,其中的生产者和消费者可描述为: producer: begin pepeat
produce an inem in nestp PC.put(item); until false; end
consumer: begin repeat
PC.get(item);
consume the item in enxtc; until false; end 31.什么是AND信号量?试利用AND信号量写出生产者一消费者问题的解法。 答:为解决并行带来的死锁问题,在wait 操作中引入AND 条件,其基本思想是将进
程在整个运行过程中所需要的所有临界资源,一次性地全部分配给进程,用完后一次性释放。
解决生产者-消费者问题可描述如下: var mutex,empty,full: semaphore:=1,n,0; buffer: array[0,...,n-1] of item; in,out: integer:=0,0; begin parbegin 14
producer: begin repeat ?
produce an item in nextp; ?
wait(empty);
wait(s1,s2,s3,...,sn); //s1,s2,...,sn为执行生产者进程除empty 外其余的条件 wait(mutex);
buffer(in):=nextp; in:=(in+1) mod n; signal(mutex); signal(full);
signal(s1,s2,s3,...,sn); until false; end
consumer: begin repeat wait(full);
相关推荐: