计算机操作系统(第四版)汤小丹课后答案完整版 下载本文

内容发布更新时间 : 2024/4/28 4:23:34星期一 下面是文章的全部内容请认真阅读。

所有信号量均被初始化为

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;

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,

-1,n]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;