用户信号量及其PV操作处理实际问题

P187-43

Cemaphore

empty=1;橘子精=0;糖=0;水=0;

Process product(){

While(true){

P(empty);

产生一个随机数s;

If(s==0) V(橘子精);

If(s==1) V(水);

If(s==2) V(糖);

  }

}

Process P1(){      | Process P2(){      | Process P3(){

While(true){        While(true){        While(true){

P(橘子精);        P(糖);             P(水);

取走橘子精;       取走糖;           取走水;

V(empty);        V(empty);            V(empty);

    }             }             }

  }             }            }

 

Coend

2.IPO问题:有多个输入进程、多个处理进程和多个输出进程。输入进程把数据逐步输入到一个有M个单位缓冲区B1上,经处理进程处理之后放到有N个单位的缓冲区B2上,由输出进程进行输出。

        a.这个问题有哪些进程?进程之间有什么样的制约关系?

        b.用信号量及PV操作写出这些进程之间的同步算法

解:

   a:有输入进程、处理进程、输出进程,进程之间有同步关系。

   b:     semaphore B1;B1=M;        /*B1可用的空缓存区*/

           semaphore B2;B2=N;        /*B2可用的空缓存区*/

           semaphore mutex;mutex=1;           /*同步信号*/

           cobegin

                  process input(){

                          while(true){

                                p(mutex);

                  输入;

                  B1--;

                                v(mutex);

                           }

                    }

                 process chuli(){

                        while(true){

                                p(mutex);

                  处理;

                  B1++;

                                 B2--;

                                v(mutex);

                          }

                    }

                 process output(){

                          while(true){

                                p(mutex);

                  输出;

                  B2++;

                                v(mutex);

                           }

                    } 

  coend

posted @ 2019-05-10 11:07  陈振铭  阅读(170)  评论(0编辑  收藏  举报