c - 父进程如何找到导致其子进程死亡的进程的pid?

标签 c linux unix signals freebsd

当父进程由于其子进程死亡而收到 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/

相关文章:

c - 使用奇怪的权限打开创建文件

java - 使用JNI时如何设置 `jps`显示的主类?

MPI sendbuf 和 recvbuf 可以是一回事吗?

python - 如何在 windows 和 linux (apache) 上使用相同的 python cgi 文件?

linux - 键盘按键是否可能不生成扫描码?

linux - 在 linux 中创建一个大小为 128k 的文件,其中的数据有 128k 个

c - 如何使用ImageMagick C API的MagickGetImageHistogram

linux - 扩展 iptables 内核模块

linux - 参数未传递给 shell 脚本 copyfile.sh : 1: copyfile. sh: [d: 未找到

linux - 如何按日期批量重命名文件?