用信号量进程同步与互斥

1.理解生产者和消费者问题

没有引入信号量时的生产者和消费者进程,什么情况下会出现结果不唯一?什么情况下会出现永远等待?

因为进程之间共享变量count,在生产者和消费者进程中对count的交替操作会使结果不唯一。

当生产者和消费者进程的交替执行会导致进程永远等待。

用信号解决生产者和消费者的同步与互斥,要求能自己写出来。

生存者

item B[k] ;
seamphore empty;      
emptuy=k ;
seamphore full ; //缓冲区可用产品数 full=0//缓冲区内没有产品
seamphore mutex=1 ;    //互斥信号 int in=0,out=0 ; //缓冲区放入和取出指针
cobegin process producer_i(){ while(true){ produce(); P(empty) ;
P(mutex) ; append to B[in] ;
in=(in + 1) %k ;
       V(mutex) ; V(full) ; } }
coend

消费者

process consumer_i(){
    whiletrue){
     P(full);
     P(mutex);
     take from B[out] ;
    out=( out + 1) % k ;
    V(mutex );
    V(empty) ;
    consume() ;
    }  
}

2.哲学家吃面问题

 

semaphore fork[5];
for(int i-0;i<5;i++)
    fore[i]=1;
cobegin
process philosopher_i(){
    while(true){
        thnk();
        P(fork[i]);
        P(fork[(i+1)%5]);
        eat();
        V(fork[i]);
        V(fork[(i+1)%5]);     
}           

 

3.读写文件问题

读者问题

int readcount=0 ;
semaphore writeblock , mutex ;
writeblock =1 ; mutex=1 ;
cobegin
process reader_i(){
P(mutex) ;
readcount++ ;
if(readcount==1)
P(writeblock) ;
V(mutex) ;
P(mutex) ;
readcount-- ;
if(readcount===0)
V(writeblock) ;
V(mutex) ;
}
coend
process writer_i(){
    P(writeblock) ;
    V(writeblock) ;
}

4.理发师问题

int waiting = 0 ;                    //等候理发的顾客人数
int CHAIRS = N ;                  //为顾客准备的椅子数
seamphore customers , barbers , mutex ;
 customers =0 ;barbers=0 ; mutex=1;
cobegin
     process barbers(){
    while(true){
         P(customers) ;//判断有无顾客,无顾客理发师就睡眠
         P(mutex) ;     //有顾客,进入临界区
         waiting-- ;     //等候顾客数减1
         V(barbers) ;  //理发师准备为顾客理发
         V(mutex) ;   //推出临界区
         cuthair() ;     //理发师准备理发
          }
    }
      process customer_i(){
        P(mutex)  ;      //进入临界区
        If( waiting<CHAIRS) {     //判断是否有空椅子
           waiting++ ;                 //等候顾客数加1
           V(customers)  ;            //唤醒理发师
           V(mutex) ;                  //退出临界区
           P(barbers) ;                //理发师忙,顾客坐着等待
           get_haircut() ;            //否则,顾客理发
       }
       elxe V(mutex) ;                //人满了,顾客离开
    }

 

5.在一间酒吧里有三个音乐爱好者队列,第一队的音乐爱好者只有随身听,第二队只有音乐磁带,第三队只有电池。而要听音乐就必须随身听、音乐磁带和电池这三种物品俱全。酒吧老板一次出售这三种物品中的任意两种。当一名音乐爱好者得到这三种物品并听完一首乐曲后,酒吧老板才能再一次出售这三种物品中的任意两种。于是第二名音乐爱好者得到这三种物品,并开始听乐曲。全部买卖就这样进行下去。试用P,v操作正确解决这一买卖。

process 酒吧老板
begin 
    repeat
        取两样物品出售
    until false
end

process 第一队
begin
    repeat
        购音乐磁带和电池
        装备部件
        听歌一曲
     until false 

process 第二队
begin
    repeat
    购随身听
    装备部件
    听歌一曲
    until false

process 第三队
begin 
    repeat
    购随身听和音乐磁带
    装备部件
    听歌一曲
    until false

 

 

6.某银行有人民币储蓄业务,由n个储蓄员负责。每个顾客进入银行后先取一个号,并且等着叫号。当一个储蓄人员空闲下来,就叫下一个号。请用P,V操作正确编写储蓄人员和顾客进程的程序。

semaphore mutex=A, customer_count=0:
Cobegin
Customeri(){
    p(mutex);    //取号码,进入队列
    v(mutex);
    v(customer_count);
}
serversi(){
    while(A){
        p(customer_count);
        p(mutex);            //从队列中取下一个号码
        v(mutex);            //为该号码持有者服务;
    }
}
Coend        

 

7.下面是两个并发执行的进程。它们能正确运行吗?若不能请举例说明,并改正之。(5分)

parbegin

    var X:integer;

    process  P1                    process  P2

    var y,z:integer:            var t,u:integer;

     begin                          begin

       x:=1;                           x:=0:

       y:=0:                           t=0;   

       if  x≥l  then y:=y十1;    if  x≤l  then  t:=t+2;

            z:=y;                           u:=t;

     end;                         end;

parend.

 

8.九、在一个盒子里,混装了相等数量的黑棋子和白棋子,现要用自动分拣系统把黑棋子和白棋子分开,该系统由两个并发执行的进程P1和P2组成,其中进程P1专门拣黑子,进程P2专门拣白子。规定两个进程轮流拣子且每个进程每次只拣一个子。当一个进程在拣子时不允许另一个进程去拣子,并设P1先拣。请用P,V操作管理这两个并发进程,使其能正确实现上述功能。

semaphore S1,S2;
S1=1;S2=0;
cobegin
      process P1(){
            begin
            repeat
            P(S1);
            拣白子
            V(S2);
            until false;
           end
        }
      process P2(){
           begin
           repeat
           P(S2);
           拣黑子
           V(S1);
           until false;
           end
       }
coend.

 

posted on 2019-04-30 17:48  施伟康  阅读(132)  评论(0编辑  收藏  举报

导航