in reply to New Child to Wait for First Child to Die

It seems to me like you've got something in reverse. fork returns undef on failure, 0 to the child, and a pid to the parent. The child, in your code, does nothing.
This is a semaphore like solution to your code:
my $i = 3; # how many simultaneous checks $SIG{CHLD} = sub { $i++; wait } # or something # if your signals are safe you may # want to keep a hash of PIDs and # the ip's they're associated with, # and have SIGCHLD's handler print # out the info and test result based # on $? and the return value from wait. # You should exit the child with a # status which you may check. if the # open failed exit with a nonzero value. have running foreach $remote (@iplist){ sleep until ($i); # SIGCHLD will wake us. if it wasn't sigchld we +should go back to sleep. foreach $port (@port_range){ if (fork){ $i--; # empty the reservoir } else { # ping something exit ($test_failed) ? 1 : 0; # exit with a proper value } } }
And a regular one:
foreach my $remote (@iplist){ foreach my $port (@iprange){ if (my $pid = fork){ waitpid, $pid; print "$remote on $port test finished..."; # check $? } else { # check stuff exit ( $test_succeeded ) ? 1 : 0; } } }


Update: Suddenly I wondered - if you don't need to check for many at a time, why are you forking?

-nuffin
zz zZ Z Z #!perl

Replies are listed 'Best First'.
Re: Re: New Child to Wait for First Child to Die
by spike_hodge (Novice) on Apr 24, 2003 at 10:54 UTC
    Good point! Only so that I can have
    $SIG{"ALRM"} = sub { print "TIME_OUT! at $timeout\n"; exit; }; alarm ($timeout);
    timeout the (child) process if the connection hangs. I don't really want to fork at all but I need a way of terminating the port check after a number of seconds and to jump to the next port. I tried something like
    $SIG{"ALRM"} = sub { next; }; alarm($timeout);
    But the SIG does not "happen" inside the loop so the  next fails. timeout the (child) proccess if the connection hangs. I don't realy want to fork at all but I need a way of terminating the port check after a number of secconds and to jump to the next port. I tried something like
    $SIG{"ALRM"} = sub { next; }; alarm($timeout);
    But the SIG does not "happen" inside the loop so the  nect fails.
      Peek inside Net::Ping's source code, and see how tcp pings are implemented. That's the idiom for tcp timeouts, and is an elegant way of doing them safely.

      -nuffin
      zz zZ Z Z #!perl