v(test); end v(enter); v(computer); end end coend
十七真经之3进程问题
问题描述:
设有A、B、C三组进程, 它们互斥地使用某一独占型资源R, 使用前申请, 使用后释放.
资源分配原则如下:
k 当只有一组申请进程时, 该组申请进程依次获得R;
k 当有两组申请进程时, 各组申请进程交替获得R, 组内申请进程交替获得R; k 当有三组申请进程时, 各组申请进程轮流获得R, 组内申请进程交替获得R. 试用信号灯和PV操作分别给出各组进程的申请活动程序段和释放活动程序段.
The P,V code Using Pascal
Int Free=1;//设备状态标志 semaphore mutex=1;
semaphore qa=qb=qc=0; //各组等待队列 Int counta=countb=countc= 0;//等待队列长度 A组申请: P(mutex);
if Free==1 then begin Free=0; V(mutex); end else begin counta++; V(mutex); P(qa); end
A组释放: if countb>0 then begin countb--; V(qb); end else begin
if countc>0 then begin countc--;
CHAPTER 3. 九阴真经之研究生题辑42
V(qc); end else begin
if (counta > 0) begin counta--; V(qa); end else begin Free=1; end end end
A组进程活动可以给出B组和C组进程活动。
_思考(南大1997):
今有三个并发进程R,M,P,它们共享了一个可循环使用的缓冲区B,缓冲区B共有N个单元。进程R负责从输入设备读信息,每读一个字符后,把它存放在缓冲区B的一个单元中;进程M负责处理读入的字符,若发现读入的字符中有空格符,则把它改成“,”;进程P负责把处理后的字符取出并打印输出。当缓冲区单元中的字符被进程P取出后,则又可用来存放下一次读入的字符。请用PV操作为同步机制写出它们能正确并发执行的程序。
The P,V code Using Pascal
var empty,full1,full2,mutex:semaphore; char buffer[n];
int in=0,out1=0,out2=0; empty=N; full1=0; full2=0; mutex=1; cobegin
procedure R; procedure M; procedure P; coend
procedure R: procedure M: procedure P: begin begin begin
while True then while True then while True then begin begin begin char x; char x; char x;
Read a char to x; p(full1); p(full2);
CHAPTER 3. 九阴真经之研究生题辑43
p(empty); p(mutex); p(mutex);
p(mutex); x=buffer[out1]; x=buffer[out2]; buffer[in]=x; if x==\in=(in+1)%N; begin v(mutex); v(mutex); x=\
v(full1); buffer[out1]=x; 输出字符x; end end end
end out1=(out1+1)%N; end v(mutex); v(full2); end end
_思考(北大1996):
有P1、P2、P3三个进程共享一个表格F,P1对F只读不写,P2对F只写不读,P3对F先读后写。进程可同时读F,但有进程写时,其他进程不能读和写。用(1)信号量和P、V操作。(2)管程编写三进程能正确工作的程序。
The P,V code Using Pascal
答:(1)信号量和P、V操作。这是读--写者问题的变种。 其中,P3既是读者又是写者。读者与写者之间需要互斥, 写者与写者之间需要互斥,为提高进程运行的并发性, 可让读者尽量优先。
var rmutex,wmutex:semaphore; rmutex:=wmutex:=1; count:integer;count:=0; cobegin {
process P1 begin repeat P(rmutex);
count:=count+1;
if count=1 then P(wmutex); V(rmutex); Read F; P(rmutex);
count:=count-1;
if count=0 then V(wmutex); V(rmutex); untile false; end
process P2 begin repeat
CHAPTER 3. 九阴真经之研究生题辑44
P(wmutex); Write F; V(wmutex); untile false; process P3 begin repeat P(rmutex);
count:=count+1;
if count=1 then P(wmutex); V(rmutex); Read F; P(rmutex);
count:=count-1;
if count=0 then V(wmutex); V(rmutex); P(wmutex); Write F; V(wmutex); untile false; end } coend
_思考(国防科技大学1996):
如图所示,四个进程Pi(i=0?3)和四个信箱Mj(j=0?3),进程间借助相邻信箱传递消息,即Pi每次从Mi中取一条消息,经加工后送入M(i+1)mod4,其中M0、M1、M2、M3分别可存放3、3、2、2个消息。初始状态下,M0装了三条消息,其余为空。试以PV操作为工具,写出Pi(i=0?3)的同步工作算法。
The P,V code Using Pascal
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
CHAPTER 3. 九阴真经之研究生题辑45
process P0 process P1 begin begin repeat repeat P(full0); p(full);
P(mutex0); p(mutex1);
从M0[out0]取一条消息; Get a message from M[out1]; out0:=(out0+1)mod 3; out1:=(out1+1)mod 3; V(mutex0); v(mutex1); V(empty0); v(empty1); 加工消息; 加工消息; P(empty1); p(empty2); P(mutex1); p(mutex2);
消息已M1[in1]; 消息己M2[in2];
In1:=(in1+1) mod 3; In2:=(in2+1)mod 2; V(mutex1); V(mutex2); V(full1); v(full2);
untile false; until false; end end
process P2 process P3 begin begin repeat repeat P(full2); p(full3);
P(mutex2); P(mutex3);
从M2[out2]取一条消息; 从M3[out3] 取一条消息; out2:=(out2+l)mod 2; out3:=(out3+1)mod 2; V(mutex2); v(mutex3); V(empty2); v(empty3); 加工消息; 加工消息; P(empty3); p(empty0); P(mutex3); p(mutex0);
消息己M3[in3]; 消息己MO[in0];
in3:=(in3+1)mod 2 ; In0:=(in0+1)mod 3; V(mutex3); v(mutex0); V(full3); v(full0);
untile false ; until false; end end coend
_思考:
四个进程A、B、C、D都要读一个共享文件F,系统允许多个进程同时读文件F。但限制是进程A和进程C不能同时读文件F,进程B和进程D 也不能同时读文件F。为了使这四个进程并发执行时能按系统要求使用文件,现用PV操作进行管理,请回答下面的问题: