0

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.

  • 1
    Zombies are already dead, you can't kill them ;) And remember, zombie processes are not using any resources, they are just process table entries. To reap the zombies, the parent process has to call wait to check their exit status... – Zanna Dec 07 '16 at 10:46
  • "To reap the zombies, the parent process has to call wait to check their exit status", can you please explain me, how this can be done? – Purnima Naik Dec 07 '16 at 10:49
  • The only way to achieve that is to rewrite the parent process to call wait() when appropriate; that and some incredibly ugly hacks that aren't worth the effort and ugliness just to clear an entry from the process table that doesn't actually hold any resources. – David Foerster Dec 08 '16 at 15:16
  • Wait… are you the author of the application in question (the one that spawns child processes and doesn't reap them after their death)? – David Foerster Dec 08 '16 at 15:19

1 Answers1

1

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 https://stackoverflow.com/questions/10932592/why-fork-twice 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);
}
}
marosg
  • 1,303
  • I am having a doubt. How will Process B end immediately, as its parent Process A is still alive? – Purnima Naik Dec 07 '16 at 16:34
  • Purpose of process B is just to start process C and then exit immediately while process A is waiting for its return code. I will try to find an example. – marosg Dec 08 '16 at 09:28