我試圖確定執行是否通過檢查waitpid函數的結果失敗()。但是,即使當我跑,我知道失敗並寫入這個問題到stderr命令,檢查以下從未登記。這段代碼可能有什麼錯誤?叉/ EXEC/waitpid函數問題
感謝您的任何幫助。
pid_t pid; // the child process that the execution runs inside of.
int ret; // exit status of child process.
child = fork();
if (pid == -1)
{
// issue with forking
}
else if (pid == 0)
{
execvp(thingToRun, ThingToRunArray); // thingToRun is the program name, ThingToRunArray is
// programName + input params to it + NULL.
exit(-1);
}
else // We're in the parent process.
{
if (waitpid(pid, &ret, 0) == -1)
{
// Log an error.
}
if (!WIFEXITED(ret)) // If there was an error with the child process.
{
}
}
'WIFEXITED'用於區分'WIFSIGNALED'和'WIFSTOPPED'。在正常情況下,當孩子失敗時,「WIFEXITED」爲真(以非零狀態退出)。你需要檢查'WIFEXITED'和'WEXITSTATUS'。 –