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 finised 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.
Android Android Commands Android Java Applications Application Libraries Application Stack / User Interface Bash / Shell Scripts Commands and Packages Compilation Content Management System - CMS Core Kernel C Programs Development & Build Development Environment Setup Errors & Failures Flutter git Go Language Programs Hardware Platforms HTML JAVA Programs Kernel & Device Drivers Linux, OS Concepts and Networking Linux Device Drivers Linux Host, Ubuntu, SysAdmin Linux Kernel Linux Networking Middleware Libraries, HAL Monetization / Google AdSense Multimedia - Audio, Video, Images NDK / Middleware / HAL OS Concepts PHP Programming Languages RaspberryPi Scripting and Automation Search Engine Optimisation ( SEO ) Social Media Socurce Code Management ( SCM ) System Administration, Security Testing and Debugging Uncategorized User Interface Web design and development Wordpress Yocto / Bitbake / Openembedded

Leave a Reply / Ask Question