spike_hodge has asked for the wisdom of the Perl Monks concerning the following question:
I need to be able to make the main program wait until the child process has either died a natural death or has been killed by it's alarm/exit call. Using wait(); sort of works ... that is the main program seems to run through the loops forking a new process for each IP check (this could be thousands of processes) and then each child seems to get on with it's stuff one at a time. I don't understand why the loops don't just wait for the child to die before starting a new one. I suspect I am using wait() all wrong. A simple way would be to put the IP check part of the script in a separate file and call it with system("./check.pl $ip $port");. But I cant believe it's not possible to do this in one script! A simplified copy of the code so far:-$SIG{ "ALRM"} = sub { print "TIME_OUT! at $timeout\n"; exit; }; alarm ($timeout);
require 5.002; require DBI; use strict; use Socket; use POSIX "sys_wait_h"; my ($pid, @port_range, @ip_list, $remote, $port, $timeout, $wait); #----------- config -----------# $timeout = 10; @port_range = qw (8080 6969 80 8081 8080 8000 3128 555 657 889 1180 1181 1182 1183 1184 1185 11012 25318 25719 6969 886); @ip_list = qw (196.2.49.19 grunt.mweb.co.za mweb.co.za); #this will be + populated dynamically from a DB #----------- config -----------# foreach $remote (@ip_list) { foreach (@port_range) { if ($pid = fork) { $wait = wait (); print "Running with port $_ and \$pid $pid \$wait $wait \$? +$? \n"; $port = $_; test ($remote, $port, $pid, $timeout); kill 9, $pid or die "KILL FAILED"; #not necessary? exit; } } } ########################################################### ## SUB ROUTINES ## ########################################################### sub test { my $remote = shift; my $port = shift; my $pid = shift; my $timeout = shift; my ($iaddr, $paddr, $proto); $SIG{"ALRM"} = sub { print "TIME_OUT! at $timeout\n"; exit; }; alarm ($timeout); $iaddr = inet_aton ($remote) or die "no host: $remote"; $paddr = sockaddr_in ($port, $iaddr); $proto = getprotobyname ('tcp'); socket (SOCK, PF_INET, SOCK_STREAM, $proto) or die "socket: $!"; if (connect (SOCK, $paddr)) { print "WE HAVE A CONNECTION!\n" } else { die "connect: $!"; } close (SOCK) or die "close: $!"; } exit;
edited: Fri Apr 25 05:01:24 2003 by jeffa - added readmore tag
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: New Child to Wait for First Child to Die
by mugwumpjism (Hermit) on Apr 24, 2003 at 08:16 UTC | |
by spike_hodge (Novice) on Apr 24, 2003 at 09:16 UTC | |
|
Re: New Child to Wait for First Child to Die
by nothingmuch (Priest) on Apr 24, 2003 at 10:28 UTC | |
by spike_hodge (Novice) on Apr 24, 2003 at 10:54 UTC | |
by nothingmuch (Priest) on Apr 24, 2003 at 11:07 UTC | |
|
(IO::Socket::INET) Re: New Child to Wait for First Child to Die
by bbfu (Curate) on Apr 24, 2003 at 19:53 UTC |