How can I prevent zombie child processes?

thejh picture thejh · Jun 10, 2013 · Viewed 22k times · Source

I am writing a server that uses fork() to spawn handlers for client connections. The server does not need to know about what happens to the forked processes – they work on their own, and when they're done, they should just die instead of becoming zombies. What is an easy way to accomplish this?

Answer

thejh picture thejh · Jun 10, 2013

There are several ways, but using sigaction with SA_NOCLDWAIT in the parent process is probably the easiest one:

struct sigaction sigchld_action = {
  .sa_handler = SIG_DFL,
  .sa_flags = SA_NOCLDWAIT
};
sigaction(SIGCHLD, &sigchld_action, NULL);