一个过程如何知道哪个孩子结束了?

时间:2013-11-06 13:21:57

标签: c signals sigchld

当进程子进程被终止时,他正在向父进程发送SIGCHLD。现在,如果父进程有多个子进程,父进程如何知道哪个子进程发送了信号?

2 个答案:

答案 0 :(得分:2)

wait()系统调用返回已终止子级的pid。您可以在wait()处理程序中调用SIGCHLD以确定哪个子项已终止。

来自man page

       wait(): on success, returns the process ID of the terminated child;
   on error, -1 is returned.

答案 1 :(得分:1)

@shanet's answer中的wait()系列电话外,SIGCHLD本身也会传输该信息。

传递给三参数信号处理程序(si_pid)的siginfo_t成员的SA_SIGINFO成员包含子项的PID。