2013-09-30 50 views
0

當等待子進程終止時,我看到以下代碼中出現異常信號編號(例如50,80或117)。我只從一個特定的子進程中看到了這一點,而且我無法訪問進程源代碼,並且只在某些時候發生。waitpid後來自WTERMSIG宏的不尋常信號編號()

我想知道這些不尋常的值是什麼意思,給出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"; 
    } 
} 

注意這個代碼是這個classProcess::unload()方法的一部分。

回答

2

OS X manpage for waitpid,specifing 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

+0

是啊,這肯定是爲「搞笑」的信號值的原因,但是你可以解釋爲什麼'waitpid函數()'將返回'0'和但孩子還活着?我做了處理'0'返回的變化,孩子終止了,但是它需要幾個循環。 – trojanfoe

+0

我也使用'WUNTRACED'標誌'waitpid()',它似乎解決了這個問題。我無法獎勵另外4個小時的獎勵,所以如果我忘了請提醒我。非常感謝您的幫助。 – trojanfoe

+0

很高興幫助! 'waitpid'只會報告停止或已經終止的子節點的狀態(並返回> 0)。這聽起來像你使用'WUNTRACED'已經處理了這個,但是。 – MikeGM