How do I prevent them from occuring?

How do I prevent them from occuring?


You need to ensure that your parent process calls wait() (or
waitpid(), wait3(), etc.) for every child process that
terminates; or, on some systems, you can instruct the system that you
are uninterested in child exit states.



Another approach is to fork() twice, and have the
immediate child process exit straight away. This causes the grandchild
process to be orphaned, so the init process is responsible for cleaning
it up. For code to do this, see the function fork2() in the
examples section.



To ignore child exit states, you need to do the following (check your
system's manpages to see if this works):




struct sigaction sa;
sa.sa_handler = SIG_IGN;
#ifdef SA_NOCLDWAIT
sa.sa_flags = SA_NOCLDWAIT;
#else
sa.sa_flags = 0;
#endif
sigemptyset(&sa.sa_mask);
sigaction(SIGCHLD, &sa, NULL);



If this is successful, then the wait() functions are prevented
from working; if any of them are called, they will wait until all
child processes have terminated, then return failure with
errno == ECHILD.



The other technique is to catch the SIGCHLD signal, and have the signal
handler call waitpid() or wait3(). See the examples
section for a complete program.






Home FAQ