操作系统教程_孙钟秀(第四版)课后习题答案

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

begin repeat P (S2 ) ; 取原料; 做香烟; V(S) ; 吸香烟;

untile false ; process 吸烟者3 begin repeat P (S3 ) ; 取原料; 做香烟; V ( S ) ; 吸香烟;

untile false ; coend .

( 3 )用管程。

TYPE mskesmoke=moonitor

VAR S, S1 ,S2 ,S3 : condition ; flag1 , flag2, flag3 : boolean

DEFINE give , take1 , take2 , take3 ; USE check , wait , signal , release ; procedure give begin

check ( IM ) ; 准备香烟原料;

if 桌上有香烟原料then wait( S , IM ) ; 把准备的香烟原料放桌上; if fiag2 & flag3 then signal ( S1 ,IM);

if flag1 & flag3 then signal ( S2 ,IM ) ; else signal (S3 , IM ) ; release ( IM ) ; end

procedure take1 begin

check(IM):

if 桌上没有香烟原料then wait ( S1 ,IM); else 取原料;

signal ( S , IM ) ; release ( IM ) ; end

procedure take2 begin

check ( IM ) :

if 桌上没有香烟原料 then wait(S2,IM); else 取原料;

signal ( S , IM ) ; release (IM); end

procedure take3 begin

check ( IM ) :

if 桌上没有香烟原料then wait(S3,IM); else 取原料

signal ( S ,IM ) ; release ( IM ) ; end begin

flag1:=flag2:=flag3:=true; end.

cobegin {

process 供应者 begin repeat

Call makesmoke.give(); ??

until false ; end

process 吸烟者1 begin repeat

Call makesmoke.take1() ; 做香烟,吸香烟; until false ; end

process 吸烟者2 begin repeat

Call makesmoke.take2() ; 做香烟,吸香烟; until false ; end

process 吸烟者3 begin repeat

Call makesmke.take3();

做香烟,吸香烟; until false ; end }

coend .

18、 如图所示,四个进程Pi (i=0? 3 )和四个信箱Mj (j=0? 3 ) ,进程间借助相邻信箱传递消息,即Pi 每次从Mi中取一条消息,经加工后送入M(i + 1) mod4 ,其中M0 、M1 、M2 、M3 ;可存放3 、3 、2 、2 个消息。初始状态下,MO 装了三条消息,其余为空。试以P 、V 为操作工具,写出Pi(i=0?3)的同步工作算法 答:

var mutexl , mutexZ , mutex3 ,mutex0 :semaphore; Mutex1=nutex2:=mutex3:=mutex0:=1;

Empty0,empty1,empty2, empty3; semaphore; empty:=0 ; empty1:=3 ; empty:=2:=empty3:=2; full0 , full1 , full2 , full3:semphore ; full0:=3;full1:=full2:=full3:=0;

in0,in1,in2,in3,out0 ,out2,out3,;intger;

in0:=in1:=in2:=in3:=out0:=out1:=out2:=out3:=0; cobegin {

process P0 begin repeat P(full0); P(mutex0);

从M0[out0]取一条消息; out0:=(out0+1) mod 3 ; V(mutex0); V(empty0) ; 加工消息; P(empty1) ; P(mutex1) ; 消息已M1[in1];

In1:=(in1+1) mod 3; V(mutex1) ; V(full1 ) ; untile false ; end

process P1 begin repeat

P ( full1 ) ; P ( mutex1 ) ;

从M1[out1]取一条消息; Out1:=(out1+1) mod 3 ; V(mutex1); V(empty1); 加工消息; P(empty2); P(mutex2 ) ; 消息己M2[in2];

In2:=(in2+1) mod 2; V(mutex2 ) ; v ( full2 ) ; untile false ; end

process P2 begin repeat P(full2) ; P(mutex2 ) ;

从M2[out2]取一条消息; out2:=(out2 + l ) mod 2; V(mutex2) ; V(empty2) ; 加工消息; P(empty3) ; P(mutex3) ; 消息己M3[in3];

in3:=(in3+1) mod 2 ; V(mutex3) ; V(full3) ;

untile false ; end

process P3 begin repeat P(full3) ; P(mutex3) ;

从M3[out3] 取一条消息; out3:=(out3+1)mod 2; V (mutex3) ; V (empty3) ; 加工消息;

P ( empty0 ) ;

P ( mutex0 ) ; 消息己MO[in0];

In0:=(in0+1) mod 3 ; V(mutex0) ; V(full0) ;

untile false ; end { coend

19、有三组进程Pi 、Qj、Rk ,其中Pi 、Qj构成一对生产者和消费者,共享一个由M1个缓区构成的循环缓冲池buf1 。Qj、Rk凡构成另一对生产者和消费者,共享一个由M2 个缓冲区构成的循环缓冲池buf2 。如果Pi每次生产一个产品投入buf1,Qj每次从中取两个产品组装成一个后并投入buf2,Rk每次从中取三个产品包装出厂. 试用信号量和P 、V操作写出它们同步工作的程序。 答:

var mutex1 , mutex2 , mutex3 : semaphore; empty1 , empty2 , full1 , full2 ; semaphore ;

in1 , in2 , out1 , out2 : integer ; counter1 , counter2:integer ; buffer1:array[0?M1-1] of item ; buffer2:array[0?M2-1]of item ; empty1:=M1 ; empty:=M2;

in1 : = in2 :=out1:=out2:=0 ; counter1:=counter2:=0 ; fun1:=full2:=mutex1:=mutex2:=mutex3:=1; cobegin {

process Pi begin L1:

P(empty1) ; P(mutex1 ) ;

put an item into buffer [in1] ; in1:=(in1+1) mod M1 ; counter++;

if counter1 = 2 then { counter1:=0;V(full1);} V(mutex) ; goto L1; end

process Qj begin L2:

P ( full2) ; P ( mutex1 ) ;

take an item from buffer1[out1]; out1:=(out1+1) mod M1;

联系客服:779662525#qq.com(#替换为@) 苏ICP备20003344号-4 ceshi