用信号量进程同步与互斥

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

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

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

 答:生产者进程和消费者进程对counter的交替操作会使其结果不唯一。

   生产者进程和消费者进程的交替执行会导致进程永远等待,造成系统死锁。

 

          Int k;

 

         Typedef anyitem item;

 

         Item buffer[k];

 

         Int in=0,out=,counter=0;

 

         Process producer(void){

 

        While(true){

 

        {produce an item in nextp};

 

        If(counter==k)

 

        Sleep(producer);

 

        Buffer[in]=nextp;

 

        In-(in+)%k;

 

       Counter++;

 

       If(counter==1)

 

      Wakeup(consumer);

 

     }

 

     }

 

    Process consumer(void){

 

    While(true){

 

    If(counter==0)

 

   Sleep(consumer);

 

   Nextc=buffer[out];

 

   Out=(out+1)%k;

 

   Counter__;

 

   If(counter==k-1)

 

   Wakeup(producer);

 

   {consume the item in nextc};

 

   }

 

   }

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

Var mutex=1,customer_count=0:semaphore;

cobegin

    process  customer

        begin

            repeat

            P(mutex);

           取号码,进入队列;

             V(mutex);

            V(customer_count);

             untile  false;

         end

      process  serversi(i=1...n)

          begin

              repeat

              P(customer_count);

              P(mutex);

               从队列中取下一个号码;

               V(mutex);

                为该号码持有者服务;

               untile  false;

         end

coend

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

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.

 

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(){
            begin
            repeat
            P(S1);
            拣黑子
            V(S2);
            until false;
           end
        }
      process P2(){
           begin
           repeat
           P(S2);
           拣白子
           V(S1);
           until false;
           end
       }
coend.

 

posted @ 2019-05-06 15:14  陈振铭  阅读(256)  评论(0编辑  收藏  举报