Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how a process know's which child ended?

Tags:

c

signals

sigchld

when a process child is terminated he's sending a SIGCHLD to the parent process. Now, if the parent process have more than one child how the parent process knows which child had sent the signal?

like image 883
orenma Avatar asked Sep 06 '25 23:09

orenma


2 Answers

The wait() syscall returns the pid of the child that terminated. You can call wait() in your SIGCHLD handler to determine which child terminated.

From the man page:

       wait(): on success, returns the process ID of the terminated child;
   on error, -1 is returned.
like image 111
shanet Avatar answered Sep 11 '25 17:09

shanet


In addition to the wait() family of calls as in @shanet's answer, the SIGCHLD itself carries that information.

The si_pid member of the siginfo_t member passed to a three-argument signal handler (SA_SIGINFO) contains the child's PID.

like image 44
pilcrow Avatar answered Sep 11 '25 17:09

pilcrow