[comp.unix.questions] How to clean out Zombies?

jian@kuhub.cc.ukans.edu (07/19/90)

Help Wanted:

   I am writting a program that works like a daemon process. The master
will run forever on UNIX and listen a port for request calls. In order to 
make the master process be able to handle multiple calls at the same time, 
I design the program to spawn out child process to perform work whenever a 
request call comes. The child process will terminate itself when a clear 
call signal is received. Here is the problem. When I use "ps ux" shell 
command to check the status of processes on the system, I found lots of 
Zombie processes. If I terminate the master process, all of zombies are gone.
However, I want the master process run forever. What should I do inside the 
program in order to make child processes really terminate themself without 
leave any zombies on the system. I would appreciate any helps.

Thanks in advance.
   
Jian Q. Li
jian@kuhub.cc.ukans.edu

cpcahil@virtech.uucp (Conor P. Cahill) (07/21/90)

In article <24986.26a58a7a@kuhub.cc.ukans.edu> jian@kuhub.cc.ukans.edu writes:
>   I am writting a program that works like a daemon process. The master
>will run forever on UNIX and listen a port for request calls. In order to 
>make the master process be able to handle multiple calls at the same time, 
>I design the program to spawn out child process to perform work whenever a 
>request call comes. The child process will terminate itself when a clear 
>call signal is received. Here is the problem. When I use "ps ux" shell 
>command to check the status of processes on the system, I found lots of 
>Zombie processes. If I terminate the master process, all of zombies are gone.

Read the Wait*(2) and Signal(2) manual pages for your system.  I can't
tell you exactly what to do since you didnot mention what OS you are 
running.

Essentially what you need to do is call wait() as some point which will
clean up the zombies.  You can set up your daemon to get a signal whenever
a child exits and you can then do the wait() which won't block.

-- 
Conor P. Cahill            (703)430-9247        Virtual Technologies, Inc.,
uunet!virtech!cpcahil                           46030 Manekin Plaza, Suite 160
                                                Sterling, VA 22170 

blanier@lynx.uucp (Brian Lanier) (07/21/90)

In article <24986.26a58a7a@kuhub.cc.ukans.edu> jian@kuhub.cc.ukans.edu writes:
>Help Wanted:
>
>   I am writting a program that works like a daemon process. The master
...DELETED...
>call signal is received. Here is the problem. When I use "ps ux" shell 
>command to check the status of processes on the system, I found lots of 
>Zombie processes. If I terminate the master process, all of zombies are gone.

The problem is that no one is collecting the exit status of the
children, so they just hang out. Init will fix 'em when 
they become init's children (i.e. when "Master" dies).

Try including this is the "Master" for a fix:

#include <signal.h>
#include <wait.h>

catcher()	/* for zombies */
	{
	union wait status;
	wait3 (&status,WNOHANG,0); /* plain wait will work, too */
	}	

main()
	{
	...[misc. code]...
	signal(SIGCHLD,catcher);
	...[more misc. code]...
	}
>
>Thanks in advance.

No Prob, Hope this helps.
>   
>Jian Q. Li
>jian@kuhub.cc.ukans.edu

weave@sun.udel.edu (Ken Weaverling) (07/23/90)

In article <1990Jul21.031957.7141@virtech.uucp> cpcahil@virtech.UUCP (Conor P. Cahill) writes:
>Essentially what you need to do is call wait() as some point which will
>clean up the zombies.  You can set up your daemon to get a signal whenever
>a child exits and you can then do the wait() which won't block.
>
The SYS V 3.1 box I have has an easier way to do it. Just set SIGCLD to SIG_IGN
in the parent process and have the child exit with exit(). No zombies!

That info was in the man page for signal and sigset.
-- 

Ken Weaverling (insert any job title here, except for official spokesperson)
Delaware Technical & Community College --- weave@sun.udel.edu