在waitpid()之后来自WTERMSIG宏的异常信号数字
Unusual signal numbers from WTERMSIG macro after waitpid()
在等待子进程终止时,我从以下代码中看到异常信号数字(例如 50、80 或 117)。 我只从一个特定的子进程中看到了这一点,我无法访问进程源代码,它只在某些时候发生。
我想知道这些不寻常的值是什么意思,给定NSIG == 32
,以及在哪里可以找到标题或手册页中的一些文档?
请注意,此代码在循环中运行,发送越来越危险的信号,直到子级终止。
int status, signal;
if (waitpid(m_procId, &status, WNOHANG) < 0) {
LOGERR << "Failed to wait for process " << name() << ": " <<
strerror(errno) << " (" << errno << ")";
break;
} else if (WIFEXITED(status)) {
m_exitCode = WEXITSTATUS(status);
terminated = true;
LOGINF << "Process " << name() << " terminated with exit code " << m_exitCode;
} else if (WIFSIGNALED(status)) {
signal = WTERMSIG(status); // !!! signal is sometimes 50, 80 or 117 !!!
terminated = true;
LOGINF << "Process " << name() << " terminated by signal " << signal;
} else {
LOGWRN << "Process " << name() << " changed state but did not terminate. status=0x" <<
hex << status;
}
这是在OSX 10.8.4下运行的,但我也在10.9 GM种子中看到过它。
编辑 按如下方式修改代码使代码更加健壮,但是有时子进程会被孤立,因为我想循环不足以杀死子进程。
else if (WIFSIGNALED(status)) {
signal = WTERMSIG(status);
if (signal < NSIG) {
terminated = true;
LOGINF << "Process " << name() << " terminated by signal " << signal;
} else {
LOGWRN << "Process " << name() << " produced unusual signal " << signal
<< "; assuming it's not terminated";
}
}
请注意,此代码是此类的 Process::unload()
方法的一部分。
在 waitpid 的 OS X 手册页中,指定 WNOHANG 时,应检查返回值是否为 0:
When the WNOHANG option is specified and no processes wish to report status, wait4() returns a process
id of 0.
The waitpid() call is identical to wait4() with an rusage value of zero. The older wait3() call is the
same as wait4() with a pid value of -1.
发布的代码没有检查这一点,这对我来说表明 status
的值可能是垃圾(int 的值永远不会初始化)。 这可能会导致您看到的内容。
编辑:status
确实仅在waitpid
返回>0时设置。
相关文章:
- Qt VTK交互风格的信号到小部件
- 处理多个异常集合的C++方法
- 我在c++代码中生成了一个运行时#3异常
- 孤立代码块在结构中引发异常
- 获取日期异步信号安全吗?如果在信号处理程序中使用,它会导致死锁吗
- C++中的赋值发生,尽管右侧出现异常
- 如何将点击的信号和插槽添加到qt中的自定义按钮中
- 从构造函数抛出异常时如何克服内存泄漏
- 异常属于C++中的线程还是进程
- 当类定义不可见时捕获异常
- 在信号处理程序中捕获C++未处理的异常并恢复应用程序
- 信号后未捕获异常
- 替换 QObject 与信号和插槽一起使用时的异常
- 在waitpid()之后来自WTERMSIG宏的异常信号数字
- 我可以从Qt中的异常处理程序中发出信号吗
- 通过QT的信号/插槽系统传递通用异常作为参数
- 为什么 SIGSEGV 的信号处理程序没有捕获我的C++抛出异常?
- 是否有事实上的标准例外或最佳实践方法来发出异常"invalid input"信号?
- 捕获 Segfault 或任何其他错误/异常/信号C++例如在 Java 中捕获异常
- 为什么我的信号处理程序(引发异常)不会多次触发?