用信号量及其PV操作处理实际问题
1.书上课后练习P187-43
semaphore mutex , sweat , water ,orange ; semaphore empty; process Supply(){ while(1){ P(empty) ; 产生一个0-2的随机数 ; if(s==0) V(sweat) ; else if( s==1) V(water); else V(orange) ; } } process P1(){ while(1){ P(orange) ; P(mutex) ; V(mutex) ; V(empty) ; } } process P2(){ while(1){ P(sweat) ; P(mutex) ; V(mutex) ; V(empty) ; } } process P3(){ while(1){ P(water) ; P(mutex) ; V(mutex) ; V(empty) ; } }
2.IPO问题:有多个输入进程、多个处理进程和多个输出进程。输入进程把数据逐步输入到一个有M个单位缓冲区B1上,经处理进程处理之后放到有N个单位的缓冲区B2上,由输出进程进行输出。
- 这个问题有哪些进程?进程之间有什么样的制约关系?
- 用信号量及PV操作写出这些进程之间的同步算法。
1.这个问题有三个进程,分别是输入进程,处理进程和输出进程。处理进程需要输入进程输入完毕以后才能对数据进行处理,输出进程要等处理进程处理完以后才能输出进程。
2.
S1=S2=S3=S4=0; mutex1=1;mutex2=1; process input begin 输入数据; P(S1); P(mutex1); 数据放入B1(in1); in1=(in+1)%M; V(mutex1); V(S2); end process handle begin P(S2); P(mutex1); 从B1(out1)取出数据; out1=(out+1)%M; V(mutex1); V(S1); 处理数据; P(S3); P(mutex2); 数据放入B2(in2); in2=(in2+1)%N; V(mutex2); V(S4); end process output begin P(S4); P(mutex2); 从B2(out2)取出数据; out2=(out2+1)%N; V(mutex2); V(S3); 输出数据; end
3.探索哲学家问题的正确解法