C program demonstrates the process of producing a zombie process

Source: Internet
Author: User
Tags sleep function

A description of the zombie process is first transcribed online:

Zombie Process: A process uses fork to create a child process, and if the child process exits, and the parent process does not call wait or waitpid to get state information for the child process, the process descriptor of the child process is still stored in the system. This process is called a zombie process. At the time each process exits, the kernel frees all of the resources of the process, including open files, memory usage, and so on. However, it still retains certain information (including process ID, exit status of the termination status of the process, run time of the amount of the CPU taken by the Proces s, etc.). It is not released until the parent process passes wait/waitpid. However, this causes the problem, if the process does not call Wait/waitpid, then the reservation of the piece of information will not be released, its process number will always be occupied, but the system can use the process number is limited, if a large number of zombie process generated, The system cannot produce a new process because no process number is available. This is the threat to the zombie process and should be avoided.

From a system perspective, there are two ways to handle a zombie process:

1 Find the parent process of the zombie process, kill the parent process, then the zombie process will become an orphan process, the orphan process is taken over by the INIT process in the system, the INIT process will reclaim the resources of the zombie process
2 reboot system because the zombie process is not allowed to kill


Test as follows:

[[email protected] ~]# ps-ef|grep 16704root     16704 16703  0 04:06 pts/1    00:00:00 [a.out] <defunct>root< c3/>16719  8866  0 04:06 pts/3    00:00:00 grep 16704[[email protected] ~]# [[email protected] ~]# [[Email Protect Ed] ~]# kill-9 16704[[email protected] ~]# [[email protected] ~]# ps-ef|grep 16704root     16704 16703  0 04:06 PTS/1    00:00:00 [a.out] <defunct>root     16725  8866  0 04:06 pts/3    00:00:00 grep 16704

There are two ways to avoid a zombie process from a development perspective:
1 is handled by the signal function, because each child process exits will cause a sigchild signal to the parent process
2 through the fork two sub-process implementation, so that the child process parent process is the init process (), the majority of daemons are implemented this way


A simple program demonstrates the process of producing a zombie process:

A parent process fork a child process and then exits without using the Waitpid function, and the child process exits # include <sys/types.h> #include <sys/wait.h> #include after sleep 120 seconds <errno.h> #include <unistd.h> #include <stdlib.h> #include <stdio.h> #include <string.h>        int main (int argc, char *argv[]) {pid_t pid;        PID = fork ();                if (PID = = 0) {int iPid = (int) getpid ();                fprintf (stderr, "I am child,%d\n", iPid);                Sleep (120);                fprintf (stderr, "Child exits\n");        return exit_success;       } int iPid = (int) getpid ();       fprintf (stderr, "I am parent,%d\n", iPid);       fprintf (stderr, "parent exits\n"); return exit_success;} [[email protected] ~]# gcc zom.c [[email protected] ~]#./a.out [[email protected] ~]#./a.out I am Parent, 15019parent Exitsi am child,15020 from the output see the parent process exits first, then the child process runs the sleep function [[email protected] ~]# ps-ef|grep 15019//view parent Process root 15 046 8866 0 03:29 pts/3 00:00:00 grep 15019[[email protected] ~]# [[email protected] ~]# ps-ef|grep 15020//view sub-process root 15020 1 0 03:29 pts/1 00:00:00./a.outroot 15056 8866 0 03:29 pts/3 00:00:00 grep 15020 from PS See the parent process has been destroyed, the child process's parent process number is 1, that is, the INIT process two parent process fork a child process and then use the Waitpid function, and then exit, and the child process s Leep 120 seconds to exit # include <sys/types.h> #include <sys/wait.h> #include <errno.h> #include <unistd.h > #include <stdlib.h> #include <stdio.h> #include <string.h>int main (int argc, char *argv[]) {PID        _t pid;        PID = fork ();                if (PID = = 0) {int iPid = (int) getpid ();                fprintf (stderr, "I am child,%d\n", iPid);                Sleep (120);                fprintf (stderr, "Child exits\n");        return exit_success;       } int iPid = (int) getpid ();       fprintf (stderr, "I am parent,%d\n", iPid);       Waitpid (pid,null,0);       fprintf (stderr, "parent exits\n"); return exit_success;} [[email protected] ~]# gcc zom.c [[email protected] ~]#./a.out I amparent,15187i am Child,15188child exitsparent exits the child process exits from the output, the parent process waits for the child process to exit before exiting [[email protected] ~]# Ps-ef|grep 15187//view parent process root 15187 7459 0 03:32 pts/1 00:00:00./a.outroot 15188 15187 0 03:32 pts/1 00:00:00./a.o     Utroot 15197 8866 0 03:33 pts/3 00:00:00 grep 15187[[email protected] ~]# ps-ef|grep 15188//view sub-process root 15188 15187 0 03:32 pts/1 00:00:00./a.outroot 15207 8866 0 03:33 pts/3 00:00:00 grep 151,883 parent Process Fork A child process, and then Using the Waitpid function, the sleep function is used last, and the child process exits # include <sys/types.h> #include <sys/wait.h> #include < after 40 seconds of sleep errno.h> #include <unistd.h> #include <stdlib.h> #include <stdio.h> #include <string.h>int        Main (int argc, char *argv[]) {pid_t pid;        PID = fork ();                if (PID = = 0) {int iPid = (int) getpid ();                fprintf (stderr, "I am child,%d\n", iPid);                Sleep (40);                fprintf (stderr, "Child exits\n"); Return EXIT_SUCCESS;       } int iPid = (int) getpid ();       fprintf (stderr, "I am parent,%d\n", iPid);       Waitpid (pid,null,0);       fprintf (stderr, "sleep....\n");       Sleep (120);       fprintf (stderr, "parent exits\n"); return exit_success;} [[email protected] ~]#./a.out I am parent,15673i am child,15674child exits//wait 40 seconds to output sleep ...//wait 40 seconds to output the parent ex Its from the above output to see the sleep function is not executed immediately, but wait for the child process to run the completion before execution, is actually waiting for the waitpid function return [[email protected] ~]# ps-ef|grep 15673//      The program runs within 40 seconds of root 15673 7459 0 03:43 pts/1 00:00:00./a.outroot 15674 15673 0 03:43 pts/1 00:00:00./a.outroot 15681 8866 0 03:44 pts/3 00:00:00 grep 15673[[email protected] ~]# ps-ef|grep 15674//program run within 40 seconds root 156 15673 0 03:43 pts/1 00:00:00/a.outroot 15692 8866 0 03:44 pts/3 00:00:00 grep 15674[[email protecte D] ~]# [[email protected] ~]# ps-ef|grep 15673//program run 40 seconds outside root 15673 7459 0 03:43 pts/1 00:00:00./a.outro OT 15725 8866 0 03:44 PTS/3 00:00:00 grep 15673[[email protected] ~]# ps-ef|grep 15674//program runs 40 seconds away, the child process has destroyed root 15727 8866 0 03:44 pts/3 00:   00:00 grep 15674[[email protected] ~]# [[email protected] ~]# ps-ef|grep 15673//program run 120 seconds outside root 15798 8866 0 03:46 PTS/3 00:00:00 grep 156,734 parent Process Fork A child process, not using the Waitpid function, but sleep 120 seconds, and the child process after sleep 1 seconds exit # include <sys/types.h&gt , #include <sys/wait.h> #include <errno.h> #include <unistd.h> #include <stdlib.h> #include <        Stdio.h> #include <string.h>int main (int argc, char *argv[]) {pid_t pid;        PID = fork ();                if (PID = = 0) {int iPid = (int) getpid ();                fprintf (stderr, "I am child,%d\n", iPid);                Sleep (1);                fprintf (stderr, "Child exits\n");        return exit_success;       } int iPid = (int) getpid ();       fprintf (stderr, "I am parent,%d\n", iPid);       fprintf (stderr, "sleep....\n");       Sleep (120); fprintf (stderr, "Parent exits\n"); return exit_success;} [[email protected] ~]#./a.out I am parent,16026sleep .... I am Child,16027child exitsparent exits from above output see child process exit before parent process [[email protected] ~]# ps-ef|grep 16026//view parent process root 1602     6 7459 0 03:51 pts/1 00:00:00/a.outroot 16027 16026 0 03:51 pts/1 00:00:00 [a.out] <defunct>root 16039 8866 0 03:51 pts/3 00:00:00 grep 16026 [[email protected] ~]# [[email protected] ~]# ps-ef|grep 1602 7//view child process, child process in zombie state root 16027 16026 0 03:51 pts/1 00:00:00 [a.out] <defunct>root 16046 8866 0 03:51 PTS/3 00:00:00 grep 16027[[email protected] ~]# top//top process shows there is a zombie process top-04:02:20 up 2:46, 4 users, load a verage:0.21, 0.15, 0.15tasks:280 Total, 1 running, 278 sleeping, 0 stopped, 1 zombiecpu (s): 1.2%us, 0.6%sy, 0.0 %ni, 97.4%id, 0.7%wa, 0.0%hi, 0.0%si, 0.0%stmem:2050752k total, 1819480k used, 231272k free, 141876k BUFFERSSW ap:6291448k Total, 0k used, 6291448k free,  775368k cached[[email protected] ~]# ps-ef|grep 16027//120 seconds Zombie process disappears root 16513 8866 0 04:01 PTS/3 00:00 : xx grep 16027






C program demonstrates the process of producing a zombie process

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.