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?


In reply to Windows fork problem or my ignorance? by romandas

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.