Elie De Brauwer wrote:
Hello list,

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();

die "cannot fork" unless defined( $pid );

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 ?

greetings
E.


The lower 8 bits are flags that tell you the conditions under which the child stopped. It could be normal termination, halted by signal (TERM or HALT), and if there is a core file. (Core files contain both the program and data at the time of termination. If the program was working on sensitive data and you're clever enough, you can read this data. Leaving core files lying around is a security risk.) Normally, Perl programs just ignore these flags.


--

Just my 0.00000002 million dollars worth,
   --- Shawn

"Probability is now one. Any problems that are left are your own."
   SS Heart of Gold, _The Hitchhiker's Guide to the Galaxy_

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>


Reply via email to