Socket accept and waitpid in parent

71 views Asked by At

I have a parent that is waiting for a pool and then do a waitpid in a while loop.

poll_list[0].fd = server_fd;
poll_list[0].events = POLLIN;
retval = poll(poll_list, 1, 3500);
if (retval == -1)
{
    my_error(stderr,
             "%s: Error setting pool %d", __func__, __LINE__);
    exit(EXIT_FAILURE);
}
if (retval > 0)
{ // There was an accept
    if ((new_socket = accept(server_fd, (struct sockaddr *)&address, (socklen_t *)&addrlen)) < 0)
    {
        my_error(stderr,
                 "%s: Accepted error of near line %d", __func__, __LINE__);
        exit(EXIT_FAILURE);
    }
}
...
// Forker pour créer un processus enfant
while (waitpid(-1, NULL, WNOHANG) > 0);

However during 3,5s, I may have zombie processes. I can reduce the pool timeout but is there a better solution to manage socket accept and children management?

zombie processes

Thanks.

With SIGCHLD, I have this :

void childsignal()
{
    pid_t status;
    int pid;
    while ((pid=waitpid(-1, &status, WNOHANG))>0)
    {
        my_error(stderr, "Parent: The child process %d caught signal %d and exited", pid, status);
        if (!WIFEXITED(status)) // process exit abnormally, need to log.
            my_error(stderr, "Parent: The child process %d caught signal %d and exited", pid, status); //Never executed
    };
}

For instance, if process a exits succesfully, first error would log but if process b is killed by a signal, nothing would show.

0

There are 0 answers