All about programming in GNU/LINUX

Aside

Everything to programatically understand about Zombie processes and Avoiding them under GNU/LINUX

A Zombie is a process which has died but its entry still remains in the process table.This happens in unix and unix-like Operating systems when parent doesnot execute wait() system call for its child processes .

USEFULNESS OF ZOMBIES

*Parent process can fetch the information of the child process from the process table entry even after the child has died by expecting wait() or waitpid() system call .

*The pid of the zombie process will not be allocated to any new process

Disadvantages of Zombie processes

*The presence of a few zombies is not worrisome in itself, but may indicate a problem that would grow serious under heavier loads. Since there is no memory allocated to zombie processes except for the process table entry itself, the primary concern with many zombies is not running out of memory, but rather running out of process ID numbers.

SIGCHLD signal

When a child process exists , it sends a SIGCHLD signal to the parent , by default this signal is not taken into account unless a signal handler is setup

Ways of avoiding creation of Zombie processes

*Execute wait() system call in the sequential code of the parent

  Image

#include<stdio.h>

#include<unistd.h>

#include<time.h>

int main()

{

pid_t pid;

int status;

pid=fork();

if(pid==0) /*child proces exists*/

{

exit(0);

}

printf(“\nThe process Id of the Zombie child process is %d\n\n\n”,pid);

system(“ps -aeo pid,stat | grep Z”);

/*obtain the process list with the status information and grep for Z which indicates that the proces is a Zombie */

printf(“\n\n\nZ+ Indicates that the process with PID=%d is a Zombie running in foreground\n\n”,pid);

sleep(5);

pid=wait(&status);/*wait returns the pid of the child that was terminated */

printf(“\n\nThe Zombie status of the child process pid=%d is removed\n\n”,pid);

system(“ps -aeo pid,stat | grep Z”);/*this call doesnt result in any output since there Zombie status of the child process no more exists, since wait system call is executed in the parent*/

sleep(5);

exit(0);

}

*Execute waitpid() system call in the sequential code of the parent

 Image

#include <sys/wait.h>

#include <stdlib.h>

#include <unistd.h>

 

int main(void)

{

pid_t pids[10];

int i;

char message[]=”\n\nTen Zombie child processes created the shell command inside call to system illustrates this\n\n”;

 

for (i = 9; i >= 0; –i) {

pids[i] = fork();

if (pids[i] == 0) {

 

_exit(0);

}

}

/*for each iteration of the for loop fork() is called and the child process is made to exit without waiting for them in the parent , this results in creation of 10 Zombies , which is clearly illustrated the ps command inside the call to system()*/

write(1,message,sizeof(message));

system(“ps -aeo pid,stat | grep Z”);

for (i = 9; i >= 0; –i)

 waitpid(pids[i], NULL, 0);/*Equivalent of executing wait() call ten times */

return 0;

}

*setup a signal handler using signal() or sigaction() system call use wait() system call inside it

Image 

#include<signal.h>

#include<stdio.h>

#include<unistd.h>

#include<time.h>

void Child_handler(int status)

{

pid_t pid;

printf(“\n\nSIGCHLD generated and caught “);

pid=wait(&status);

printf(“\n\nProcess Id of the Zombie Child = %d\n\n”,pid);

return ;

}

int main()

{

pid_t pid;

int status;

pid=fork();

signal(SIGCHLD,Child_handler);

/*Child_handler is the function where control moves onto when SIGCHLD is generated , that is when the child dies*/

if(pid==0) /*child proces exists*/

{

exit(0);

}

sleep(2);

exit(0);

}

*In modern implementations by explicitly ignoring SIGCHLD by setting its handler to SIG_IGN

#include<signal.h>

#include<stdio.h>

#include<unistd.h>

#include<time.h>

int main()

{

pid_t pid;

int status;

pid=fork();

signal(SIGCHLD,SIG_IGN);

/*In modern implementations of the Linux systems , by explicitly setting the signal handler for the SIGCHLD signal to SIG_IGN zombies can be avoided*/

if(pid==0) /*child proces exists*/

{

exit(0);

}

system(“ps -aeo pid,stat | grep Z”);/*This execution of shell command doesnt give any output since There are no Zombie processes*/

printf(“\n\nNo Zombies created \n\n”);

sleep(2);

 exit(0);

}

*When the parent process dies init becomes parent of all the Child processes including the Zombie child processes

Advertisements

4 responses

  1. There are several wordpress plugins for syntax highlighting and otherwise sprucing up your code blocks. You should install one. I recommend this one: http://wordpress.org/extend/plugins/wp-syntax/

    You can see it in action here on my blog: http://tonybaldwin.me/blog/2011/09/20/web-word-count/

    You’ll note how it makes it easier to read and follow code blocks and how it distinguishes them from the remaining text.

    September 10, 2012 at 2:15 pm

    • Thank you very much .Do comment your suggestions and help me improve …

      September 10, 2012 at 2:35 pm

    • Thanks a lot for the suggestion , SIR.But I believe the plugin is not available for free .Im still under wordpress.com domain , using their free service ..Im still a student and cannot afford to pay for my blog at this point of time , is there any way where i can get this feature added for free .

      September 10, 2012 at 2:38 pm

      • Well, the plugin is available for free. I do not, however, know whether you can install it on a free/hosted account on wordpress.com. I do not see a means of installing plugins on such accounts.

        September 11, 2012 at 4:56 pm

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s