Are there any standard exit status codes in Linux?

后端 未结 10 1704
醉话见心
醉话见心 2020-11-22 06:46

A process is considered to have completed correctly in Linux if its exit status was 0.

I\'ve seen that segmentation faults often result in an exit status of 11, thou

10条回答
  •  悲&欢浪女
    2020-11-22 07:13

    8 bits of the return code and 8 bits of the number of the killing signal are mixed into a single value on the return from wait(2) & co..

    #include 
    #include 
    #include 
    #include 
    #include 
    #include 
    
    int main() {
        int status;
    
        pid_t child = fork();
        if (child <= 0)
            exit(42);
        waitpid(child, &status, 0);
        if (WIFEXITED(status))
            printf("first child exited with %u\n", WEXITSTATUS(status));
        /* prints: "first child exited with 42" */
    
        child = fork();
        if (child <= 0)
            kill(getpid(), SIGSEGV);
        waitpid(child, &status, 0);
        if (WIFSIGNALED(status))
            printf("second child died with %u\n", WTERMSIG(status));
        /* prints: "second child died with 11" */
    }
    

    How are you determining the exit status? Traditionally, the shell only stores an 8-bit return code, but sets the high bit if the process was abnormally terminated.

    $ sh -c 'exit 42'; echo $?
    42
    $ sh -c 'kill -SEGV $$'; echo $?
    Segmentation fault
    139
    $ expr 139 - 128
    11
    

    If you're seeing anything other than this, then the program probably has a SIGSEGV signal handler which then calls exit normally, so it isn't actually getting killed by the signal. (Programs can chose to handle any signals aside from SIGKILL and SIGSTOP.)

提交回复
热议问题