Following is C code that is destined to crash:
#include
#include
int main() {
char *p = NULL;
printf(\"Value at P: %c
The OS always sees the same thing... what is returned by the wait(2) family of functions. system(3), et. all, call wait(2). How it peculates up you is what is causing the differences. The shell and the programs do different things and report different ways. Having to shift right 8 to get the most common exit status would be very annoying in shell programs and confuse less tech savvy users.
While the very first UNIX systems I used (genuine Unix) had the same returns I have always wondered if pre-release versions were different and returning the signal and core/dump were a later addition.
My preferred reporting of exit status tends to have the computer split my bits for me.
my $x = $?; #save status print( "Exit status: %d.%d%s\n", $x>>8, $x&127, ($x&128)?"*":"" );
Reading the documentation for system
might answer your question:
system('a.out');
if ($? == -1) {
print "failed to execute: $!\n";
}
elsif ($? & 127) {
printf "child died with signal %d, %s coredump\n",
($? & 127), ($? & 128) ? 'with' : 'without';
}
else {
printf "child exited with value %d\n", $? >> 8;
}
Output:
child died with signal 11, without coredump
The shell just encodes the signal in the status in a different way: 139 - 128 = 11. For example, man bash
says:
The return value of a simple command is its exit status, or 128+n if the command is terminated by signal n.