How to kill zombie process keeping its parent alive? [duplicate]
Matthew Harrington
I am doing printing in .net core framework.
For printing every label, I start a process. So for 100 labels, it creates 100 zombie process.
The zombie process gets removed from the process table, only when I stop the parent application, which has created the printing process.
But I can't stop the application every-time, as that will affect other functionality.
How can I kill zombie, without killing parent application? How many zombie processes Ubuntu can handle?
I tried all ways given in link: Is there any way to kill a zombie process without reboot?but none is working.
I cant kill parent process.
41 Answer
You cannot create a zombie process. When you fork a process and this forked process (child) ends, it sends a return code. If parent process is not waiting for this return code, child process becomes a zombie (child process, which dies and parent does not know).
In C language, it would be a wait() function in parent process which reads the return value.
Common workaround for this (if you have no intention in parent process to wait for a return code from child) is to double fork. It means, your main process A will create new process B, which will start process C. Process B ends immediately, process A can receive its return code right after starting it. Process C will do whatever is needed (printing labels in your case) and when it ends, it will sent a return code. Its parent process B is already dead, so system will take care of it (process with pid 1 in the past, not sure how that works now). And it will not become a zombie.
Edit: Here is an example I found at The first if sequence is process A after a fork, it will just wait for its child process B to finish.
int main()
{
pid_t p1 = fork();
if (p1 != 0)
{ printf("p1 process id is %d", getpid()); wait(); system("ps");
}
else
{ pid_t p2 = fork(); int pid = getpid(); if (p2 != 0) { printf("p2 process id is %d", pid); } else { printf("p3 process id is %d", pid); } exit(0);
}
} 2