15 April 2013

what is a Zombie process?


zombie : a dead but not completely dead.
similarly a zombie process (also known as defunct on ) is a process which has completed its execution but still waiting to give its end status to its parent process.
take in this way..
The parent can know the end status of their child process by executing wait(&status) ORITS FAMILY functions
By calling wait() the parent process informs operating system that it have extracted last bit of information and now you can remove child process as if they didn't exist at all.
If no wait(&status) is called from parent side,child process after completing execution do not die completely and maintains an entry in a process table ( zombie process )so at some point of time their parent can know their status by calling wait(&status).as soon as wait() is called child process terminates completely.
lets see some code
[cpp]
/*parent is continuously running without waiting.
* child is zombie
*/
int main(){
pid_t t;
int n=10000;
t=fork();
if(!t){
printf("i am going to become zombie.reap me by calling wait(&status)");
exit(EXIT_SUCCESS);
}
else
{
while(n--){
printf("sorry my son its programmer's fault");
}
}
}
[/cpp]
How you will see that you have a zombie in your system?
well type this at console and look for z under stat column
[cpp]
$ ps -A all
[/cpp]
or
[cpp]
/*look for defunct*/
$ ps
[/cpp]
as you can see child process completed its execution but still it has its presence in process table.It is not consuming any resource but parent still cannot allot other child process with the id of this zombie process as its process id still occupies an entry in process table.
To save these processes from becoming zombie,call wait() for every child
[cpp]
int main(){
pid_t t;
int status;
t=fork();
if(!t){
printf("Thank god you reap me and saved me from zombie world");
exit(EXIT_SUCCESS);
}
else
{
printf("GOOd bye son.");
t=wait(&status);
}
}
[/cpp]
Disadvantage of zombie processes
1.Although zombies does not consume system resources,but still they occupies a entry in a process table.
system cannot allot another process the same pid as that of zombie.
2.This poses the danger of running out of process id if there are large number of zombie processes.
orphan processes..
Just like orphan children we have orphan process.This means that the parent process complete its execution before the child process does.since now there is no parent to take care of them, orphan processes are then adopted by a special system process called init (saviour)
[cpp]
/*Type at console*/
$ ps -C init
[/cpp]
The process id of init is 1
remember orphan processes are different from zombie processes.
[cpp]
int main(){
pid_t t;
int n=80000000;
t=fork();
if(t){
/*parent exits very soon*/
exit(0);
}
else
{
/*child process will get adopted by init for its remaining life time */
while(n--);
printf(" my parent id is :%d",getppid()); // 1
}
}
[/cpp]
Removal
to remove zombie process the parent must wait on it.Now this can be pretty expensive if child processes are taking some reasonable time to complete their execution.
On the other hand if parent doesn't wait then its child will become zombies though not consuming resources but still poses the danger of running out of processes.
a solution to this is to make the children orphaned.After child becomes orphaned it will be adopted by init which frequently runs wait on its children.
Glitch
now to make child orphan the parent process should die but since we want our parent process to keep running ,parent will create child (will die after creating a child) which in turn creates another child to do the actual work.(at this point of time system will have grandparent and grandchild.)
since the actual parent of child process terminates ,this child will be adopted by init process which will run wait after child completes its execution.
"No wait for child,no zombie by parent"
[cpp]
/*process hierarchy
* process a <-process b <-process c
*/
int main(){
int n=10000,status;
pid_t t;// only a upto this point
t=fork(); // a and b
if(!t){ // b will go inside
t=fork();// band c
if(t) // b will go inside
{
printf("nborn to dien");
exit(0);
}
else{
//code for c
while(n--)
printf("n c running %d %dn",getppid(),getpid());
}
exit(0);
}
else
{
/*code for a*/
wait(&status);// waiting for b.
while(1)
printf("ngp runningn");
}
}
[/cpp]

SHARE THIS

Author:

0 comments: