为什么我的 Perl 程序在 fork 后没有收获子进程?

发布于 2024-07-21 12:39:55 字数 1768 浏览 9 评论 0原文

我一直在尝试使用 Perl 编写一个简单的 ping 扫描器供内部使用。 由于它扫描 24 位 CIDR 网络,因此如果脚本在单线程中运行,则运行时间会过长。 我尝试添加 fork 功能来加速该过程,但我的第一次尝试花费了几乎相同的时间,因为在任何给定时间只有一个子进程处于活动状态。

我在 perlipc 文档以及 Perl Cookbook 并提出了第二个版本:

##Install the CHLD SIG handler
$SIG{CHLD} = \&REAPER;
sub REAPER {
    my $childPID;
    while (( $childPID = waitpid(-1, WNOHANG)) > 0) {
        print "$childPID exited\n";
    }
    $SIG{CHLD} = \&REAPER;
}

my $kidpid;
for (1 .. 254) {
    my $currIP = join ".", (@ipSubset[0,1,2], $_);

    die "Could not fork()\n" unless defined ($kidpid = fork);
    if ($kidpid) {
        #Parent process
        #Does nothing
    } 
    else {
        #Child process
        my $pingConn = Net::Ping->new();    #TCP
        say "$currIP up" if $pingConn->ping($currIP);
        $pingConn->close(); 

        #Nothing else to do
        exit;
    }
}

say "Finished scanning $ipRange/24";

当我扫描内部网络时,输出是:

$perl pingrng2.pl 192.168.1.1
192.168.1.2 up
5380 exited
192.168.1.102 up
192.168.1.100 up
5478 exited
5480 exited
Finished scanning 192.168.1.1/24

从结果中可以看出,成功扫描的线程打印“up”消息,干净地退出并由父进程获取。 同时,其他 251 个线程悬空附加到“/sbin/init”,如快速“ps -ef”列表所示。 如果我在退出语句之前的子处理块中添加“print "Child: $currIPending\n"”,我会在 Perl 脚本退出后从终端上的剩余 251 个进程中获得输出。

这里发生了什么? 我认为 $SIG{CHLD} 子例程与 waitpid 循环相结合将收获所有子进程并确保系统中不留下任何僵尸/悬空进程。

同时,我还希望能够在任何给定时间运行特定数量的子进程,例如,“n”个子进程同时运行,每当一个子进程退出时,父进程就会在需要时启动另一个子进程,但没有更多子进程比任何特定时刻的“n”个孩子都要多。 这可能吗? 如果是的话,我可以获得一些伪代码来帮助指导我吗?

I have been trying to write a bare-bones ping scanner using Perl for internal use. Since it scans a 24-bit CIDR network the script takes too long to run if it runs in a single thread. I have tried adding fork functionality to speed up the process but my first attempt was taking pretty much the same time since there was only one child process active at any given time.

I read up on child processes in the perlipc document and also in the Perl Cookbook and came up with the second version:

##Install the CHLD SIG handler
$SIG{CHLD} = \&REAPER;
sub REAPER {
    my $childPID;
    while (( $childPID = waitpid(-1, WNOHANG)) > 0) {
        print "$childPID exited\n";
    }
    $SIG{CHLD} = \&REAPER;
}

my $kidpid;
for (1 .. 254) {
    my $currIP = join ".", (@ipSubset[0,1,2], $_);

    die "Could not fork()\n" unless defined ($kidpid = fork);
    if ($kidpid) {
        #Parent process
        #Does nothing
    } 
    else {
        #Child process
        my $pingConn = Net::Ping->new();    #TCP
        say "$currIP up" if $pingConn->ping($currIP);
        $pingConn->close(); 

        #Nothing else to do
        exit;
    }
}

say "Finished scanning $ipRange/24";

When I scan my internal network the output is:

$perl pingrng2.pl 192.168.1.1
192.168.1.2 up
5380 exited
192.168.1.102 up
192.168.1.100 up
5478 exited
5480 exited
Finished scanning 192.168.1.1/24

As can be seen in the result, the threads which do a successful scan print the "up" message, exit cleanly and are reaped by the parent process. The other 251 threads meanwhile are left dangling attached to '/sbin/init' as can be seen by a quick 'ps -ef' listing. If I add a 'print "Child: $currIP ending\n"' in the child processing block just before the exit statement I get the output from the remaining 251 processes on my terminal "after" my perl script has exited.

What's going on here? I thought that the $SIG{CHLD} subroutine coupled with the waitpid loop would reap all the child processes and ensure that no zombies/dangling processes were left in the system.

In the same breath I would also like to be able to run a specific number of child processes at any given time, say for example, 'n' children running concurrently, whenever one exits the parent process starts another child if needed but has no more than 'n' children at any given moment. Is this possible? If yes could I get some pseudo-code to help guide me?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

生生漫 2024-07-28 12:39:55

查看 Parallel::ForkManager。 它会为您处理所有这些小细节。

Have a look at Parallel::ForkManager. It takes care of all those little details for you.

ぽ尐不点ル 2024-07-28 12:39:55

看起来你的父进程在子进程之前完成(因此永远没有机会收获它们)。 试试这个:

#!/usr/bin/perl

use 5.010;
use strict;
use warnings;

use Net::Ping;

my @ipSubset = (192, 168, 10);

my $i = 0;
my @pids;
for my $octet (1 .. 254) {
    my $currIP = join ".", @ipSubset[0 .. 2], $octet;

    die "Could not fork()\n" unless defined (my $pid = fork);

    #parent saves chlidren's pids and loops again
    if ($pid) {
        push @pids, $pid;
        next;
    } 

    #child process
    my $pingConn = Net::Ping->new;
    say "$currIP up" if $pingConn->ping($currIP);
    $pingConn->close(); 
    exit;
}

#wait on the children
for my $pid (@pids) {
    waitpid $pid, 0;
}

It looks like your parent process is finishing before the children (and therefore never getting a chance to reap them). Try this instead:

#!/usr/bin/perl

use 5.010;
use strict;
use warnings;

use Net::Ping;

my @ipSubset = (192, 168, 10);

my $i = 0;
my @pids;
for my $octet (1 .. 254) {
    my $currIP = join ".", @ipSubset[0 .. 2], $octet;

    die "Could not fork()\n" unless defined (my $pid = fork);

    #parent saves chlidren's pids and loops again
    if ($pid) {
        push @pids, $pid;
        next;
    } 

    #child process
    my $pingConn = Net::Ping->new;
    say "$currIP up" if $pingConn->ping($currIP);
    $pingConn->close(); 
    exit;
}

#wait on the children
for my $pid (@pids) {
    waitpid $pid, 0;
}
眼泪都笑了 2024-07-28 12:39:55

当线程调用 ping() 时,它会不断尝试 ping IP,直到设置响应。 要解决此问题,请尝试在 ping() 中包含超时作为第二个参数。 现在看起来,那些剩余的线程正在继续 ping 请求响应,直到得到响应。

至于拥有一组 N 数量的线程,为什么不将 0-255 分成多个块,例如拥有两个线程,一个从 0-127,另一个从 128-255? 为了简单起见,我将使用 2 的倍数作为线程数。

When a thread calls ping() it keeps trying to ping the IP until it sets a response. to fix this, try to include a timeout as a second argument in ping(). It looks like right now, those remaining threads are continuing to ping for a response until they get one.

As far as having a set N number of threads, why not break the 0-255 into chunks, for instance having two threads, one that goes from 0-127 and one that goes 128-255? I would use some multiple of 2 for your number of threads for simplicity's sake.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文