How can I kill a <defunct> process whose parent is init?
You cannot kill a <defunct>
process (also known as zombie process) as it is already dead. The system keeps zombie processes for the parent to collect the exit status. If the parent does not collect the exit status then the zombie processes will stay around forever. The only way to get rid of those zombie processes are by killing the parent. If the parent is init then you can only reboot.
Zombie processes take up almost no resouces so there is no performance cost in letting them linger. Although having zombie processes around usually means there is a bug in some of your programs. Init should usually collect all children. If init has zombie children then there is a bug in init (or somehwere else but a bug it is).
http://en.wikipedia.org/wiki/Zombie_process
Anyone trying to fix the Transmission C source code should read about the "double fork" trick to avoid zombies and signal handlers ... and how it can be used as part of a smart variadic spawn function (see Spawning in Unix).
excerpt from:
"Spawning in Unix", http://lubutu.com/code/spawning-in-unix
Double fork
This trick lets you spawn processes whilst avoiding zombies, without
installing any signal handler. The first process forks and waits for its
child; the second process forks and immediately exits and is reaped;
the third process is adopted by init, and executes the desired program.
All zombies accounted for, since init is always waiting.
if(fork() == 0) {
if(fork() == 0) {
execvp(file, argv);
exit(EXIT_FAILURE);
}
exit(EXIT_SUCCESS);
}
wait(NULL);