嗜睡的理发师问题

来源:互联网 发布:程序员的浏览器 编辑:程序博客网 时间:2024/04/29 02:09

var count:integer:=0;

mutex,sofa,empty,full:=semaphore:=1,N,1,0;

cut,payment,receipt:semaphore:=0,0,0;

begin

          parbegin

                            guest:begin

                                                       wait(mutex);

                                                       if(count>N) then   

                                                       begin

                                                       signal(mutex);

                                                       离开理发店;

                                                       end

                                                       else

                                                      begin

                                                                 count:=count+1;

                                                                  if(count>1) then

                                                     begin

                                                                 wait(sofa);

                                                                 在沙发上就座;

                                                                wait(empty);

                                                                 从沙发上起来;

                                                                signal(sofa);

                                                    end

                                                     else

                                                                wait(empty);

                                                                  在理发椅上就座;

                                                                 signal(full);

                                                                理发;

                                                                付费:

                                                               signal(payment);

                                                              wait(receipt);

                                                              从理发椅上起来;

                                                                signal(empty);

                                                               wait(mutex);

                                                              count:=count-1;

                                                               signal(mutex);

                                                               离开理发店:

                                                        end

                                      end

                                              barber:begin

                                                           repeat

                                                             wait(full);

                                                            替顾客理发:

                                                             wait(payment);

                                                               收费;

                                                               signal(recipt);

                                                    until false

                                                 end

             parend

end

 

                                 

 

 

 

 

 

 

 

 

 

                                       

原创粉丝点击