Devember: Kill the zombies

This post is part of the Devember collection, useful for the study of the Socket API.

Devember: Kill the zombies

In the previous post, we have seen the server spawning child processes to handle multiple incoming connections at the same time. However, we must not forget to handle these processes when they end otherwise they will remain in memory as zombies.

When a child process terminates, the operating system sends a SIGCHLD signal to the father which by default do not do anything. To remove the zombies (dead child processes), the father must register a handler function for the SIGCHLD signal:

if (signal(SIGCHLD, handle_zombies) < 0) {
	return -1;

Below it’s shown the handler function. By calling the waitpid function with -1 as first parameter, the father process waits for the termination of any one of its children. The stat variable will contain information about the exit status.

void handle_zombies(int signo) {
    pid_t pid;
    int stat;

    while ((pid = waitpid(-1, &stat, WNOHANG)) > 0) {
        printf("Child %d terminated with exit status %d\n", pid, stat);

The complete code can be found on this repository.

6 December 2015