操作系统习题答案第(3)

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;

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

Process the products ; P ( emPty2) ;

P ( mutex2 ) ;

put an item into buffer2 [ in2 ] ; in2:=( in2 + l ) mod M2 ; counter2 + + ;

if counter2 = 3 then { counter2:=0 ;V( full2 ) ; } V ( mutex2) ; goto L2 ; process Rk begin L3 :

P ( full2 ) ; P ( mutex2 ) ;

take an item from buffer2 [out2]; out2: = ( out2 + 1 ) mod M2 ; take an item from buffer2 [out2] ; out2:=( out2 + 1) mod M2 ; take an item from buffer2 [out2]; out2:=(out2 + 1 ) mod M2 ; v ( mutex2 ) ; V ( empty2 ) ; V ( empty2 ) ; V ( empty2 ) ;

packet the products ; goto L3 ; end } coend

20 在一个实时系统中,有两个进程P 和Q ,它们循环工作。P 每隔1 秒由脉冲寄存器获得输入,并把它累计到整型变量W 上,同时清除脉冲寄存器。Q 每隔1 小时输出这个整型变量的内容并将它复位。系统提供了标准例程创PUT 和OUT 卫UT 供拍,提供了延时系统调用Delay ( seconds )。试写出两个并发进程循环工作的算法。 答:

Var W ,V:integer; Mutex:semaphore; W:=0 ; V:=0 ;mutex:1; cobegin { process P begin repeat P(mutex) ; delay (1) ; V=INPUT ; W:=W + V ; 清除脉冲寄存器;

V (mutex) ; untile false ; end

process Q begin repeat

P ( mutex ) ; delay ( 60 ) ; OUTPUT ( W ) ; W : = 0 ; V ( mutex ) ; untile false ; }

coend .

21 系统有同类资源m 个,被n 个进程共享,问:当m > n 和m≤n 时,每个进程最多可以请求多少个这类资源时,使系统一定不会发生死锁?

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