I have a forked ping program (mostly lifted from www.stonehenge.com, thanks merlyn). Anyway, I have modified his code to use Net::Ping and log the results to a file.

The problem I am having is this, I need the part that loads the array @hosts to only be executed in the parent not the child, but the fork comes below in the block that depends on @hosts being populated. The problem is that the loading of @hosts is being done in every child. Not only is this not efficient, but I am getting multiple reports of bad ip's written to the log file.

How can I, prior to having $pid set in the fork, make the block that loads @hosts execute only in the parent?

#!/usr/bin/perl -w use strict; use Net::Ping; my $source_file = (shift || "/tmp/ping_list.txt"); my $good_out = "/tmp/good_ips.txt"; my $bad_out = "/tmp/bad_ips.txt"; my %pid_to_host; my %host_result; my @hosts; $|++; open(GOOD,">$good_out") or die "Cannot open output file $good_out: $!\n"; open(BAD,">$bad_out") or die "Cannot open output file $bad_out: $!\n"; open(IPS,"$source_file") or die "Cannot open source file $source_file: $!\n"; for (<IPS>) { chomp; s/ *//; s/\cM//; if ($_ =~ m/^\d+\.\d+\.\d+\.\d+$/) { push(@hosts,$_); } else { print BAD "$_ : NOT A VALID IP ADDRESS\n"; } } for (@hosts) { wait_for_a_kid() if keys %pid_to_host > 50; if ( my $pid = fork ) { # parent $pid_to_host{$pid} = $_; } else { # child exit !ping_a_host($_); } } 1 while wait_for_a_kid(); for (sort keys %host_result) { print GOOD "$_\n" if ($host_result{$_}); print BAD "$_\n" if (!$host_result{$_}); } sub ping_a_host { my $host = shift; my $p = Net::Ping->new('tcp',5); $p->ping($host) ? 1 : 0; } 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; $host_result{$host} = $? ? 0 : 1; 1; }

In reply to Forkin around by gnu@perl

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.