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

Yaerox has asked for the wisdom of the Perl Monks concerning the following question:

Hey guys, it's the first time for me posting on Perlmonks, I really hope I didn't chose the wrong area to do this. If so, please let me know.

Well, I'm actual trying to get www.Gearman.org running and it seems like I have to fork on some point. When I was researching about forking how it works, and how it should be done, I read something about exit_value, signal_num and dump_core. Maybe a little code example here:

You can check all the failure possibilities by inspecting "$?" like this: $exit_value = $? >> 8; $signal_num = $? & 127; $dumped_core = $? & 128;

Best method to capture return code from system calls?

I know what bitwise is and how it works, but I don't get what this three variables should tell me. I mean I know what an exit_value is, but this signal num or dumped_core ... what does it brings to me to bitwise $? (= return value by a fork) with 127 or 128? Regards.

Replies are listed 'Best First'.
Re: What means: exit_value , signal_num and dumped_core
by Bloodnok (Vicar) on Apr 11, 2014 at 12:23 UTC
    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 :-))

      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 :-))