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

内容发布更新时间 : 2024/6/26 7:45:31星期一 下面是文章的全部内容请认真阅读。

所有信号量均被初始化为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,…,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 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);

wait(k1,k2,k3,...,kn); //k1,k2,...,kn 为执行消费者进程除full外其余的条件 wait(mutex); nextc:=buffer(out); out:=(out+1) mod n; signal(mutex); signal(empty); signal(k1,k2,k3,...,kn); consume the item in nextc; until false; end parend end

32.什么是信号量集?试利用信号量集写出读者一写者问题的解法。 答:对AND信号量加以扩充,形成的信号量集合的读写机制。 解法:Var RN integer; L,mx: semaphore:=RN,1; begin parbegin reader:begin repeat Swait(L,1,1); Swait(mx,1,1); …

perform read operation; …

Ssignal(L,1); until false end writer:begin repeat

Swait(mx,1,1;L,RN,0); perform write operation; Ssignal(mx,1);