Linux 中是否有任何标准的退出状态代码? [英] Are there any standard exit status codes in Linux?

查看:36
本文介绍了Linux 中是否有任何标准的退出状态代码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果进程的退出状态为 0,则该进程在 Linux 中被认为已正确完成.

我已经看到分段错误通常会导致退出状态为 11,尽管我不知道这只是我工作的惯例(像那样失败的应用程序都是内部的)还是标准.

Linux 中的进程是否有标准的退出代码?

解决方案

8 位返回码和 8 位灭杀信号数在 wait(2) &co..

#include #include #include #include #include #include int main() {内部状态;pid_t child = fork();如果(孩子 <= 0)退出(42);waitpid(child, &status, 0);如果(WIFEXITED(状态))printf("第一个孩子退出了 %u
", WEXITSTATUS(status));/* 打印:第一个孩子以 42 退出" */孩子=叉();如果(孩子 <= 0)杀死(getpid(),SIGSEGV);waitpid(child, &status, 0);如果(WIFSIGNALED(状态))printf("第二个孩子死于 %u
", WTERMSIG(status));/* 打印:第二个孩子死于 11" */}

您如何确定退出状态?传统上,shell 只存储 8 位返回码,但如果进程异常终止,则设置高位.

<前>$ sh -c '退出 42';回声 $?42$ sh -c 'kill -SEGV $$';回声 $?分段故障139$ expr 139 - 12811

如果你看到的不是这个,那么程序可能有一个 SIGSEGV 信号处理程序,然后它会正常调用 exit,所以它实际上并没有被杀死通过信号.(程序可以选择处理除 SIGKILLSIGSTOP 之外的任何信号.)

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, though I don't know if this is simply the convention where I work (the apps that failed like that have all been internal) or a standard.

Are there standard exit codes for processes in Linux?

解决方案

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 <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
#include <signal.h>

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
", 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
", 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.)

这篇关于Linux 中是否有任何标准的退出状态代码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆