Problem with SIGCHLD

  I have several child processes (about 10) forked by a parent. I have provided the SIGCHLD handler in the process and calling a wait() inside it. I observe that if I kill (kill -9) some of the child processes (say 5). Not all SIGCHLD are reaching the parent. The child processes for which SIGCHLD is not reaching become <defunct>. I would like to know what could be done to make all the child exits to be reported properly. Will waitpid(-1,...) help? If so with what options?

 Thanks in advance
Who is Participating?
PAQed, with points refunded (50)

Community Support Moderator
when you are handling a signal and same signal arrives, it is not delivered ... also if more than one children exit simultaneously, your application will see only one signal ... this is the default and proper behaviour of signal mechanism

if you are expecting situations like you have described, keep track of pids of children you have forked and use waitpid() for those who have exited. use WNOHANG in options to waitpid so that your program does not get stuck

you may find this interesting




    int main(void)
            pid_t dead_process ;

            int no_of_child_processes = 10 ;

            int i = 0, j = 0 ;

            int child_process[10] = {0,0,0,0,0,0,0,0,0,0};

            for( i = 0 ; i < no_of_child_processes ; i++ )
                   if(child_process[i] == 0)
                       printf( "Child no [%d] has pid = [%d]\n", i, child_process[i] ) ;
                    if( i == 9 )
            if( child_process[i] == 0 )
            if( child_process[i] != 0 )
                  for( i=0 ; i<no_of_child_processes ; i++ )
                      dead_process=waitpid( child_process[i], (int*)0, WNOHANG);
                      if( dead_process > 0 )
                         printf( "Process [%d] is dead\n", dead_process ) ;

Execute this program it provide u messages for every that u kill  (kill -9 1 2 3 4 . . .) or dies .

You can't use  signal handlers to achieve this because the data structures used only have the capability to
remember which signal occurred. They can't remember the number of times a particular signal ocuured.

This is reason for processes becoming defunct when u do kill -9 <more tham one process>.
( The signal handler is invoked only once, for  all the pids specified in a single kill command and accordingly wait OR waitpid ( in the signal handler  is invoked once, which displays death message ONLY for one child).  If you insert the infinite loop of this program into your signal handler it will work.
But this is not advised practically, Just test whether this change works or Not.

Insert a comment here if  you need more help or if this does not work and I will getback.



sgupta001Author Commented:
Hi TriShakti,
  I have already found a solution that works. It is also based on waitpid.
 Like inserting the following lines in the SIGCHLD handler:

       while((stat = waitpid(-1,(int*)0, WNOHANG|WUNTRACED)) > 0)
            // one child process died

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.