如何在perl中限制子进程?
我已经写了一个脚本,其中包括并行生成gcov。我成功了,但一次创建17个子进程。但我想一次只创建6个子进程,第7个子进程应该在1个子进程终止后创建如何在perl中限制子进程?,perl,fork,Perl,Fork,我已经写了一个脚本,其中包括并行生成gcov。我成功了,但一次创建17个子进程。但我想一次只创建6个子进程,第7个子进程应该在1个子进程终止后创建 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
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.
sub-gcov_parallel()
2 {
3打印“生成Gcov…\n”;
4.我的孩子;
5我的$pid;
6@list=(@iucall,@iurcall_init,@iurcall_term,@iurcall_uti,@nob,@nobcch,@nobcell,@nobrrc,@nobcall,@rnccall,@cellin
fo、@rnccom、@cellrrm、@uerrm、@uerrc、@uecall、@iupced);
7我的$len_list=标量(@list);
8我的$maxlen=0;
9我的$count=0;
10我的$process=0;
11$total_components=标量(@comp_list);
12
13(我的$comp_count=0;$comp_count<$len_list;($comp_count=$comp_count+$no_机器))
14 {
15#将子进程限制为6
16如果($process==6)
17 {
18$pid=wait();
19$process=$process-1;
20 }
21其他
22 {
23$pid=fork();
24如果($pid eq 0)
25 {
26(我的$files\u count=0;$files\u count<$no\u机器;$files\u count++)
27 {
28$count=$files\u count+$comp\u count;
29如果($count<$len_列表)
30 {
31 chomp($list[$count]);
32 my@list_gcda=`ls$list[$count]/*.gcda | sort`;
&生成_gcov(“$list[$count]”,@list_gcda);
34 }
35 }
36等待();
37出口;
38 }
39$process=$process+1;
40 }
41 }
42做
43 {
44$kid=waitpid(-1,0);
45}而$kid>0;
46 }
但我在运行脚本时观察到,它在生成gcov时跳过了文件。
我想你可以用它来做这件事
有一个问题
可以这么简单:
my $manager = Parallel::ForkManager->new( 6 );
foreach my $command (@commands) {
$manager->start and next;
system( $command );
$manager->finish;
};
列表1包含。。组件数组@iucall、@uerrc等。。。每个组件数组都包含文件。对于前6个组件,它们工作正常,但之后会跳过备用组件。您在第36行中的
wait()
调用似乎可疑:第26-35行构成子进程的代码,对吗?所有这些都不必等待任何其他子进程,因为它没有子进程。因此,它会阻塞,因此父进程将不会收到任何终止信号。我想知道您的脚本是否终止。用户只需要6个子进程,而不是17个子进程。无法进行编辑,因为字符太少。答案很好,但最终的结果并不准确,如果所有6个子进程都使用相同的临时目录,但不访问相同的文件,会不会产生任何问题?