免费注册 查看新帖 |

Chinaunix

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

PERL共享内存的问题 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2010-03-25 17:28 |只看该作者 |倒序浏览
需求是这样的,使用fork创建10个子进程,每个子进程都去取得不同的值,然后将10个值相加得到最终的需求值,于是将10个值都传到数组里面,按照下面的代码写,却无论如何都只能得到一个值,请各位高手帮忙看看
  1. use Data::Dumper;
  2. use IPC::ShareLite;
  3. use Storable qw(freeze thaw);

  4.     my $share = IPC::ShareLite->new(
  5.         -key     => 1971,
  6.         -create  => 'yes',
  7.         -destroy => 'no'
  8.     ) or die $!;

  9.     my @child;
  10.     my @str;
  11. for (my $i = 0;$i <= 10; $i++) {
  12.     my $pid = fork();
  13.     if ($pid) {
  14.         push (@child,$pid);
  15.     }
  16.     else {
  17.         push (@str,$i);
  18.         $share->store(@str);
  19.         exit 0;
  20.     }

  21. }

  22. foreach my $pid (@child) {
  23.         my $tmp = waitpid($pid,0);
  24. }

  25. @str = $share->fetch;

  26. print Dumper(@str);
复制代码

论坛徽章:
0
2 [报告]
发表于 2010-03-25 17:44 |只看该作者
本帖最后由 nuclearxin 于 2010-03-25 18:09 编辑

不懂fork
fork是什么意思啊?

论坛徽章:
0
3 [报告]
发表于 2010-03-25 18:17 |只看该作者
不懂fork
fork是什么意思啊?
nuclearxin 发表于 2010-03-25 17:44



    fork是进行进程复制的,多进程一般会用到fork

论坛徽章:
0
4 [报告]
发表于 2010-03-26 14:20 |只看该作者
没人解答一下吗?

论坛徽章:
0
5 [报告]
发表于 2010-03-26 18:10 |只看该作者
回复 4# dqchen0004

store( $scalar )
This method stores $scalar into shared memory. $scalar may be arbitrarily long. Shared memory segments are acquired and released automatically as the data length changes. The only limits on the amount of data are the system-wide limits on shared memory pages (SHMALL) and segments (SHMMNI) as compiled into the kernel.

The method raises an exception on error.

Note that unlike IPC::Shareable, this module does not automatically allow references to be stored. Serializing all data is expensive, and is not always necessary. If you need to store a reference, you should employ the Storable module yourself. For example:

    use Storable qw( freeze thaw );
    ...
        $hash = { red => 1, white => 1, blue => 1 };
    $share->store( freeze( $hash ) );
    ...
    $hash = thaw( $share->fetch );

论坛徽章:
0
6 [报告]
发表于 2010-03-26 18:14 |只看该作者
回复 5# toniz
  1. use Data::Dumper;
  2. use IPC::ShareLite;
  3. use Storable qw(freeze thaw);
  4. use IPC::ShareLite qw( :lock );

  5. my $share = IPC::ShareLite->new(
  6.   -key   => 1971,
  7.   -create  => 'yes',
  8.   -destroy => 'no'
  9. ) or die $!;
  10.   my @child;
  11.   my @aa ;
  12.         $share->store(freeze(\@aa));
  13. for (my $i = 0;$i <= 10; $i++) {
  14.   my $pid = fork();
  15.   if ($pid) {
  16.     push (@child,$pid);
  17.   }
  18.   else {
  19.     $share->lock( LOCK_EX);
  20.     $str = thaw( $share->fetch);
  21.     push @{$str},$i;
  22.     $share->store(freeze($str));
  23.     $share->unlock;
  24.     exit 0;
  25.   }
  26. }
  27. foreach my $pid (@child) {
  28.     my $tmp = waitpid($pid,0);
  29. }
  30. $str1 = thaw( $share->fetch);
  31. print Dumper @{$str1};
复制代码
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP