叉在Perl不工作,从文件中读取while循环 [英] Fork in Perl not working inside a while loop reading from file

查看:188
本文介绍了叉在Perl不工作,从文件中读取while循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在运行一个while循环读取文件中的每一行,然后将该行的数据分叉到一个子进程。在N行之后,我想等待子进程结束并继续N行,等等。 ($ w =< INP>){

#忽略文件头部
if($ w

$ b $ p $ < 〜m / ^ \D /){next;}

#从行
获取数据chomp $ w;
@ws = split(/ \ s /,$ w);

$ m = int($ ws [0]);
$ d = int($ ws [1]);
$ h = int($ ws [2]); ($ m == 3)和($ d == 15)或($ m == 4)以及($ d = 1),

#除非定义(my $ pid = fork),否则就不能分叉(fork =21)或者($ m == 7)和($ d == 18)

除非($ pid){

在这里使用$ m,$ d,$ h ...

}
推@ qpid,$ pid;

#当所有处理器都忙时,等待子进程
if($ qpid ==($ procs-1)){
for $ pid(@qpid) {
waitpid $ pid,0;
}
重置'q';
}
}
}

关闭INP;

这不起作用。在第一轮处理之后,我得到一些等于0的PID,@qpid数组被混合起来,文件开始在(显然)随机的地方被读取,跳来跳去。最终的结果是文件中的大部分行读取两到三次。有任何想法吗?

非常感谢,b
$ b $ s



>如果不是,那么你的孩子在运行这个命令之后,会把 $ pid 为零的数组添加到数组中,并且通常继续运行应该是父进程代码


I'm running a while loop reading each line in a file, and then fork processes with the data of the line to a child. After N lines I want to wait for the child processes to end and continue with the next N lines, etc.

It looks something like this:

    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;

This is not working. After the first round of processes I get some PID equal to 0, the @qpid array gets mixed up, and the file starts to get read at (apparently) random places, jumping back and forth. The end result is that most lines in the file get read two or three times. Any ideas?

Thanks a lot in advance,

S.

解决方案

Are you exiting inside unless ($pid)?

If not, then your child, after running the command, will add $pid of zero to the array and generally continue running what is supposed to be parent process code

这篇关于叉在Perl不工作,从文件中读取while循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆