From 7e49c722ab6c45467c27a309e494cee0b040cd68 Mon Sep 17 00:00:00 2001 From: dm1sh Date: Sat, 15 Oct 2022 11:45:17 +0300 Subject: [PATCH] Finally, reaping all zombies --- src/server.c | 22 +++++++++++++++++----- 1 file changed, 17 insertions(+), 5 deletions(-) diff --git a/src/server.c b/src/server.c index 20e3626..c014b38 100644 --- a/src/server.c +++ b/src/server.c @@ -4,6 +4,7 @@ #include #include #include +#include #include #include #include @@ -59,9 +60,20 @@ void handle_connection(int fd) close(fd); } +/** + * @brief Handles child process removal (to prevent zombies) + * + * @param signum + */ void handle_process_termination(int signum) { - wait(NULL); + int status; + pid_t pid; + + do { + pid = waitpid(-1, &status, WNOHANG); + } + while (pid > 0); } /** @@ -94,6 +106,8 @@ int main(int argc, char *argv[]) } printf("Waiting for connection on port %s...\n", port); + signal(SIGCHLD, handle_process_termination); + while (1) { socklen_t sin_size = sizeof cli_addr; @@ -122,11 +136,9 @@ int main(int argc, char *argv[]) handle_connection(client_fd); exit(0); } - else { + else close(client_fd); - signal(SIGCHLD, handle_process_termination); - } } return 0; -} \ No newline at end of file +}