<分区>
当父进程由于其子进程死亡而收到 SIGCHLD 时,父进程如何获取导致其各自子进程死亡的进程的 pid?
<分区>
当父进程由于其子进程死亡而收到 SIGCHLD 时,父进程如何获取导致其各自子进程死亡的进程的 pid?
最佳答案
不幸的是,家长没有收到此信息。来自 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
fork )是 ptrace(2)
child 并等待它接收信号。
ptrace
的使用对于这个答案来说太复杂了。可能你的代码可以基于 strace(1)
,它以非侵入方式使用 ptrace
,并且已经允许事件过滤。
关于c - 父进程如何找到导致其子进程死亡的进程的pid?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46224781/