首页 > 系统服务 > 详细

用信号量进程同步与互斥

时间:2019-05-05 20:08:12      阅读:151      评论:0      收藏:0      [点我收藏+]

1.并发执行,生产者和消费者进程交替执行,共享变量没有加访问限制,产生结果不一。

   生产者和消费者进程的交替执行,造成死锁,唤醒信号被丢失掉。

  

  int k;
  typedef anyitem item;
  item buffer[k];
  int i=0,o=0,c=0;
  process prodicer(){
 produce an item in nextp;
if(c==k)
       sleep(producer);
      buffer[i[=nextp;
      i=(i+1)%k;
     counter++;
  if(counter==1)
   wakeup(consumer);
}
 process consumer(){
   while(true){
   if(counter==0)
      sleep(consumer);
      nextc=buffer[o];
     o=(o+1)%k;
     counter--;
    if(counter==k-1)
             wakeup(producer);
   }
   consume the item in nextc;
}

 

2.哲学家吃面问题

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

 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(readblock);
  V(mutex);
}

4.理发师问题

int waiting=0, chairs=n;
semaphore customers=0,barbers=0,mutex=1;
cobegin
     process barbers() {
           while(ture) {
                P(customers);
                P(mutex);
                waiting--;
                V(barbers); 
                V(mutex);
                cuthair();  } }
process customer_i() {
               P(mutex);
               if(waiting<chairs) {
                     waiting++;
                      V(customers);
                       V(mutex);
                       P(barbers):
                       get_haircut();
}
else
        V(mutex);
}
coend

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

semaphore mutex=1,s1=0,s2=0,s3=0;
cobegin
    fan1(){
        s1=1;
       P(s2);
       P(s3);
      if(s1&&s2&&s3){
        V(mutex);
V(s1);
V(s2);
V(s3);
} } fan2(){ s2
=1; P(s1); P(s3); if(s1&&s2&&s3){ V(mutex);
    V(s1);
        V(s2);
        V(s3);} } fan3(){ s3
=1; P(s1); P(s2); if(s1&&s2&&s3){ V(mutex);
    V(s1);
        V(s2);
        V(s3);} } } coend

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

semaphore customers=0,mutex=n,check=0;
int waiting=0;
cobegin
     process check(){
      while(true)
        {
          P(customers);
          P(mutex);
          waiting--;
          V(check);
          V(mutex);
          }
    }
process customer_i() {
           P(mutex);
           waiting++;
           V(customers);
           V(mutex);
           P(clerk):
           get_service();
}
coend

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

parbegin
var x:integer; var s:semaphore:=1;
process P1                                   process P2
       var y,z:integer ;                             var ,t,u:integer ;
begin                                           begin
      P(s);                                           P(s);
      x:=1;                                         x:=0;
      y:=0;                                         t:=0;
        if x>=1 then y:=y+1;                   if x<=1 then t:=t+2
      V(s);                                            V(s);
         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(){
            P(S1);
            V(S2);
        }
      process P2(){
           P(S2);
           V(S1);
       }
coend

 

用信号量进程同步与互斥

原文:https://www.cnblogs.com/pigs29/p/10815556.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!