C语言 C 中退出代码 11 的含义?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/31103254/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 12:01:24  来源:igfitidea点击:

Meaning of Exit Code 11 in C?

csegmentation-faultexit-code

提问by user3593148

What's the general meaning of an exit code 11 in C? I've looked around and can not find a definitive answer so I thought I would ask here. It comes when i try to add an element to a vector.

C 中退出代码 11 的一般含义是什么?我环顾四周,找不到明确的答案,所以我想我会在这里问。当我尝试将元素添加到向量时出现。

回答by Gilles 'SO- stop being evil'

You didn't find a definitive answer because there isn't one. It's up to the author of the program to decide what exit codes they wish to use. Standard C only says that exit(0)or exit(EXIT_SUCCESS)indicate that the program is successful, and that exit(EXIT_FAILURE)indicates an error of some kind. (Returning a value from mainis equivalent to calling exitwith that value.) Most common operating systems including Windows, Linux, OSX, etc. use 0 for success and values from 1 to 255 to indicate errors; still choosing between error codes is up to the application writer, the value 11 isn't anything special.

你没有找到一个明确的答案,因为没有。由程序的作者决定他们希望使用什么退出代码。标准 C 仅表示exit(0)exit(EXIT_SUCCESS)指示程序成功,并exit(EXIT_FAILURE)指示某种错误。(返回值 frommain等效于exit使用该值调用。)大多数常见的操作系统,包括 Windows、Linux、OSX 等,使用 0 表示成功,使用 1 到 255 之间的值表示错误;仍然由应用程序编写者在错误代码之间进行选择,值 11 没什么特别的。

Under Linux and most other Unix variants, the signalnumber 11 indicates a segmentation fault, as remarked by Kerrek SB. A segmentation fault happens when a program makes some kind of invalid memory access, so it's a plausible consequence of accessing an array out of bounds, or an error in pointer arithmetic, or trying to access a null pointer, or other pointer-related errors. Signal 11 is not the same thing as exit code 11: when a program dies due to a signal, it's marked as having been killed by a signal, rather than having exited normally. Unix shells report signals by reporting an exit code which is the signal number plus 128, so 139 for a segmentation fault.

在 Linux 和大多数其他 Unix 变体下,信号编号 11 表示分段错误,正如Kerrek SB 所说。当程序进行某种无效的内存访问时,就会发生分段错误,因此它可能是访问数组越界、指针运算错误、尝试访问空指针或其他与指针相关的错误的合理结果。信号 11 与退出代码 11 不同:当程序因信号而死亡时,它被标记为已被信号杀死,而不是正常退出。Unix shell 通过报告退出代码来报告信号,退出代码是信号号加 128,因此 139 表示分段错误。

回答by user3593148

The other answers have missed a possible ambiguity in the phrase "exit code". I suspect what you meant by "exit code" is the status code retrieved with the waitfamily of syscalls, as in:

其他答案遗漏了短语“退出代码”中可能存在的歧义。我怀疑您所说的“退出代码”是使用wait系统调用系列检索到的状态代码,如下所示:

/* assume a child process has already been created */
int status;
wait(&status);
printf("exit code %d\n", status);

If you do something like that you may very will see "exit code 11" if the child process segfaults. If the child process actually called exit(11)you might see "exit code 2816" instead.

如果您执行类似的操作,如果子进程出现段错误,您可能会看到“退出代码 11”。如果子进程实际调用,exit(11)您可能会看到“退出代码 2816”。

It would be better to call those things "wait code" or "wait status" instead of "exit code", to avoid confusion with the value passed to exit. A wait code contains several pieces of information packed together into a single integer. Normally, you should not look at the integer directly (like I did above in that printf). You should instead use the W*macros from <sys/wait.h>to analyze it.

最好将这些东西称为“等待代码”或“等待状态”而不是“退出代码”,以避免与传递给退出的值混淆。等待代码包含多条信息打包成一个整数。通常,您不应该直接查看整数(就像我在上面所做的那样printf)。您应该改用W*from的宏<sys/wait.h>来分析它。

Start with the WIF*macros to find out what kindof thing happened, then use that information to decide which other W*macros to use to get the details.

WIF*宏开始找出发生了什么的事情,然后使用该信息来决定使用哪些其他W*宏来获取详细信息。

if(WIFEXITED(status)) {
  /* The child process exited normally */
  printf("Exit value %d\n", WEXITSTATUS(status));
} else if(WIFSIGNALED(status)) {
  /* The child process was killed by a signal. Note the use of strsignal
     to make the output human-readable. */
  printf("Killed by %s\n", strsignal(WTERMSIG(status)));
} else {
  /* ... you might want to handle "stopped" or "continued" events here */
}

回答by Marged

There is no standard defined which exit codes an application has to set in certain situations. It is totally up to the programmer which exit codes represent which error or even success !

没有标准定义应用程序在某些情况下必须设置哪些退出代码。退出代码代表哪个错误甚至成功完全取决于程序员!

Sometimes programmers decide that any value different from zero signals an error, and sometimes this value equals the operating systems error codes.

有时程序员认为任何不同于零的值都表示错误,有时这个值等于操作系统错误代码。

On Windows exit code 11 mightbe used because of problems with a file. If you want the description of this error code (which is specific to Windows and not necessarily your application) run net helpmsg 11.

在 Windows 上,由于文件问题,可能会使用退出代码 11 。如果您需要此错误代码的描述(特定于 Windows,不一定是您的应用程序),请运行net helpmsg 11.