romandas has asked for the wisdom of the Perl Monks concerning the following question:

Or both? :)

I am trying to use fork() for the first time, via merlyn's column Doing many things, like pings. However, I am trying to do this on ActiveState Perl (5.8.8) on Windows XP, and am not sure if that is the cause of my problem.

Here is my code (well, merlyn's with OS-specific modifications):

#!/perl/bin/perl use warnings; use strict; sub ping_a_host { my $host = shift; `ping -n 1 -w 1 $host 2>NUL` =~ /Received = 0/ ? 0 : 1; } my %pid_to_host; my %host_result; sub wait_for_a_kid { my $pid = wait; return 0 if $pid < 0; my $host = delete $pid_to_host{$pid} or warn("Why did I see $pid ($?)\n"), next; warn "reaping $pid for $host\n"; $host_result{$host} = $? ? 0 : 1; 1; } my @hosts = map "10.0.1.$_", "025".."026"; for (@hosts) { wait_for_a_kid() if keys %pid_to_host > 10; if (my $pid = fork) { ## parent does... $pid_to_host{$pid} = $_; warn "$pid is processing $_\n"; } else { # child does ## child does... exit !ping_a_host($_); } } ## final reap: 1 while wait_for_a_kid(); for (sort keys %host_result) { print "$_ is ", ($host_result{$_} ? "good" : "bad"), "\n"; }

The code runs, however there are two problems I've noticed. 1) The final print statement never prints, regardless of the system state. 2) If the IP is not up, the process never finishes; it outputs:

C:\Scripts\Parallel>ping_parallel.pl -3676 is processing 10.0.1.025 -3072 is processing 10.0.1.026

and then sits there. Any ideas what may be wrong?

Replies are listed 'Best First'.
Re: Windows fork problem or my ignorance?
by ikegami (Patriarch) on Mar 21, 2008 at 12:02 UTC

    Some quick debugging shows that the print loop is reached, but that the hash is empty.

    return 0 if $pid < 0; wasn't adjusted for the OS.

    Windows doesn't support forking. When you call fork a thread is actually created. For those pseudo-processes, $$ is negative, so wait can return a negative number on success.

    Use return 0 if $pid == -1; instead.

      Thanks! That solved some of the problem, at least the non-printing portion of it. However, when I run it against the range 10.0.1.25..30, I get this:

      C:\Scripts\Parallel>ping_parallel.pl -4012 is processing 10.0.1.025 -3148 is processing 10.0.1.026 reaping -4012 for 10.0.1.025 reaping -3148 for 10.0.1.026 10.0.1.025 is good 10.0.1.026 is good ## This one finished C:\Scripts\Parallel>ping_parallel.pl -2808 is processing 10.0.1.025 -3424 is processing 10.0.1.026 -2532 is processing 10.0.1.027 -3360 is processing 10.0.1.028 -696 is processing 10.0.1.029 -4036 is processing 10.0.1.030 reaping -2808 for 10.0.1.025 reaping -3424 for 10.0.1.026 reaping -2532 for 10.0.1.027 reaping -3360 for 10.0.1.028 reaping -696 for 10.0.1.029
      and sits here. I know that .27 - .30 are not up. Any idea why it sits there?

        I can reproduce the problem. wait never returns. Looks like a bug in Perl. Fortunately, it appears to be fixed in 5.10.0.

        Maybe it had to do with `` using wait as well. On unix, that wouldn't be a problem since the two calls to wait happen in different processes. The pseudo-processes fork creates on Windows are just threads, so the two calls to wait happen in the same process.

Re: Windows fork problem or my ignorance?
by NetWallah (Canon) on Mar 21, 2008 at 17:07 UTC
    Perhaps you could recycle some of my old code that does Multi-threaded ping of a list of hosts (Tested successfully on Windows).

    If you just want to get the Threads part, please view this node.

         "As you get older three things happen. The first is your memory goes, and I can't remember the other two... " - Sir Norman Wisdom

Re: Windows fork problem or my ignorance? (sorting IP4)
by ikegami (Patriarch) on Mar 21, 2008 at 23:15 UTC

    A better sort:

    sub pack_ip4 { pack 'C4', split /\./, @_ ? $_[0] : $_ } for ( map substr($_, 4), sort map pack_ip4($_).$_, keys %host_result ) { ... }

    That way, it'll sort the IPs numerically (.30 will be after .29 instead of after .3) without having to resort to adding zeroes.