免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 3088 | 回复: 2
打印 上一主题 下一主题

[算法] 关于生产者-消费者的两个例子,我都觉得有问题 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2008-04-01 11:43 |只看该作者 |倒序浏览
西电的操作系统书上关于p-c问题有两个例子,一个是用记录型信号量来解决,一个是用管程,抱歉,用pascal伪码编的,实在没地方问了。
记录型信号量:
Var mutex,empty,full:semaphore:=1,n,0;
      buffer:array[0,...,n-1] of item;
      in,out:integer:=0,0;
      begin
          parbegin
                  proceducer:begin
                                        repeat
                                               ...
                                        producer an item nextp;
                                               ...
                                        wait(empty);
                                        wait(mutex);
                                        buffer(in):=nextp;
                                        in:=(in+1)mod n;
                                        signal(mutex);
                                        signal(full);
                                        until false;
                                     end
                  consumer:   begin
                                        repeat
                                        walit(full);
                                        wait(mutex);
                                        nextc:=buffer(out);
                                        out:=(out+1)mod n;
                                        signal(mutex);
                                        signal(empty);
                                        consumer the item in nextc;
                                        until false;
                                     end
              parend
        end
管程:
type producer-consumer=monitor
         Var in, out, count: integer;
             buffer: array[0,...,n-1] of item;
             notfull, notempty:condition;
             procedure entry put(item)
                       begin
                            if count>=n then notfull.wait;
                                buffer(in):=nextp;
                                in:=(in+1) mod n;
                                count:=count+1;
                                if notempty.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;
                            if notfull.queue then notfull,signal;
                      end
              begin
                    in:=out:=0;
                    count:=0;
              end

producer:begin
                      repeat
                           produce an item in nextp;
                           PC.put(item);
                      until false;
               end
consumer:begin
                      repeat
                           PC.get(item);
                           consume the item in nextc;
                      until false;
                 end

我觉得这两个例子都有问题。
第一个,wait(mutex);(mutex:=1)就等于只允许一个进程访问buffer[n]阿,如果生产者生产产品到buffer[4]的同时,消费者需要消费buffer[3],因为设置了mutex,不就无法实现了吗?但事实上是可以的阿。
第二个,居然没设mutex,我觉得这也不行,因为 buffer[n]毕竟是共享资源,需要互斥使用。
大家说我分析的对吗?如果既要使生产者和消费者在同一时间内互斥的访问buffer[ i ],又可以使同一时间内生产者在访问buffer[ i ],而消费者在访问buffer[j](i!=j),请问应该如何做到呢?

[ 本帖最后由 C文_tinker 于 2008-4-1 11:45 编辑 ]

论坛徽章:
0
2 [报告]
发表于 2008-04-02 17:23 |只看该作者
顶下

论坛徽章:
0
3 [报告]
发表于 2008-04-03 19:06 |只看该作者
再顶下
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP