父进程如何找到导致其子进程死亡的进程的PID?

父进程如何找到导致其子进程死亡的进程的PID?

问题描述:

当父进程因其子进程死亡而收到SIGCHLD时,父进程如何获得导致其相应子进程死亡的进程的PID?父进程如何找到导致其子进程死亡的进程的PID?

+1

错误重复:此问题询问*父母*,而不是孩子。 – o11c

+0

[处理SIGCHLD,如何记录孩子死亡时的返回值](https://stackoverflow.com/questions/5530904) – Martin

不幸的是,家长没有收到这些信息。从sigaction(2)

* SIGCHLD fills in si_pid, si_uid, si_status, si_utime, and si_stime, 
    providing information about the child. The si_pid field is the 
    process ID of the child; si_uid is the child's real user ID. The 
    si_status field contains the exit status of the child (if si_code is 
    CLD_EXITED), or the signal number that caused the process to change 
    state. The si_utime and si_stime contain the user and system CPU 
    time used by the child process; these fields do not include the times 
    used by waited-for children (unlike getrusage(2) and times(2)). In 
    kernels up to 2.6, and since 2.6.27, these fields report CPU time in 
    units of sysconf(_SC_CLK_TCK). In 2.6 kernels before 2.6.27, a bug 
    meant that these fields reported time in units of the (configurable) 
    system jiffy (see time(7)). 

因此,为了得到的信息(比其他的合作,这将不会为SIGKILL叉反正)的唯一方法是ptrace(2)孩子并等待它接收的信号。

使用ptrace对于这个答案太复杂了。可能您可以将您的代码从strace(1)中删除,该代码以非侵入方式使用ptrace,并且已允许事件过滤。