How to mix orphan an non orphan child ?
scenario : a remanent daemon spawns different child process depending of incomming tcp requests. Some will be orphan : I do not care of their life or their status. For some others non orphan : I want to get an end status or to know if they crash.
What means an orphan for me in C
struct sigaction sa;
sa.sa_handler = SIG_IGN;
sa.sa_flags = SA_NOCLDWAIT;
sigemptyset(&sa.sa_mask);
sigaction(SIGCHLD, &sa, NULL);
pid = fork
--> no zombie created when child end
For non orphan,
struct sigaction sa;
sa.sa_handler = SIG_DFL;
sa.sa_flags = 0;
sigemptyset(&sa.sa_mask);
sigaction(SIGCHLD, &sa, NULL);
pid = fork
if (pid == 0) {
child
}
else {
--> zombie created when child end until I get the status
waitpid(pid, &status, WNOHANG);
// pid could be -1 for many
}
My problem :
After many non orphan child, when I create my first orphan child, I can't get anymore status of child non orphan. I just get ECHILD error from waitpid when last child end. If I recreate non orphan child, orphan child creates zombie.
It seems impossible to get orphan and non orphan cohabitation.
Did someone find a solution to fix it ? or could point the weakness of my algorithm ?
|