Linux中是否有任何标准的退出状态代码? [英] Are there any standard exit status codes in Linux?
问题描述
如果某个进程的退出状态为0,则认为该进程已在Linux中正确完成.
A process is considered to have completed correctly in Linux if its exit status was 0.
我已经看到,分段错误通常会导致退出状态为11,尽管我不知道这仅仅是我工作的惯例(失败的应用程序都是内部的)还是标准的.
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.
Linux中是否有用于进程的标准退出代码?
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\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" */
}
您如何确定退出状态?传统上,shell仅存储8位返回码,但如果进程异常终止,则将高位设置为高位.
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
如果您看到的不是此内容,则程序可能有一个SIGSEGV
信号处理程序,该信号处理程序随后会正常调用exit
,因此实际上并没有被该信号杀死. (除了SIGKILL
和SIGSTOP
,程序可以选择处理任何信号.)
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屋!