从
wait(2)&co返回时,将8位返回码和8位终止信号编号混合为一个值。。
#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 %un", WEXITSTATUS(status)); child = fork(); if (child <= 0) kill(getpid(), SIGSEGV); waitpid(child, &status, 0); if (WIFSIGNALED(status)) printf("second child died with %un", WTERMSIG(status)); }
您如何确定退出状态?传统上,外壳程序仅存储8位返回码,但如果进程异常终止,则将高位设置为高位。
$ sh -c'出口42'; 回声$?42$ sh -c'kill -SEGV $$'; 回声$?分段故障139$ expr 139-12811
如果您看到的不是这个,则程序可能有一个
SIGSEGV信号处理程序,该信号处理程序随后
exit会正常调用,因此实际上并没有被信号杀死。(程序可以选择处理
SIGKILL和以外的任何信号
SIGSTOP。)
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)