We could simply set the SIGCHLD signal to be handled by SIG_IGN (ignored), and based on POSIX.1-2001 it will clean up the terminated child process.
struct sigaction sa;
sa.sa_handler = SIG_IGN;
sigemptyset(&sa.sa_mask);
sa.sa_flags = 0;
if (sigaction(SIGCHLD, &sa, 0) == -1) {
perror(0);
exit(1);
}
Reference: http://www.microhowto.info/howto/reap_zombie_processes_using_a_sigchld_handler.html