How to avoid zombie process in Linux ?

Zombie process is the process which dies/finishes immediately and whose parent didn’t cares to handle the status of child death. Such process’s continues to run in defunct state till the parent is exited completely.

So this needs to be avoided, and we need to handle the status of child process properly in parent process. We do this by using SIGCHLD signal. When child finishes it sends it status to parent where parent receives the signal and calls the signal handler to process the status.

void catch_child(int sig_num) {
        printf("received signal...");
        int child_status;
        printf("child exited.\n");

int main(int argc, char **argv) {
        pid_t child_pid;
        int child_status, i;

        // register a signal to catch child status ( whether is
        // completed normally or died

        signal(SIGCHLD, catch_child);

        child_pid = fork();
        switch (child_pid) {
                case -1:
                        printf("error: we can use perror\n");
                case 0:
                        printf("child is getting finished after 5 sec\n");
        printf("Parent process proceeding to complete normally after processing child status\n");
        for (i=0; i < 50; i++) {
        printf("After 50 Sec, parent is now getting closed\n");
        return 0;

you can run below program as,

$ gcc -o avoid_zombie avoid_zombie.c

This program makes parent goes sleep for 50 sec, meantime child has already gets finished after 5 sec, and we receive signal in parent which tries to handle the signal from child.

$ time  ./avoid_zombie

Parent process proceeding to complete normally after processing child status
child is getting finised after 5 sec
child exited.
After 50 Sec, parent is now getting closed

real    0m50.015s
user    0m0.000s
sys     0m0.000s

Above time command shows, parent getting properly exited after 50 sec ( As expected ) and before that we get “child exited” message after 5 sec of stating the program.


Subscribe with Valid Email Id to receive updates in Inbox. ( Secured by Google FeedBurner )


1 thought on “How to avoid zombie process in Linux ?”

Leave a Comment

Android Android Applications Android Build system Android Commands Application Libraries Application Stack / User Interface Bash / Shell Scripts Bluetooth driver Cloud Technologies Commands and Packages Compilation Content Management System Core Kernel C Programs Development & Build Development, Debugging and Performance Tools Development Environment Setup Django & REST Api Errors & Failures Git Hardware Platforms HTML JAVA Programs Linux, OS Concepts and Networking Linux Device Drivers Linux Host, Ubuntu, SysAdmin Linux Kernel Linux Networking Middleware Libraries, HAL Multimedia Audio, Video, Images NDK / Middleware / HAL OS Concepts PHP Programming Languages Scripting and Automation Search Engine Optimisation ( SEO ) Social Media Source Code Management ( SCM ) System Administration, Security Testing and Debugging Uncategorized Web design and development Website Hosting Wordpress Yocto / Bitbake / Openembedded