1.书上课后练习P187-43

semaphore 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(){

 

  While(true){ 

    P(橘子精);

    取走橘子精;

     V(empty); 

  }

}

Process P2(){

  while(true){ 

     P(糖);

    取走糖;

    V(empty); 

  }

}

Process P3(){

  while(true){

     P(水);   

    取走水;

    V(empty);

  }

Coend

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

  1. 这个问题有哪些进程?进程之间有什么样的制约关系?
  2. 用信号量及PV操作写出这些进程之间的同步算法。

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

 2:     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       

 

3.探索哲学家问题的正确解法。

posted on 2019-05-10 15:15  baijjjy  阅读(134)  评论(0)    收藏  举报