Daemon Server, Forking, Defunct Processes

Discussion in 'C Programming' started by Scottman, Sep 11, 2008.

  1. Scottman

    Scottman Guest

    I am writing a server daemon that forks a child for every incoming
    request. The child process terminates when the client closes the
    connection. My problem is that I am unsure how to prevent the child
    process from becoming defunct. Here is an over-simplified main
    function...

    int main(void) {

    // Daemonize
    int pid;
    if ((pid = fork()) != 0) {
    exit(0);
    }
    setsid();
    if ((pid = fork()) != 0) {
    exit(0);
    }

    // Assume this gets the socket descriptor, binds
    // the socket and listens on given port.
    int server_socket = create_server_socket(1234);

    int client_socket;

    while (1) {
    // Assume this accepts a request to the server socket
    client_socket = accept_socket_request(server_socket);

    if (!fork()) {
    // Child

    // Assume this initiates a text interaction
    // with the client, looping until the client
    // enters 'exit'. Then it returns.
    session();

    // Close the client connection
    shutdown(client_socket, 2);

    // The child exits
    exit(0);

    } else {

    // Parent
    waitpid(-1, NULL, WNOHANG);
    }
    }

    // Close the server socket
    shutdown(server_socket, 2);
    return 0;
    }

    What is happening is that if a client terminates a child process the
    parent isn't aware until a new connection/process is made. I think
    this is because of my placement of waitpid(). The result is that
    there is a defunct child sitting around until a new connection is
    made, then the defunct process is cleaned up. There is always at
    least 1 defunct process lying around. How can I remedy this?

    Thanks,
    Scott
    Scottman, Sep 11, 2008
    #1
    1. Advertising

  2. On 11 Sep 2008 at 4:40, Gordon Burditt wrote:
    >>There is always at least 1 defunct process lying around. How can I
    >>remedy this?

    >
    > If you do not care about getting the status of a terminated child,
    > nor do you want to keep track of which children have terminated,
    > something like:
    >
    > signal(SIGCHLD, SIG_IGN);
    > OR
    > signal(SIGCLD, SIG_IGN);
    >
    > in the parent may be appropriate (chances are your system has only
    > one of these signals).


    It may be worth pointing out that pre-2001 versions of POSIX allowed
    terminated child processes to become zombies even if the disposition of
    SIGCHLD is set to SIG_IGN - I seem to recall that Linux up to 2.4.* had
    this behavior.
    Antoninus Twink, Sep 11, 2008
    #2
    1. Advertising

  3. Scottman

    CBFalconer Guest

    Scottman wrote:
    >
    > I am writing a server daemon that forks a child for every incoming
    > request. The child process terminates when the client closes the
    > connection. My problem is that I am unsure how to prevent the
    > child process from becoming defunct. Here is an over-simplified
    > main function...


    The C language doesn't contain forks, child processes, etc. That
    makes this off-topic on c.l.c. Try comp.unix.programmer.

    --
    [mail]: Chuck F (cbfalconer at maineline dot net)
    [page]: <http://cbfalconer.home.att.net>
    Try the download section.
    CBFalconer, Sep 11, 2008
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Gordon Beaton

    <defunct> processes

    Gordon Beaton, Oct 27, 2003, in forum: Java
    Replies:
    4
    Views:
    10,828
    Andrew
    Nov 7, 2003
  2. John Abel
    Replies:
    2
    Views:
    3,269
    John Abel
    May 12, 2005
  3. Srini Vuggumudi

    Script to kill defunct and zombie processes

    Srini Vuggumudi, Apr 29, 2004, in forum: Perl Misc
    Replies:
    4
    Views:
    575
    Chris Mattern
    May 1, 2004
  4. Srini Vuggumudi

    Script to kill defunct and zombie processes

    Srini Vuggumudi, Apr 29, 2004, in forum: Perl Misc
    Replies:
    0
    Views:
    150
    Srini Vuggumudi
    Apr 29, 2004
  5. Richard

    Avoiding defunct processes

    Richard, Nov 2, 2012, in forum: Python
    Replies:
    2
    Views:
    241
    Nobody
    Nov 2, 2012
Loading...

Share This Page