2

When a parent process receives SIGCHLD due to death of its child process, how can the parent get the pid of the process that that caused the death of its respective child process?

GIZ
  • 4,409
  • 1
  • 24
  • 43
  • 1
    Bad duplicate: this question asks about the *parent*, not the child. – o11c Sep 14 '17 at 17:12
  • [Handling SIGCHLD, how to record the return values of children as they die](https://stackoverflow.com/questions/5530904) – Martin Sep 14 '17 at 17:16

1 Answers1

0

Unfortunately, the parent does not receive this information. From 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)).

Therefore, the only way to get the information (other than by cooperation, which won't fork for SIGKILL anyway) is to ptrace(2) the child and wait for it to receive a signal.

The use of ptrace is far too complicated for this answer. Possibly you could base your code off of strace(1), which uses ptrace in a non-intrusive way and already allows event filtering.

o11c
  • 15,265
  • 4
  • 50
  • 75