Jean-Charles Ripault wrote:
Hi all,
For a customer project I'm writing a "firewall proof" script.
This script, between two systems tries systematically all tcp ports (up
to a given parameter) of all target network.
Now, I've made a double loop (one for the IP addresses, one for the
ports) to browse this). For speed reasons, I fork all the network
"clients" and they all individually log their results in a global log
file opened before the fork.
Despite this, and the fact that I log both success and failure and that
I check that all children successfully died, I get some holes the log
files, means that some tests do not get logged (they most of the times
do show up in the firewall log) and I get no error message telling me
that a child unexpectedly died or crashed.
[...]
I think your children are clobbering on each other's output.
My advice is to use syslog since it's designed to receive log messages
from multiple asynchronously run applications.
If you don't want to do that, try to organize things so that the
children never have to open the log file themselves. Instead, they
should just write to a file-handle you've opened or redirected.
This is what I'm thinking:
#!/usr/bin/perl
use strict;
use warnings;
use constant EXIT_OK => 0;
local $\ = "\n";
exit (@ARGV < 1 ? parent() : child() );
###############################
sub parent {
close STDOUT;
open (STDOUT, '>', 'logfile') or die("Couldn't re-open STDOUT\n");
for my $id (2..10) {
system("$0 child=$id &");
}
EXIT_OK;
}
sub child {
my ($id) = $ARGV[0] =~ m/(\d+)/;
print "I'm a child, and my id is $id.";
EXIT_OK;
}
__END__
The "&" in the 'system' command allows of asynchronous running of the
child processes, and the children inherit the redirected STDOUT.
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>