http://qs321.pair.com?node_id=1081954


in reply to What means: exit_value , signal_num and dumped_core

All shells return the reason for their exit to the kernel, this is $signal_num - ordinarily this is 0 (EXIT); See here or trap for examples and indeed, far better descriptions.

In addition to the above, there are quite a number of cases whereby a shell exits with a non-0 signal sufficiently severely to warrant a post-exit investigation - in these cases, the shell 'core dumps' in order to fascilitate the root cause investigation - this is the meaning of $dumped_core - i.e. it's set if and only if, the shell core dumped as a result of a 'bad' exit.

A user level that continues to overstate my experience :-))

Replies are listed 'Best First'.
Re^2: What means: exit_value , signal_num and dumped_core
by Yaerox (Scribe) on Apr 11, 2014 at 12:44 UTC

    Firstly, thanks for the reply. So did I understood this right, that in case that the signal_num isn't equal to 0 coredump is having a value too and the child died unexpected?

    But I'm still asking myself, why I have to check this on the bitwise-operand way with if ($? & 127)?

    Here some sample code:

    my $pid = fork( ); if ( defined $pid && $pid == 0 ) { # child system( $_[0]->arg ); if ($? == -1) { return "failed to execute: $!\n"; } elsif ($? & 127) { return "child died with signal %d, %s coredump\n", ($? + & 127), ($? & 128) ? 'with' : 'without'; } else { return "child exited with value " . $? >> 8; } } else { # parent }
      It's because the exit code, signal number and core dump flag are all rolled up into a word i.e. a single 16 bit, value - the bitwise operators are used to extract the various bits of the word.

      Note, also, that just because a process exits with a non-zero signal, it doesn't necessarily mean that a core dump will have been generated as the process exited.

      A user level that continues to overstate my experience :-))

        Ahh, okay, now I got it ^^

        Thank you very much Sir.