这是我正在运行的程序的一个片段,但纠正它的机器会尝试这样做:
strace ./watcher echo 2>&1 > /dev/null | grep wait | cut -c1-4
预期的输出是:
wait
但是我的程序会打印随机数量的等待(像这样):
wait
wait
wait
wait
wait
wait
这是代码:
// creates a child process
pid_t process_id;
int status;
process_id = fork();
switch(process_id)
{
case -1: // Error while making the forkFork failed
fprintf(stderr, "Fork failed.\n");
break;
case 0: // Child process.
execvp(command[0], command); // command here is char**
// execvp only returns when error.
Error(argv[0], 1); // Error just prints the error
exit(1);
break;
default: // Parent process.
while(!waitpid(process_id, &status, WNOHANG));
finished(status); // It prints how the process has finished.
}
我认为问题在于内部有waitpid,这会产生很多等待。但是,如果我删除它并单独留下 waitpid,我会收到以下输出:
standard input: Input/output error
有没有办法只打一个等待电话而不会出现该错误?