Jeff Pang wrote:
You can't see the output in your log is because when your childs are
executing,your parent has exited.
In order to see the callbacking childs,just add this line in the code end:
sleep while(1); # parent sleep to wait for childs exiting
This would be a bad idea since the process never exits.
And,don't forget to flush the LOG file handle.
I'v modified your code as below,it could show me correctly the childs exiting
status from the log.
use strict;
use warnings;
use POSIX qw(WNOHANG);
$SIG{CHLD} = \&reaper;
open LOG, ">>","test.log" or die "Can't open log file: $!";
select LOG;$|=1;select STDOUT; # here is important
my $NumberOfChildren = 0;
sub reaper
{
while((my $stiff = waitpid(-1, WNOHANG)) > 0)
{
print LOG "Child process no. $stiff exited with status $?\n";
$NumberOfChildren --;
}
}
my $dir = ".";
my @files;
opendir DH, $dir or die "Can't open Video dir: $!";
for (readdir DH)
{
(/msfwsvr\.log\./)? push @files, $_ : next;
}
closedir DH;
print LOG "Simpsons episodes:\n", map "$_\n", @files;
for my $file(@files)
{
defined(my $pid = fork) or die "Can't fork for $file: $!";
unless($pid)
{
exec "sleep 5";
die "Can't execute tovid for process no $$ on file $file: $!";
}
else
{
print LOG "Forked child process no. $pid for file $file.\n";
$NumberOfChildren ++;
}
}
sleep while(1);
sleep while( $NumberOfChildren > 0 );
HTH.
--
Jeff Pang
NetEase AntiSpam Team
http://corp.netease.com
--
Just my 0.00000002 million dollars worth,
--- Shawn
"For the things we have to learn before we can do them,
we learn by doing them."
Aristotle
"The man who sets out to carry a cat by its tail learns something that
will always be useful and which will never grow dim or doubtful."
Mark Twain
* Perl tutorials at http://perlmonks.org/?node=Tutorials
* A searchable perldoc is at http://perldoc.perl.org/
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>