在PERL中尝试分叉多个ping请求时无法打印数组

在PERL中尝试分叉多个ping请求时无法打印数组,perl,Perl,我试图在PERL中使用forkping 2 IP。我正在推送在不同阵列中可访问和不可访问的IP。在程序结束时,我打印阵列,但没有得到输出 下面是代码 use strict; use warnings; my (@up, @down, @child, @test); sub prog1 { my $ip = $_[0]; print "Started testing IP : $ip \n"; my $resp = `ping $ip`; if ( $resp =

我试图在PERL中使用
fork
ping 2 IP。我正在推送在不同阵列中可访问和不可访问的IP。在程序结束时,我打印阵列,但没有得到输出

下面是代码

use strict;
use warnings;
my (@up, @down, @child, @test);

sub prog1 {
    my $ip = $_[0];
    print "Started testing IP : $ip \n";
    my $resp = `ping $ip`;
    if ( $resp =~ /reply/) {
        push (@up, $ip);
    }
    else {
        push (@down, $ip);
    }
    push (@test, $ip);
    print "Ended with testing of ip $ip \n";
}

my @list = qw /192.168.1.1 192.168.2.1/;
foreach (@list) {
    my $pid = fork();
    if ( !$pid ) {
        prog1($_);
        exit 0;
    }
    else {
        push (@child, $pid);
    }
}

foreach (@child) {
    waitpid ($_, 0);
    print "Done with pid $_ \n";
}

print "\n\n up is @up";
print "\n\n down is @down\n";
print "test is @test";
我得到的输出是

$ perl fork_ping.pl
Started testing IP : 192.168.1.1
Started testing IP : 192.168.2.1
Ended with testing of ip 192.168.1.1
Done with pid 99804
Ended with testing of ip 192.168.2.1
Done with pid 81456


 up is

 down is
test is

如您所见,
@up
@down
数组为空。任何不在那里填充任何IP的线索

fork不会与父级共享变量。@mpapec:如何打印数组然后不fork是最简单的方法。如果你必须使用fork,你需要建立一个机制,也许是一个管道,让孩子们与父母沟通。为什么你需要fork?因为如果你只因为需要ping大量的主机而进行fork-检查@Nitesh,你现在知道如何进行fork了。您已经知道,fork启动了一个新的过程。您需要在两个进程之间进行通信。在这种情况下,手册页是最好的起始点。。。