Fork子进程终止返回信号0的原因及代码问题排查
问题原因与修复方案
你得到信号0的直接原因是错误使用了进程状态宏:
当WIFSIGNALED(status)为真时(表示子进程被信号终止),应该用WTERMSIG(status)获取导致进程终止的信号;而你用了WSTOPSIG(status)——这个宏的作用是获取导致进程暂停的信号,仅在WIFSTOPPED(status)为真时才有效。当进程是被信号终止而非暂停时,调用WSTOPSIG的结果是未定义的,这里恰好返回了0。
修复后的代码
修改debug_wait函数,替换错误的宏:
int debug_wait() { int status; wait(&status); if (WIFSIGNALED(status)) { int sig = WTERMSIG(status); // 替换为WTERMSIG printf("tests failed with signal %d (%s)\n", sig, strsignal(sig)); return 1; } else if (!WIFEXITED(status)) { printf("tests ended in an unexpected way\n"); return 1; } return 0; }
验证说明
你的示例程序中,子进程因重复释放内存触发了SIGABRT信号(通常是信号6),修复后运行应该会输出类似:
double free or corruption (out) tests failed with signal 6 (Aborted)
这才是正确的终止信号信息。
内容的提问来源于stack exchange,提问作者Tenobaal
相关产品推荐
相关产品推荐

