在waitpid()之后来自WTERMSIG宏的异常信号数字

Unusual signal numbers from WTERMSIG macro after waitpid()

本文关键字:异常 信号 数字 WTERMSIG waitpid 之后 后来      更新时间:2023-10-16

在等待子进程终止时,我从以下代码中看到异常信号数字(例如 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时设置。