Linux fork() zombie processes problems..
Recently at work I was porting some code from HP-UX to Linux, and ran into a problem. The code in question belonged to a service which listens on a socket, and forks a child to handle each incoming request. The children among other things execute a sub-process using system(), and check it’s return value to know if the sub-process executed successfully. When you fork without calling wait() your children become zombies apparently: I guess it’s because nobody is reaping their return values or something.
To fix this, on HP-UX you apparently do this:
signal(SIGCLD,SIG_IGN);
Note that the signal used is SIGCLD not SIGCHLD, this is probably due to the age of the code. If this same “fix” is applied on Linux however, the system() function can’t return the sub-processes return value, because the parent program is already set to ignore SIGCLD (and SIGCHLD?). I initially solved this by having the parent process wait() for it’s children, but failed to realize that wait() is a blocking call, so it effectively slowed my server down to processing 1 request at a time: BAD.
When I setup a signal handler for SIGCHLD like in the link below however, my problems were solved: my parent process didn’t block, and forked for new requests, no zombie processes (because of waitpid() in the SIGCHLD handler) and best of all, my system() calls managed to return the return values of the sub-processes.
Thank you Linux Programming Blog
