Thread: Fork Zombie Processes

  1. #1
    Registered User valaris's Avatar
    Join Date
    Jun 2008
    Location
    RING 0
    Posts
    507

    Fork Zombie Processes

    Hello,

    I have been going through stevens advanced unix programming book and have recently written a webserver. When adding forks to service incoming requests in the child and continue listening for new clients on the parent I came to a problem. Stevens says either a return from main or an exit in the child will kill the process, either that or wait() on the parent for the child. When calling exit or return from the child the processes are not being deleted but put into zombie states. Is there something I'm doing wrong? My server is long so ill give the general gist of my program.

    Code:
    int main()
    {
    if(fork() == 0)
    {
    //handle client etc etc
    return 0;
    }
    else
    {
    //parent
    //block on accept.
    }
    }
    So I guess my question: Is return/exit not the way to ensure a child process is completely killed and not just made into a zombie? If not how can I get rid of my process without blocking on wait()?

    Thanks =D

  2. #2
    Registered User valaris's Avatar
    Join Date
    Jun 2008
    Location
    RING 0
    Posts
    507
    I suppose I could just handle the SIGCHLD signal and call wait in that? I dont know if that would be most efficent though?

  3. #3
    Malum in se abachler's Avatar
    Join Date
    Apr 2007
    Posts
    3,195
    Rather than fork()ing, try creating additional threads. Threads wont kill the process when they terminate.

  4. #4
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Stevens says either a return from main or an exit in the child will kill the process, either that or wait() on the parent for the child.
    That's incorrect.
    A return from main or an exit in the child will end the child. It will remain a zombie, though.
    A wait in the parent will not do anything except block the parent, if the child is running. A wait collects zombies, though.

    So you need both - the child must end and the parent must wait on it to remove the zombie. To avoid being blocked in the parent, wait for SIGCHLD and do the wait in the handler.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  5. #5
    Registered User valaris's Avatar
    Join Date
    Jun 2008
    Location
    RING 0
    Posts
    507
    Ahh ok thankyou very much, just making sure that was most efficient.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Reaping Zombie Processes
    By NuNn in forum C Programming
    Replies: 7
    Last Post: 03-10-2009, 05:50 PM
  2. Fork multiple processes()??
    By Paul22000 in forum C Programming
    Replies: 8
    Last Post: 11-12-2008, 04:47 PM
  3. Fork => zombie => error
    By Morbo in forum Linux Programming
    Replies: 1
    Last Post: 12-08-2005, 11:53 AM
  4. binary tree of processes
    By gregulator in forum C Programming
    Replies: 1
    Last Post: 02-28-2005, 12:59 AM
  5. fork(), exit() - few questions!
    By s3t3c in forum C Programming
    Replies: 10
    Last Post: 11-30-2004, 06:58 AM