views:

163

answers:

3

I'v some questions about zombie processes

  • what the benefits from zombie process concept?
  • know that the kernel keeps (PID,termination status, resource usage information) for zombie process
    what's the meaning of "resource usage information"
  • how zombie's PPID() = 1 and it still zombie , (init reaps Zombies because it wait() by default)
    can any one write some C code to make a zombie it's parent is Init?
  • can zombies refusing to release some lock on memory??

thanks in advance

+2  A: 

A zombie process is purely a pid and exit status value. The pid cannot be released because the resource (the pid) "belongs to" the parent. If it were released, another process might get assigned the same pid, and then the parent could end up sending signals to an unrelated process; even if the parent first waited to determine if the child had exited, there would be no way to avoid race conditions.

R..
...I think I'm missing something. What is the definition of a zombie process that you are working from?
San Jacinto
A child process which has exited but whose parent has not obtained notification of the exit status via `waitpid` or similar interfaces.
R..
Ahh, yes. Sorry. I just realized that I kept reading "zombie" and was thinking "orphan." Thanks.
San Jacinto
@San Jacinto: This is a common mistake. When I took Operating Systems in university my professor taught that definition of _zombie_, and I got into an argument with him about it. In the end I just had to knowingly put down the wrong answer on the test so that it would get marked correct.
nategoose
+1  A: 
  1. I don't think there is any benefit of zombie process. Please refer to nategoose's answer. A good programmer always makes sure that the parent process waits for the child process( except when you are daemonizing).

  2. I can't answer your second question as I am a beginner too.

  3. I think if a parent process finishes first then init process adopts the child process and hence it's PPID() = 1 (PID of init is 1) and when the child finishes it does not exit, hence it is called a zombie process.

A bad programmer never writes code that waits for the child process to finish, which results in zombie process

Code :

#include <stdio.h>
#include <unistd.h>
#include <sys/wait.h>

void ChildProc(void)
{
    int i;
    for(i = 0; i < 100; i++)
    {
         printf("Child : %d from process <%d> : Parent <%d>\n",i, getpid(), getppid());
    }
}

void ParentProc(void)
{
    int i;
    for(i = 0; i < 20; i++)
    {
         printf("Parent : %d from process <%d> : Parent <%d>\n",i, getpid(), getppid());
    }
}

int main(void)
{
     pid_t child;
     child = fork();

     if(child == 0)
     {
          ChildProc();
     }
     else
     {
          ParentProc();
          //waitpid(child,NULL,0);
     }
}

All you have to do is uncomment the line from waitpid function, so that the parent waits for the child.

4 . Depends. (May be under extremely rare condition)

Searock
Point 1 is misleading. A child process that has exited is *always* a zombie for a short interval between termination and the parent reaping it (with `waitpid` etc.) unless `SIGCHLD` has been ignored.
R..
1- I mean why there is zombie in process status why linux developers make azombie status,2-I know that I mean there is zombie processes it's parent is init and they never be reaped and they gone only after rebooting 4-Depends on what?
Aboelnour
@Aboelnour "why there is zombie" ? Please forgive me, I am a beginner like you.
Searock
If you see zombie processes whose parent is `init`, your `init` implementation is buggy. Try `kill -CHLD 1` to see if that gets `init` to reap them properly.
R..
@Searock - On #1, not "always": when daemonizing, the parent explicitly orphans the child so it will run in the background with `init` as the parent.
bstpierre
@bstpierre thanks for the correction.
Searock
+3  A: 

-- what the benefits from zombie process concept?

A zombie process is just a pid, an exit status, and some accounting information that stays around until a parent uses one of the wait family of system calls to get its final status. Until a parent calls wait the child's process ID must stay marked as used so that no other process can be assigned it. If another process were to get assigned a recycled pid it would be difficult to tell the difference between it and previous processes that had that same pid. Once wait is called by the parent and returns a final exit status it can be assumed that no one will go looking for the child at that pid again, so the pid may now be reused. (I think on Linux if a parent leaves SIGCHLD as SIG_IGN the kernel will not keep zombies around, but that re-registering SIGCHLD's disposition as SIG_IGN does not have the same effect)

-- know that the kernel keeps (PID,termination status, resource usage information) for zombie process what's the meaning of "resource usage information"

Some of this information is what running a program as:

time my_program

will report. These values are usually reported in the siginfo structure for SIGCHLD (which isn't exactly a call to wait) but also available from a call to the waitid form of systme call (on some systems). Look at man sigaction for info about this structure.

-- how zombie's PPID() = 1 and it still zombie , (init reaps Zombies because it wait() by default)

A zombie whose ppid = 1 should not stay a zombie for very long because init should reap it pretty quickly. A process will remain a zombie from a point soon after it dies (either via exit or by an unhanded signal that kills it) until its parent calls wait and gets it's final status. This means that even if init does nothing but call init over and over there could be a small amount of time where a process may show up as a zombie. If processes show up as children of init (0=ppid) for long amounts of time (seconds) then something is probably wrong.

-- can any one write some C code to make a zombie it's parent is Init?

This isn't clear, but I think you want:

pid_t f = fork();
if (f > 0) {
    exit(0); // this is the parent dying, so the child will be an orphan
             // and get adopted by init
} else if (f == 0) {
    sleep(100); // This is the child doing something that takes enough time for
                // its parent to commit suicide (exit(0)) and then for you to
                // observe that it has now been adopted by init
    exit(0);    // And now it dyes as well, so init should reap its status, but
                // it may be a zombie for a short amount of time first.
} else /* error condition would be handled here */

-- can zombies refusing to release some lock on memory??

Zombies can't hold onto much of anything. They lose all of their memory pages, open file handles, ...etc. Pretty much everything the operating system can figure out how to free up should get freed. It would be a bug not to, but remember that the OS has to know that it is something that is supposed to be freed. It is very easy to create resources in user space that should be freed when a program dies that the OS doesn't know are supposed to be freed.

nategoose
look @ this and you will know my meaning about point 3 http://stackoverflow.com/questions/1907775/init-never-reaping-zombie-defunct-processes
Aboelnour
@Aboelnour: Either there was one or more bugs in the kernel or _init_ on the system that was run on, for some reason _init_ had died (I don't know what is supposed to happen if _init_ dies), root sent `SIGSTP` to _init_, or _init_ was running extremely slow. That is not supposed to happen.
nategoose