John W. Krahn wrote:
Elie De Brauwer wrote:
Hello list,
Hello,
I recently encountered a small oddity. Suppose I have a process A:
#!/usr/bin/perl
use strict;
print "Hello \n";
sleep 1;
print "Goodbye\n";
exit 9;
Simply shows some out and gives a certain exit code. A second process,
simply calls fork, execs the child in a process and waits for the child
in the other process. In Perl this can look like this:
my $cmd = "/home/user/proces.pl";
my $pid = fork();
if($pid == 0){
print "Hi I'm a child\n";
exec $cmd or die "Failed to run $cmd\n";
}else{
print "Hi I'm a parent waiting for child with PID: $pid\n";
my $ret = waitpid($pid,0);
print "$pid exited with code ". ($?>>8) ."\n";
}
The oddity i located in the last line. It seemd that I had to divide $?
by 256 (or shift over 8 positions to the right) to get the correct exit
code. So my question is:
a) Is there an other way to wait for a child to die and get the exit code
b) Can someone explain the odd behaviour of the exit code ?
Have you read the perl documentation on the functions and variables you are
using?
perldoc perlipc
-> Read it
perldoc perlvar
-> Didn't read it
perldoc -f fork
-> Read it
perldoc -f exec
-> Read it
perldoc -f waitpid
-> Read it
And as murphy'd say, perldoc perlvar contained what i was looking for:
$? The status returned by the last pipe close, backtick (``) com-
mand, successful call to wait() or waitpid(), or from the sys-
tem() operator. This is just the 16-bit status word returned
by the wait() system call (or else is made up to look like it).
Thus, the exit value of the subprocess is really ("$? >> 8"),
and "$? & 127" gives which signal, if any, the process died
from, and "$? & 128" reports whether there was a core dump.
(Mnemonic: similar to sh and ksh.)
Thanks for the pointer
--
Elie De Brauwer
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>