Program C demonstrates the process of producing zombie processes, and program c demonstrates zombie processes.

Source: Internet
Author: User
Tags sleep function

Program C demonstrates the process of producing zombie processes, and program c demonstrates zombie processes.
Copy the following online description of the zombie process:

Zombie Process: A process uses fork to create a child process. If the child process exits and the parent process does not call wait or waitpid to obtain the status information of the child process, the sub-process descriptor is still stored in the system. This process is called a zombie process. When each process exits, the kernel releases all resources of the process, including open files and occupied memory. However, some information (including the process ID, the termination status of the process, and the running time of the amount of CPU time taken by the process) is retained for the process ). It is released only when the parent process is obtained through wait/waitpid. However, this causes problems. If the process does not call wait/waitpid, the information retained will not be released, and the process number will be occupied all the time, however, the process numbers used by the system are limited. If a large number of zombie processes are generated, the system cannot generate new processes because there is no available process number. this is the danger of botnets and should be avoided.

From a system perspective, there are two ways to deal with zombie processes:

1. Find the parent process of the zombie process and kill the parent process. The zombie process becomes an orphan process. The orphan process is taken over by the init process in the system. The init process recycles the resources of the zombie process.
2 reboot system, because the dead process cannot be killed


Test as follows:

[Root @ limt ~] # Ps-ef | grep 21165 root 21165 7459 0 00:00:00 pts/1. /. outroot 21166 21165 0 00:00:00 pts/1 [. out] <defunct> root 21190 8866 0 00:00:00 pts/3 grep 21165 [root @ limt ~] # Kill 21165 [root @ limt ~] # [Root @ limt ~] # [Root @ limt ~] # Ps-ef | after grep 21165 kills the parent process, the child process also disappears root 21196 8866 0 00:00:00 pts/3 grep 21165


[root@limt ~]# ps -ef|grep 16704root     16704 16703  0 04:06 pts/1    00:00:00 [a.out] <defunct>root     16719  8866  0 04:06 pts/3    00:00:00 grep 16704[root@limt ~]# [root@limt ~]# [root@limt ~]# kill -9 16704[root@limt ~]# [root@limt ~]# 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
</Pre> <p> </p> <p> from the perspective of development, there are two ways to avoid zombie processes: 1. processing through the signal function, because every sub-process exits, a sigchild signal is sent to the parent process. 2. The sub-process is implemented through fork two times so that the parent process of the sub-process is init process (), the majority of daemon processes are simple programs to demonstrate the process of producing zombie processes: </p> <pre code_snippet_id = "554626" snippet_file_name = "blog_20141217_3_6668512" name = "code" class = "cpp"> A parent process fork a child process and then does not use the waitpid function, exit directly, and the sub-process exits after 120 seconds of sleep # include <sys/types. h> # include <sys/wait. h> # include <errno. h> # include <unistd. h> # include <stdlib. h> # incl Ude <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;} [root @ limt ~] # Gcc Zom. c [root @ limt ~] #./A. out [root @ limt ~] #./A. out I am parent, 15019 parent exitsI am child, 15020 from the output view, the parent process exits first, and then the child process runs the sleep function [root @ limt ~] # Ps-ef | grep 15019 // view the parent process root 15046 8866 0 00:00:00 pts/3 grep 15019 [root @ limt ~] # [Root @ limt ~] # Ps-ef | grep 15020 // view the sub-process root 15020 1 0 00:00:00 pts/1. /. outroot 15056 8866 0 00:00:00 pts/3 grep 15020 from ps, the parent process has been destroyed, and the parent process of the child process is 1, that is, the second parent process of the init process fork is a sub-process and then uses the waitpid function, and then exits, while the sub-process exits after 120 seconds of sleep # 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 (p Id = 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;} [root @ limt ~] # Gcc Zom. c [root @ limt ~] #. /. Out I am parent, 15187I am child, 15188 Child exitsparent exits: From the output, the child process exits first. The parent process exits only after the Child process exits. [root @ limt ~] # Ps-ef | grep 15187 // view the parent process root 15187 7459 0 00:00:00 pts/1. /. outroot 15188 15187 0 00:00:00 pts/1. /. outroot 15197 8866 0 00:00:00 pts/3 grep 15187 [root @ limt ~] # Ps-ef | grep 15188 // view the sub-process root 15188 15187 0 00:00:00 pts/1. /. outroot 15207 8866 0 00:00:00 pts/3 grep 15188 three parent processes fork a child process, then use the waitpid function, and finally use the sleep function, the sub-process quits after 40 seconds of sleep # 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 (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;} [root @ limt ~] #. /. Out I am parent, 15673I am child, 15674 Child exits // wait for 40 seconds and output sleep .... // wait for 40 seconds and then output the parent exits. From the above output, the sleep function is not executed immediately, but is executed only after the sub-process is completed. It is actually waiting for the waitpid function to return [root @ limt ~] # Ps-ef | grep 15673 // root 15673 7459 0 00:00:00 pts/1 within 40 seconds of running the program. /. outroot 15674 15673 0 00:00:00 pts/1. /. outroot 15681 8866 0 00:00:00 pts/3 grep 15673 [root @ limt ~] # Ps-ef | grep 15674 // root 15674 15673 0 00:00:00 pts/1 within 40 seconds of running the program. /. outroot 15692 8866 0 00:00:00 pts/3 grep 15674 [root @ limt ~] # [Root @ limt ~] # Ps-ef | grep 15673 // The program runs for 40 seconds away from root 15673 7459 0 00:00:00 pts/1. /. outroot 15725 8866 0 00:00:00 pts/3 grep 15673 [root @ limt ~] # Ps-ef | grep 15674 // after the program runs for 40 seconds, the sub-process has destroyed root 15727 8866 15674 0 00:00:00 pts/3 grep [root @ limt ~] # [Root @ limt ~] # Ps-ef | grep 15673 // The program runs for 120 seconds. root 15798 8866 15673 0 00:00:00 pts/3 grep Four Parent processes fork a sub-process, without using the waitpid function, but sleep for 120 seconds, and the sub-process will exit after 1 second of sleep # 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 (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;} [root @ limt ~] #./A. out I am parent, 16026sleep... I am child, 16027 Child exitsparent exits. From the output above, the child process exits before the parent process [root @ limt ~] # Ps-ef | grep 16026 // view the parent process root 16026 7459 0 00:00:00 pts/1. /. outroot 16027 16026 0 00:00:00 pts/1 [. out] <defunct> root 16039 8866 0 00:00:00 pts/3 grep 16026 [root @ limt ~] # [Root @ limt ~] # Ps-ef | grep 16027 // view the sub-process. The sub-process is in zombie state. root 16027 16026 0 00:00:00 pts/1 [. out] <defunct> root 16046 8866 0 00:00:00 pts/3 grep 16027 [root @ limt ~] # Top // The top process displays a dead process top-04:02:20 up, 4 users, load average: 0.21, 0.15, 0.15 Tasks: 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: 2050752 k total, 1819480 k used, 231272 k free, 141876 k buffersSwap: 6291448 k total, 0 k used, 6291448 k free, 775368 k cached [root @ limt ~] # Ps-ef | grep 16027 // after 120 seconds, the zombie process disappears. root 16513 8866 16027 0 00:00:00 pts/3 grep






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.