Ways to eliminate Zombies?

what are the precautions to be taken care for avoiding zombie process ?

... don't kill a parent before its children processes are gone

Kind regards
zxmaus

let me explain my requirement.

1) i have a daemon running , which invokes dhcpcd, when dhcpcd is invoked it waits for ip address untill it get an ip address, once it gets an ip address it spawns dhcpcd daemon and dies.

if i use waitpid() ,if dhcpcd is able to get ip addresss it is fine
but when dhcpcd failed to get ip address it is hanging for the dhcpcd to get complete its task.But practically the process should not hang

if i use waitpid with no hang, some times it is creating zombie process.

is there any signals to get registered, like when the signal get triggered , let me clean up the child info. how to keep track of the child with out blocking
any suggestion

please do the needful

You could catch SIGCHLD. When a child process closes, you get it, meanwhile, you can do whatever else you want.

1) Which one is daemon - dhcp ? I guess you meant it like crond spawning an instance ( self instance ) and arbitrating for the current job - something like that ...
so in that case, the instance that is waiting isnt that self timed out if its not able to get ip address - am not sure of the requirement, so possibly it could be that dhcp instance might have to wait indefinitely in that case it wont return with " cannot find address "

2) Another stuff is to ignore SIGCHLD - in which case zombie creation can be controlled, if there is requirement to decide / update child exit stats by the parent, then this is not a good option

would some one please clarify , what will happen once a parent process receives a SIGCHLD .
if parent process catches SIGCHLD would it clears the child's process context and eliminate zombie.

or do we have to explicitly take care of clearing the child's process context
in the handler ?

call wait() from the SIGCHLD handler to handle the termination of the child process.

If two child processes die at about the same time the parent might only get one SIGCHLD. So I usually wait in a loop until there are no more children. Naturally, I need the WNOHANG option.

Thanks to you all, will try these options.. will get back to you if any thing is needed.