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

准备好桔子;

Call FMSD.put(orange); ?? } End;

Process son Begin While(1)

{call FMSD.get(orange,x); 吃取到的桔子; ?? } End;

Process daughter Begin While(1) {

Call FMSD.get(apple,x); 吃取到的苹果; ?? } End; } Coend

39 一组生产者进程和一组消费者进程共享九个缓冲区,每个缓冲区可以存放一个整数。生产者进程每次一次性向3 个缓冲区写入整数,消费者进程每次从缓冲区取出一个整数。请用:( l )信号量和P 、V操作,( 2 )管程,写出能够正确执行的程序。

答:( l )信号量和P 、V 操作。

var buf : ARRAY [ 0?8 ] of integer ; count,getptr , putptr :integer ; count:=0;getptr:=0;putptr:=0;

S1 , S2 , SPUT , SGET ; semaphore ; S1:=1 ; S2 :=1 ; SPUT : = 1 ; SGET :=0; main() { cobegin

producer-i( ) ; consumer-j( ) ; coend }

process producer-i begin

L1 :生产3 个整数; P(SPUT);

P(S1);

Buf[putptr]:=整数 1; Putptr:=(putptr+1)mod 9; Buf[putptr]:=整数2 ; putptr :=(puttr+1 ) MOD 9 buf[putptr]:=整数3 ; putptr:=(putptr+1) MOD 9; V ( SGET ) ; v ( SGET ) ; v ( SGET ) : v ( S1 ) ; goto L1 end

process consumer-j var y:integer ; begin

L2:P(SGET ) ; P (S2 ) ;

y=buf[getptr] ;

getptr:=(getptr + 1) MOD 9 ; count:=count + 1; if count= 3 then begin

count:=0; V ( SPUT ) ; end

V (S2 ) ;

consume the 整数 y; goto L2 ; end

( 2 )管程。

TYPE get-put = MONITOR

VAR buf ARRAY [ 0 ?8] of integer ; count , getptr , putptr:integer ; SP , SG ; codition DEFINE put,get ;

USE wait ,signal , check , release ;

Procedure put(var a1 , a2 , a3 :integer ; ) ; begin

check(IM);

if ( coun>6)then wait(SP , IM ) ; count:count + 3 ; buf[putptr]:=a1 ;

putptr(put1+1 ) MOD 9; buf [putptr]:=a2;

putptr:=(putptr+1) MOD 9 ; buf[putptr]:=a3;

putptr:=(putptr+1) MOD 9 ; signal(SG,IM); release(IM ) ; end ;

procedure get (b); begin

check(IM);

if ( count==0)then wait(SG,IM ) ; b:buf[getptr] ;

getptr:=(getptr + 1 ) MOD 9 ; count :=count + 1 ;

if count < 7 then signal ( SG,IM ) ; else if count > 0 then signal ( SG,IM ) ; release ( IM ) ; end; begin

count:=0;getptr:=0;putptr:=0; SP:=0;SG:=0; End;

cobegin {

process producer-i begin

L1 :生产3 个整数;

Call get-put.put(a1, a2 , a3 ) ; goto L1 end

process consumr-j var y:integer ; begin

L2 : call get-put.get(b) consume the 整数b ; goto L2; end } coend

40 设有三个进程P 、Q 、R 共享一个缓冲区,P 进程负责循环地从磁带机读入一批数据并放入缓冲区,Q 进程负责循环地从缓冲区取出P 进程放入的数据进行加工处理并把结果放入缓冲区,R 进程负责循环地从缓冲区读出Q 进程放入的数据并在打印机上打出。请用:( 1 )信号量和P 、v 操作,( 2 )管程,写出能够正确执行的程序.

答:( 1 )信号量和P 、v 操作

var Sp , Sq , Sr : semaphore; Buf :integer; SP:=1;SP:=Sr:=0; Cobegin {process P Begin Repeat

从磁带读入数据; P(SP);

Buf:=data; V(sq);

Until false; End

Process Q Begin Repeat P(sq);

Data:=buf; 加工处理data; Buf:=data; V(Sr);

Until false; End

Process R Begin Repeat P(Sr);

Data:=buf; V(sp); 打印数据

Until false; End }

(2)管程

TYPE PQR=MONITOR VAR buf:integer; SP,SQ,SR:codition; Turn:{p,q,r};

DEFINE PPUT,QGET,QPUT,RGET; USE wait,signal,check,release; Procedure PPUT(var data:integer;); Begin

Check(IM);

If turn!=p then wait (sp,IM);

Turn:=q; Buf:=data;

Signal(SQ,IM); Release(IM); End

Process QGET(var data:integer;); Begin

Check(IM);

If turn !=q then wait(SQ,IM) Data:buf

Release(IM); End

Procedure QPUT(var data:integer;); Begin

Check(IM); Turn:=r; Buf:=data;

Signal(SR,IM); Release(IM); End

Procedure RGET(var data:integer;); Begin

Check(IM);

If turn !=r then wait(SR,IM); Turn:=p; Data:buf

Signal(SP,IM); Release(IM); End Begin

Sp:=0;SQ:=0;SR:=0;turn:=p; End Main() {cobegin Process P X:=integer; Begin

LP:从文件读入一个数据到X; PPUT(X); Goto LP; End

Process Q X:integer; Begin

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