For more info visitZombie Process
A zombie process is the process which is actually dead, but is actually haunting the process structure table. We know that whenever a process runs in Unix, its entry is made in the process structure table which keeps track of a lot of things, for example number of files that the process is using, etc. Whenever a child process dies, its parent process recieves the signal SIGCHLD. Suppose a parent spawned the child process and forgot to handle the SIGCHLD signal or ignored the signal (which is the default scenario when signal is not handled), then the child process becomes the Zombie process. Whenever a parent waits for its child process and reads its exit status, the child process is removed from the process structure table before the parent.
Zombie processes are denoted by Z in the status column when we run the command
ps -le.
Note: Suppose the child has become a zombie, it remains zombie only for the time interval for which its parent is alive. Once the parent of a zombie process is dead, it is adopted by the init process of the unix which then acts as the father of the process. Init continously runs wait system call to remove zombies from the system. Zombie processes are a big problem when their parent processes run for a long time. In this case Zombies will persist in the process structure table for a lot of time.
Let's see an example which creates a Zombie process. We will write a program in C language to understand the zombie process.
#include<stdio.h>
int main()
{
int pid;
pid=fork();
if(pid>0)
{
sleep(30);
printf("parent process id %d",getpid());
printf("in parent process");
}
else if(pid==0)
{
printf("child process id %d",getpid());
printf("in child process");
}
return 1;
}
Now we will understand, what the above code is doing.
With the use of fork() system call we are creating a new child process. Fork() actually returns twice, i.e. from now onwards two process are created, so it returns 0 to the child process and pid of child process to the parent process. Compile this program and run it in the background because we want the shell to be interactive to check the creation of zombie process.
While this program runs, it will run the parent process and put it to sleep for 30 sec, which was necessary for our analysis. At the same time pid of child process will be printed and child process will die after completion.
Immediately run the command to see the dead child zombie.
ps -le|grep 'Z'
This command will search for Z in the list of processes running. As we already know that zombie process remains in the process structure table unless its parent is dead. So, we will be able to find its details through ps command till 30 seconds. If everything worked right, you will notice the process detail with status as Z and pid as the pid of the child process.
Orphan Process
Orphan process is the process for which parent is dead. Suppose a program forks a new child process and while the child is performing some work, its parent dies. This child process then becomes orphan. Orphan process doesn't remain orphan for a long time because after its parent death , the child process is adopted by the init process which we already know is the parent of all the processes. When the child process is adopted by the init process then its ppid value becomes 1, which is the pid for init process.
#include<stdio.h>
int main()
{
int pid;
pid=fork();
if(pid>0)
{
printf("parent process id %d",getpid());
printf("in parent process");
}
else if(pid==0)
{
printf("child process id %d",getpid());
sleep(30);
printf("in child process");
}
return 1;
}
Note the pid of the child process which is printed by the program. Run
ps -el command and search for pid as pid of the child process. See the ppid(parent process id) of this process and you will notice that ppid of this process is 1. This is because the orphan child process has now been adopted by the init process
No comments:
Post a Comment