我已经实现了解析大文件的概念,如下所示,但似乎不正确。 我面对代码的问题很少,一些变量无法访问。
#ALL the variable in Complete CAPS are global variable
TLog("MSG",1,"Parent process $$");
TLog("MSG",4,"Creating child process for $$");
my $MAX_FORK = 2;
my $forkCount = 0;
my $processCounter = 0;
my @childId = ();
foreach my $fileNameFasta (@{$ref_array_file}) {
my $pid = fork();
if ( $pid ) {
TLog("MSG",1,"child process created : $pid");
push @childId,$pid;
$forkCount++;
}
elsif ( $pid == 0 ) {
my $outputFile = $STAT_FILE;
my $pidLocal = $childId[$processCounter]; #Use of unintialized variable
$outputFile =~s/\d{1,}\.txt$/$pidLocal\.txt/og; #hence naming of all ouput file are same
TLog("MSG",1,"For $pidLocal Creating output file for stat : $outputFile");
open my $outputfh,'>',$outputFile;
GenerateTupleCountFile($outputfh,$fileNameFasta);
close $outputfh;
TLog("MSG",5,"Calculation completed for $pidLocal");
TLog("MSG",5,"Plz check the $outputFile");
$processCounter++;
exit(0);
}
if ( $forkCount >= $MAX_FORK ) {
foreach (@childId) {
my $tmp = waitpid($_, 0);
TLog("MSG",5,"Process completed for with pid $tmp");
}
}
}
}
如果我是冤枉,请给我正确的指示。
答案 0 :(得分:3)
一旦进行了分叉,父母和孩子就会自主。
在@childID
之后,你永远不会在fork
数组中设置任何内容,到那时,孩子知道在那里写的内容为时已晚。您需要在孩子中使用getpid()
,或者使用神奇变量$$
,或者(如果您使用过use English '-no_match_vars';
)$PID
或$PROCESS_ID
。父级永远不会增加$processCounter
。
同样,孩子会增加$processCounter
的副本,但这不会影响父母的变量。