免费注册 查看新帖 |

Chinaunix

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

perl多进程出错,求解 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2012-08-08 15:56 |只看该作者 |倒序浏览
本帖最后由 kingwmj 于 2012-08-08 16:02 编辑

写了一个程序,因为太慢,所以改用多进程,但是与我的原来的不分进程的程序结果不一致,最大的可能是多个进程互相修改某个变量.但我没有找到,求高手给看一下.
  1.         sub step_3_jaccard_distance_cal(){
  2.                 #格式:+1        P08758        Human        0        0        0        0        0        0        0        0        0        0        0        0        0        1
  3.                 my $in_file=shift;
  4.                 my $in= new IO::File($in_file) or die  $!;
  5.                 my $out_file=shift;
  6.                 my $out = new IO::File(">$out_file") or die $!;
  7.                 my $len=0;
  8.                 seek $in,0,0;
  9.                 print "distance of r and s\n";
  10.                 my @matrix=<$in>;
  11.                 $len=my @list=split/\s+/,$matrix[0];
  12.                 print "$len\n";
  13.                 #以下为多进程----------------------------------------------------------------------------------------
  14.                 my @child;
  15.                          $|++;
  16.                   print "good\n";
  17.                  # my $R;       
  18.                   my $num_proc = 0;
  19.                   ## == number of collected ==
  20.                   my $num_collect = 0;
  21.                   my $collect;
  22.                   ## == get the child signal ==
  23.                   $SIG{CHLD} = sub { $num_proc--};
  24.                
  25.                
  26.                 for(my $r=0;$r<=$len-4;$r++){  #
  27.                         #my @sum=();
  28.                         #$sum[$r]=0;
  29.                         print "r=$r\n";
  30.                         my $pid=fork();   #分多进程
  31.                         push(@child,$pid);
  32.                         if(!defined($pid)){
  33.                                      print "Error in fork: $!";
  34.                                 exit 1;
  35.                         }
  36.                         if($pid==0){    #子程序
  37.                                 my @sum=();
  38.                                 $sum[$r]=0;
  39.                                
  40.                                 for(my $s=0;$s<=$len-4;$s++){  
  41.                                         print "s= $s\n";
  42.                                         my $l_up;  #这个是hs_up共有多少个键 #这个是分子
  43.                                         my $l_dn;  #这个是hs_dn共有多少个键 #这个是分母
  44.                                         my %hs_up=();  
  45.                                         my %hs_dn=();  
  46.                                         my @d=();
  47.                                         foreach my $line (@matrix){
  48.                                                 my @data=split/\s+/,$line;
  49.                                                 if(($data[3+$r]!=$data[3+$s]) and (($data[3+$r]==1) or ($data[3+$s]==1)))        {  #注意,这里是从3开始,把输入文件整理成前三列是标记
  50.                                                         $hs_up{$data[1]}=2;  #这里data[1] 是UNIPROT ID
  51.                                                 }
  52.                                                 if(($data[3+$r]==1) or ($data[3+$s]==1)){
  53.                                                         $hs_dn{$data[1]}=2;
  54.                                                 }
  55.                                         }

  56.                                         $l_up=keys %hs_up; #
  57.                                         $l_dn=keys %hs_dn;

  58.                                         if($l_dn==0){
  59.                                                 print "r=$r, s=$s\n";
  60.                                                 die ;  #
  61.                                                
  62.                                         }
  63.                                         else{
  64.                                                 $d[$r][$s]=$l_up/$l_dn;
  65.                                                 #print $out "$d[$r][$s]        "
  66.                                         }
  67.                                         $sum[$r]+=$d[$r][$s];
  68.                                         #print $out "$sum[$r]        ";
  69.                                 }
  70.                                 print $out "$sum[$r]        ";
  71.                                 exit 0;         #very important. 运行到他,这个子进程就结束了.所以要放到子进程的最后.
  72.                         }
  73.                         $num_proc ++;
  74.                         ## == if need to collect zombies ==
  75.                         if (($r-$num_proc-$num_collect) > 0) {  
  76.                                 while (($collect = waitpid(-1, WNOHANG)) > 0) {
  77.                                                 $num_collect ++;
  78.                                 }       
  79.                         }
  80.                         while($num_proc > 1000){     #   控制子进程数
  81.                                         sleep(3);
  82.                                         print "stop\n";     
  83.                         }
  84.                        
  85.                        
  86.                 }
  87.                 print $out "\n";
  88.                 foreach (@child){      #  回收所有进程
  89.                         waitpid($_, 0);
  90.                 }
  91.                 $in->close;
  92.                 $out->close;
  93.         }
复制代码
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP