生产者与消费者得问题描述

来源:互联网 发布:java二级考试 编辑:程序博客网 时间:2024/05/21 17:51

生产者_消费者问题(参考资料;计算机操作系统)

1:利用记录型信号量解决生产者与消费者问题:

Var mutext,empty,full:semaphore=1,n,0;

buffer :array[0,…,n-1] ofitem;

in out :interge:=0,0;

begin

         parbegin

                   producer:begin

                                     repeat

                                     proceduceran item nextp;

                                     wait(empty);

                                     wait(mutex);

                                     buffer(in):=nextp;

                                     in=(in+1)mod n;

                                     signal(mutex);

                                     signal(full);

                                     until false;

                            end

                   consumer:begin

                                     repeat

                                     wait(full);

                                     wait(mutex);

                                     nextc:=buffer(out);

                                     out:=(out+1)mod n;

                                     signal(empty);

                                     signal(mutex);

                                     consumerthe item in nextc;

                                     until false;

                            end

parend

end

 

 

2:利用AND信号量解决生产者与消费者问题:
Var mutext,empty,full:semaphore=1,n,0;

buffer :array[0,…,n-1] ofitem;

in out :interge:=0,0;

begin

         parbegin

                   producer:begin

                                     repeat

                                     proceduceran item nextp;

                                     Swait(empty,mutex);

                           

                                     buffer(in):=nextp;

                                     in=(in+1)mod n;

                                     Ssignal(mutex.mutex);

                                     utilfalse;

                            end

                   consumer:begin

                                     repeat

                                     Swait(full,mutex);

                                     nextc:=buffer(out);

                                     out:=(out+1)mod n;

                                     Ssignal(empty,mutex);

                                     consumerthe item in nextc;

                                     until false;

                            end

parend

end

 

 

3:利用管程解决生产者与消费者问题:

(1):Pc管道可以描述为:

type producer-consumer=monitor

         Varint,out,count :integer;

         buffer:array[0,…,n-1] of item;

         notfull,notempty:condition;

         proceducerentry put(item);

                   begin

                            if(count>=n)then notfull.wait;

                            buffer(in):=nextp;

                            in:=(in+1)mod n;

                            count:=count+1;

                            ifnotempty.queue then notempty.signal;

                   end

procedure entry get(item)

         begin

                  if count<=0 then notempty.wait;

                   nextc:=buffer(out);

                   out:=(out+1)mod n;

                   count:=count-1;

                   ifnotfull.queue then notfull.signal;

end

(2):利用管程解决生产者与消费者问题是,可以描述为:

producer: begin

                   repeat

                            producean item in nextp;

                            PC.put(item);

                   utilfalse;

end

 

consumer:begin

                   repeat

                   PC.get(item);

                   Consumer the item in nextc;

         util false;

end

 

 

0 0