我正在尝试通过检查waitpid()的结果来确定执行是否失败。然而,即使我运行一个已知会失败并将问题写入stderr的命令,下面的检查也没有被注册。这段代码可能出了什么问题?
感谢任何帮助。
感谢任何帮助。
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
为 true。您需要检查WIFEXITED
和WEXITSTATUS
。 - William Pursell