signal handler for SIGCHLD gets called twice if parent process also gets killed
Posted on 2003-10-23
I have a parent process that forks a child process and from there execlp another executable. The parent process and the exe uses pipes for IPC. The parent process has a signal handler that handles the SIGCHLD signal as follows :
struct sigaction act;
act.sa_handler = SIG_IGN;
act.sa_flags = 0;
sigaction(SIGPIPE, &act, 0);
act.sa_handler = child_killed; //handler function
act.sa_flags = SA_NOCLDSTOP | SA_NODEFER;
In the child_killed(), I close the read end of the pipe.
Some times just after the child process exits, the parent process goes 100% (at times I see that the signal handler gets called in logs and some times i do not see the logs, so i cannot be sure that everytime the problem happens the signal handler gets called in parent). Also the child process becomes a Zombie. After I kill and restart the Parent process I see in its logs that the signal handler is called again but this time with child pid as -1.
Can you please tell me why the signal handler is getting called in the newly started parent process.
Also I would like to know that whether there is a chance that even after the child exits the signal handler doesn't gets called in the parent.