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

内容发布更新时间 : 2024/5/11 12:31:58星期一 下面是文章的全部内容请认真阅读。

2)信号量与P、V操作

Var S,S1,S2,S3:=semaphore; S:=1,S1:=S2:=S3:=0;

Container{糖,水,橘子精}; Cobegin

{ process 供应商 Begin Repeat P(s);

Take raw material into container; If (container)=橘子精 then V(S1); Else if (container)=糖then V(s2); Else V(s3); Until false; End

Process P1 Begin Repeat P(S1);

Take the 橘子精 from container; V(s);

做橘子水; Until false; End

Process P2 Begin Repeat P(s2);

Take the 糖from container; V(s);

做橘子水; Until false; End

process P3 begin repeat P ( S3 ) ;

take the 水 from container; V ( S ) ; 做桔子水;

untile false ; end }

coend .

46 有一材料保管员,他保管纸和笔若干。有A 、B 两组学生,A 组学生每人都备有纸,B 组学生每人都备有笔.任一学生只要能得到其他一种材料就可以写信。有一个可以放一张纸或一支笔的小盒,当小盒中无物品时,保管员就可任意放一张纸或一支笔供学生取用,每次允许一个学生从中取出自己所需的材料,当学生从盒中取走材料后允许保管员再存放一件材料,请用:1 )信号量与P 、v 操作,2 )管程,写出他们并发执行时能正确工作的程序。 答:1 )信号量与P 、v 操作。

var s , Sa . Sb , mutexa , mutexb : s emaphore ; s : =mutexa :=mutexb : = 1 ; sa : = sb : = 0 ; box : ( PaPer , pen ) ; cobegin {

process 保管员 begin repeat P ( S ) ;

take a material intobox ;

if ( box ) = Paper then V ( Sa ) ; else V( Sb ) ; untile false ; end

Process A组学生 begin repeat P ( Sa ) ;

P ( mutexa ) ;

take the pen from box ; V ( mutexa ) ; V ( S ) ;

write a letter; untile false ; end

Process B组学生 begin repeat P ( Sb ) ;

P ( mutexb ) ;

take the paper from box ; V ( mutexb ) ; V ( S ) ;

wnte a letter ; untile false ; end }

Coend . 2 )管程。

TYPE paper&pen = monitor VARS , S1 , S2 : condition ; box : { paper.pen , null } DEFINE put , get1 , get2 ;

USE check , wait , signal , release ; procedure put begin

Check ( IM ) ; take a material ;

if box ≠ null then wait ( S ,IM ) ; else box : = material ;

if ( box) = Pen then signal ( S1 , IM ) ; else signal (S2 , IM ) ; release ( IM ) ; end

procrdure get1 begin

check ( IM ) ;

if ( box ) = null or ( box )≠pen then wait ( S1 , IM ) ; else {take the Pen from box ; } signal ( S , IM ) ; release ( IM ) ; end

procrdure get2 begin

check ( IM ) ;

if ( box ) = null or ( box )≠paper then wait ( S2 , IM ) ; else { take the paper from box ; } Signal ( S ,IM ); release ( IM ) ; end begin

box : = null ; end

cobegin

Process 保管员 begin

LI : Callp paper&Pen.put); goto L1 end

Process A 组学生 begin {

L2 : call paper&pen.get ( )

写信;

goto L2 ; end

process B 组学生 begin

L3 : call paper&pen.get ( ) 写信; goto L3 ; end coend

47 进程A 向缓冲区buffer发消息,每当发出一消息后,要等待进程B 、C 、D 都接收这条消息后,进程A 才能发新消息。试写出:( l )用信号量和P 、v 操作,( 2 )monitor ,写出它们同步工作的程序。 答:( l )用信号量和P 、v 操作。

本质上是一个生产者与三个消费者问题。缓冲区buffer 只要写一次,但要读三次。可把buffer 看作用三个缓冲块组成的缓冲区,故sa 初值为3 。 var Sa , Sb , Sc , Sd : semaphore ; Sa : = 3 ; Sb : = Sc : = Sd : = O ; cobegin

{ process A begin repeat ; P ( Sa ) ; P ( Sa ) ; P ( Sa ) ;

Send message to buffer ; V ( Sb ) ; V ( Sc ) ; V ( Sd ) ; until false ; end

process B begin repeat P ( sb ) ;

receive the message from buffer ; V ( Sa ) ; until false ; end

Process C begin repeat P ( Sc ) ;

receive the message from buffer ; V ( Sa ) ;

until false ; end

process D begin repeat P ( Sd ) ;

receive the message from buffer ; V ( Sa ) ; until false ; end } coend

( 2 ) monitor ·

TYPE send&receive=monitor

VAR SSb , SSc , SSd , Sb , Sc , Sd : selnaphore ; SSb_count , SSc_pount , SSd_count : integer; Sb_count , Sc_count , Sd_count :integer; fiagb , fiagc , fiagd : Boolean ; buffer : message ;

DEFINE sendmes receiveb receivec received ; USE wait , signal ; procedure sendmes begin

if flagb then wait ( sb , Sb_count ,IM); if flagc then wait ( Sc , Sc_count , IM ) ; if flagd then wait ( Sd , Sd_count , IM ) ; buffer :=message ;

flagb : =flagc : =flagd : =true ; signal ( SSb , SSb_count , IM ) ; signal ( SSc , SSc_count , IM ) ; signal ( SSd , SSd_count , lM ) ; end

procedure receiveb begin

if flagb = false then wait ( SSb , SSb_count , IM ) ; else flagb : = false ;

signal ( Sb , Sb_count , IM ) ; end

procedure receivec begin

if flagc = false then wait ( SSc , SSc_count , IM ) ; else flagb : = false ;

signal ( Sc , Sc_count , IM ) ; release ( IM ) ;