2013-05-14 86 views
4

我已经写了一个脚本,其中包括并行生成gcov。我成功了,但它一次创建了17个子进程。但是我一次只创建6个子进程,第7个子进程应该在1child进程终止。如何限制perl中的子进程?

sub gcov_parallel() 
    2 { 
    3  print "Generating Gcov...\n"; 
    4  my $kid; 
    5  my $pid; 
    6  @list = (@iucall,@iurcall_init,@iurcall_term,@iurcall_uti,@nob,@nobcch,@nobcell,@nobrrc,@nobcall,@rnccall,@cellin 
    fo,@rnccom,@cellrrm,@uerrm,@uerrc,@uecall,@iupcded); 
    7  my $len_list = scalar(@list); 
    8  my $maxlen =0; 
    9  my $count = 0; 
10  my $process = 0; 
11  $total_components = scalar(@comp_list); 
12 
13  for(my $comp_count=0; $comp_count < $len_list ; ($comp_count=$comp_count+$no_of_machines)) 
14  { 
15   #limiting child process to 6 
16   if($process == 6) 
17   { 
18    $pid = wait(); 
19    $process=$process-1; 
20   } 
21   else 
22   { 
23    $pid = fork(); 
24    if($pid eq 0) 
25    { 
26     for(my $files_count = 0; $files_count < $no_of_machines; $files_count++) 
27     { 
28      $count = $files_count+$comp_count; 
29      if($count < $len_list) 
30      { 
31       chomp($list[$count]); 
32       my @list_gcda =`ls $list[$count]/*.gcda | sort`; 
&generate_gcov("$list[$count]",@list_gcda); 
34      } 
35     } 
36     wait(); 
37     exit; 
38    } 
39    $process=$process+1; 
40   } 
41  } 
42  do 
43  { 
44   $kid = waitpid(-1, 0); 
45  }while $kid > 0; 
46 } 

But i observed while running the script it is skipping files while generating gcov.               
+0

列表1中包含..成分的阵列@ iucall,@ uerrc等......每个组件阵列包含files.for第1 6个部件工作正常,但之后,这是跳过备用组件 – Nethra 2013-05-14 08:48:54

+0

您在第36行中的wait()调用看起来很可疑:第26-35行构成了子进程的代码,对吧?这些都不必等待任何其他子进程,因为它没有子进程。从而阻塞,因此父进程不会收到任何终止信号。我想知道你的剧本是否终止。 – collapsar 2013-05-14 10:02:17

回答

7

我想你可以用Paralel::ForkManager来做到这一点。

有一个good tutorial on PerlMonks about Paralel::ForkManager

这可能是这个简单:

my $manager = Parallel::ForkManager->new(6); 
    foreach my $command (@commands) { 
     $manager->start and next; 
     system($command); 
     $manager->finish; 
    }; 
+0

用户只需要6个子进程,而不是17个。由于字符太少,无法进行编辑。好的答案,但。 – 2013-05-14 11:23:38

+0

它完成了它的工作.. – Nethra 2013-05-20 13:18:06

+0

但最终的结果是不准确的,如果所有的6子进程使用相同的临时目录但不访问相同的文件,它会产生任何问题? – Nethra 2013-05-20 13:19:39