A
axeman
Can anyone help me understand the behavior of the following code and
recommend a course of action? Thank you.
Background:
I have a daemon process which repeatedly spawns child processes with a
limited time to live, and collects the return code and output. It uses
the typical mechanisms outlined in Programming Perl and the Perl
Cookbook. Recently, over time, commands which return 0 "degrade" to
returning 16777215 each time (-1 before shift). I have witnessed this
on AIX 4.3 + Perl 5.005_03 and AIX 5.3 + Perl 5.8.2.
Narrowing it down to the simplified code snippet below, the convention
marked by line C: is what I have been using in the daemon. Research
has suggested a CHLD signal handler may be required, but when I use
either lines A: or B:, I get a return code of 16777215 every time.
Lastly, I do not want to use non-core modules, as this tool is widely
used and I don't want to mandate additional build/install. Thanks -
appreciate any insight/guidance.
==================================================================
#!/bin/perl
$SIG{'ALRM'} = \&timed_out;
use POSIX ":sys_wait_h";
#A: $SIG{'CHLD'} = \&REAPER;
#B: $SIG{'CHLD'} = 'IGNORE';
while(1){
($rc,@out) = eval {
alarm(5);
@eout = `echo hi 2>&1`;
$erc = ($? >> 8);
alarm(0);
return ($erc,@eout);
};
if( $@ =~ /^timed out/ ) {
$rc = 1;
print "timed out\n";
}
print "rc = $rc, out = @out\n";
C: while( ($stiff = waitpid(-1,&WNOHANG)) > 0 ) {}
sleep(1);
}
sub REAPER {
my $stiff;
while( ($stiff = waitpid(-1,&WNOHANG)) > 0 ) {
# handle if desired
}
}
sub timed_out { # ALRM signal handler for command time-out
die "timed out";
}
recommend a course of action? Thank you.
Background:
I have a daemon process which repeatedly spawns child processes with a
limited time to live, and collects the return code and output. It uses
the typical mechanisms outlined in Programming Perl and the Perl
Cookbook. Recently, over time, commands which return 0 "degrade" to
returning 16777215 each time (-1 before shift). I have witnessed this
on AIX 4.3 + Perl 5.005_03 and AIX 5.3 + Perl 5.8.2.
Narrowing it down to the simplified code snippet below, the convention
marked by line C: is what I have been using in the daemon. Research
has suggested a CHLD signal handler may be required, but when I use
either lines A: or B:, I get a return code of 16777215 every time.
Lastly, I do not want to use non-core modules, as this tool is widely
used and I don't want to mandate additional build/install. Thanks -
appreciate any insight/guidance.
==================================================================
#!/bin/perl
$SIG{'ALRM'} = \&timed_out;
use POSIX ":sys_wait_h";
#A: $SIG{'CHLD'} = \&REAPER;
#B: $SIG{'CHLD'} = 'IGNORE';
while(1){
($rc,@out) = eval {
alarm(5);
@eout = `echo hi 2>&1`;
$erc = ($? >> 8);
alarm(0);
return ($erc,@eout);
};
if( $@ =~ /^timed out/ ) {
$rc = 1;
print "timed out\n";
}
print "rc = $rc, out = @out\n";
C: while( ($stiff = waitpid(-1,&WNOHANG)) > 0 ) {}
sleep(1);
}
sub REAPER {
my $stiff;
while( ($stiff = waitpid(-1,&WNOHANG)) > 0 ) {
# handle if desired
}
}
sub timed_out { # ALRM signal handler for command time-out
die "timed out";
}