我正在運行一個while循環讀取文件中的每一行,然後將該行的數據分叉到子進程。後N行我要等待子進程結束,然後繼續下一個N行等在Perl中叉不工作while從文件中讀while循環
它看起來是這樣的:
while ($w=<INP>) {
# ignore file header
if ($w=~m/^\D/) { next;}
# get data from line
chomp $w;
@ws = split(/\s/,$w);
$m = int($ws[0]);
$d = int($ws[1]);
$h = int($ws[2]);
# only for some days in the year
if (($m==3)and($d==15) or ($m==4)and($d==21) or ($m==7)and($d==18)) {
die "could not fork" unless defined (my $pid = fork);
unless ($pid) {
some instructions here using $m, $d, $h ...
}
push @qpid,$pid;
# when all processors are busy, wait for child processes
if ($#qpid==($procs-1)) {
for my $pid (@qpid) {
waitpid $pid, 0;
}
reset 'q';
}
}
}
close INP;
這是行不通的。在第一輪處理之後,我得到一些等於0的PID,@qpid數組被混合起來,並且文件開始在(顯然)隨機的地方被讀取,跳來跳去。最終結果是文件中的大多數行讀取兩到三次。有任何想法嗎?
非常感謝提前,
S.
請'使用strict'和'use warnings'。這些編譯指示很大程度上減少了調試過程中的痛苦。雖然他們對這個問題沒有幫助,但總的來說,他們確實有很大的不同。 – daotoad 2010-04-09 16:14:08