从 waitpid 返回的 "termination status" 和 "end status" 有什么区别?

What is the difference between "termination status" and "end status" as returned from waitpid?

在 Andrew S. Tanenbaum 和 Albert S. Woodhull 的操作系统设计与实现中,有以下片段:

"[...] consider exit, which processes should use when they are finished executing. It has one parameter, the exit status (0 to 255), which is returned to the parent via statloc [as in waitpid(int, int *statloc, int)] in the waitpid system call. The low-order byte of status contains the termination status, with 0 being normal termination and the other values being various error conditions. The high-order byte contains the child's exit status (0 to 255)."

那么,这两个概念有什么区别呢?

退出状态是进程本身在调用 exit(或来自 main 的 return 值时提供的内容,它被定义为与调用 exit).

但这不是终止进程的唯一方法。特别是,它可以被一个信号终止,如果程序出现段错误或被零除,或者如果其他进程向它发送终止信号,就会发生这种情况。在那些情况下,没有退出状态,因为程序永远无法调用 exit()。然后,终止状态指示程序是否调用了 exit(),或者,如果没有,则指示导致终止的信号的编号。可能还有一些特定于实现的标志;例如,Linux 如果程序因信号终止而创建核心转储,则在终止状态中设置一个标志。

请参阅 main waitpid 了解可用于从 waitpid 编辑的状态值 return 中提取此信息的宏。

退出状态指的是进程在正常程序终止时传递给exit函数(或从main返回)的值。

终止状态是指导致程序结束或停止的原因。换句话说,它是正常退出(如上所述),还是被信号终止。这也可以指示进程是否被信号停止(但未终止),或者它是否在停止后继续。调试进程时可能会发生这种情况。