我在一个产生许多孩子的过程中有SIGCHLD
的处理程序。
此代码位于我的处理程序中,pid
始终为1
。我已经读过pid
应该是子进程id
。为什么我一直回来1
?
int status;
pid_t pid;
while(pid = waitpid(-1, &status, WNOHANG) > 0)
{
if (WIFEXITED(status))
printf("Child %ld: Ended properly and returned %d\n", pid, WEXITSTATUS(status));
else
printf("Child crashed\n");
}
初始化处理程序的代码。
void initSIGCHLDSignalHandler()
{
struct sigaction act;
memset(&act,'\0',sizeof(struct sigaction));
sigemptyset(&act.sa_mask);
act.sa_flags = SA_NOCLDSTOP | SA_RESTART;
act.sa_handler = SIGCHLD_SignalHandler;
sigaction(SIGCHLD, &act, NULL);
}
答案 0 :(得分:1)
我想你想要
while((pid = waitpid(-1, &status, WNOHANG)) > 0)
因为现在你得到的只是“真实”。 (您的编译器可能会对此发出警告,因此请阅读编译器警告。)