Thread: Reaping Zombie Processes

  1. #1
    Registered User
    Join Date
    Nov 2007
    Posts
    96

    Reaping Zombie Processes

    I created a server process which forks and uses a child process to handle the client's request. I was just making sure that I properly reap any zombie processes which I may have. Is this accomplished through the waitpid() method? Here is a copy of my server code:

    Code:
          msock = myTCPserverSocket(port, argv[0]); 
    
    
    	/* 
    	* Now accept connections and service clients forever
    	*/
    	while(1) {
    		/*Accept the client's socket*/
    	      socket = myTCPaccept(msock, argv[0]);
    
    		if( (pid=fork()) > 0) { //Parent Process
    			myTCPclose(socket);  //Child process is now the only one with access to this socket
    	
    
    			if( (wpid = waitpid(pid, &stat, 0)) != pid) {
    				fprintf(stderr, "Signal was received by parent while waiting...Error\n");
    				fprintf(stderr, "ERRNO:  %s\n", strerror(errno));
    				exit(1);
    			}
    		}
    		else if (pid==0) { //Child Process
    			myTCPclose(msock); //Close the "master socket" as we only care about the socket which was newly created in this process
    		
    			pid = getpid();
    
    	            if((serverGet = myHTTPserverGet(socket, path, from, userAgent, &sleepTime)) < 0) {
                      	fprintf(stdout, "Trouble in myHTTPserverGet.\nReturn Value = %d\n", serverGet);
    	                  exit(1);
    	            }
    
    			sleep(sleepTime);			
    	
    			/*Send the error code from myHTTPserverGet and path of the file to be written back to the client to myHTTPserverResponse*/
    			if((serverResp =myHTTPserverResponse(socket, userAgent, serverGet, path, sleepTime, pid)) < 0) {
    				fprintf(stdout, "Trouble in myHTTPserverResponse.\nReturn Value = %d\n", serverResp);
    				exit(1);
    			}
    
    			exit(1);
    		}
    		else {  //Error in Fork Call
    			fprintf(stderr, "There was an error in the fork call\n");
    			fprintf(stderr, "ERRNO: %s\n", strerror(errno));
    			exit(1);
    		}
    
      //          myTCPclose(socket);
    	}
    }

  2. #2
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by NuNn View Post
    I created a server process which forks and uses a child process to handle the client's request. I was just making sure that I properly reap any zombie processes which I may have.
    Unless you notice some, I wouldn't worry about that. If you look with ps, the processes will appear <defunct> after they exit, which is fine.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  3. #3
    Registered User
    Join Date
    Nov 2007
    Posts
    96
    One more thing....If I want to send multiple requests to my server, I would like to be able to handle each of these requests at the same time (the reason for the fork). But as my program is set up at the moment, if I send a request to the server from one client which says to sleep for 20 seconds then process the request. Then I send a request from another client saying to sleep for only 2 seconds and respond. But the client who sends the 20 second request gets served before the client with the 2 second sleep request. What do i have to change in my parent or in the code here to fix this. Thanks!

  4. #4
    Complete Beginner
    Join Date
    Feb 2009
    Posts
    312
    You may install a signal handler for SIGCHLD to explicitly wait() for a child process once it has terminated. On some systems, explicitly doing signal(SIGCHLD, SIG_IGN), which is the default behaviour, may prevent processes from becoming zombies.

    http://en.wikipedia.org/wiki/Zombie_process seems to be a pretty good place to get more information.

    If you're not interested in a child's return status anyway, you may have the child be adopted by init, which takes care of wait() and prevents the child from becoming a zombie. Unfortunately, I've completely forgotten how to do it. You may want to search Google for daemon processes and how they detach from its parent in order to fork into background. If you do that, be aware that a child (or any other new process) may then get the same PID as a previous child which has already been killed and reaped by init. In other words, the PID of a child is completely useless in this case.

    Greets,
    Philip
    All things begin as source code.
    Source code begins with an empty file.
    -- Tao Te Chip

  5. #5
    Registered User
    Join Date
    Nov 2007
    Posts
    96
    Thank you I will take a look at that

  6. #6
    Registered User
    Join Date
    Nov 2007
    Posts
    96
    Alright, when I was doing some research on process I read that the parent either continues to execute concurrently with its children or it waits until some or all have terminated. In this program I would like my parent to keep running concurrently with my child processes, but I want to ensure that all of them terminate accordingly and no zombies remain. I assume that it is the waitpid() that is causing my parent to hold up until the first child terminates and the second is processed. How would I set up my code here so that I am able to satisfy handling multiple requests concurrently and ensuring no zombies exist. thanks!

    NOTE: After looking at the wikipedia article what Snafuist directed me to I see the issue in my code. Now should I just have a singe waitpid statement outside the fork() section and will that solve it?
    Last edited by NuNn; 03-10-2009 at 04:58 PM.

  7. #7
    Complete Beginner
    Join Date
    Feb 2009
    Posts
    312
    I assume that it is the waitpid() that is causing my parent to hold up until the first child terminates and the second is processed.
    Right.

    How would I set up my code here so that I am able to satisfy handling multiple requests concurrently and ensuring no zombies exist.
    Exactly as I said before:

    You may install a signal handler for SIGCHLD to explicitly wait() for a child process once it has terminated. On some systems, explicitly doing signal(SIGCHLD, SIG_IGN), which is the default behaviour, may prevent processes from becoming zombies.
    Greets,
    Philip
    All things begin as source code.
    Source code begins with an empty file.
    -- Tao Te Chip

  8. #8
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by Snafuist View Post
    If you're not interested in a child's return status anyway, you may have the child be adopted by init, which takes care of wait() and prevents the child from becoming a zombie. Unfortunately, I've completely forgotten how to do it.
    The only portable way for the parent to relinquish ownership of the child is for the parent itself to exit. This can be accomplished by a second call to fork() followed by exit():

    Code:
    void spawn_and_forget()
    {
        if(fork() == 0)
        {
            exec(child);
            exit(1);
        }
        if(fork() != 0)
        {
            exit(0);
        }
    }
    The original parent exits, causing the child to be re-parented to init, which will reap it when it exits. But this is a screaming hack. Just reap your own children with a signal handler (if you want "fire-and-forget" behavior) or with one of the various wait() functions which allow non-block.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. init adopts zombie process?
    By password636 in forum Linux Programming
    Replies: 4
    Last Post: 07-01-2009, 10:05 AM
  2. Replies: 34
    Last Post: 05-27-2009, 12:26 PM
  3. Unreapable zombie
    By brewbuck in forum Linux Programming
    Replies: 5
    Last Post: 03-30-2009, 09:04 PM
  4. Fork Zombie Processes
    By valaris in forum Linux Programming
    Replies: 4
    Last Post: 09-05-2008, 08:16 AM
  5. binary tree of processes
    By gregulator in forum C Programming
    Replies: 1
    Last Post: 02-28-2005, 12:59 AM