in perl, how do we detect a segmentation fault in an external command

前端 未结 2 1731
一向
一向 2021-01-12 14:04

Following is C code that is destined to crash:

#include
#include

int main() {
    char *p = NULL;
    printf(\"Value at P: %c         


        
2条回答
  •  萌比男神i
    2021-01-12 14:52

    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)?"*":"" );

提交回复
热议问题