Generation and prevention of zombie processes in linux
Source: Internet
Author: User
The creation and prevention of Zombie processes in linux when a process stops its own life by calling the exit command, it is not actually destroyed, but left a dead process (Zombie) (The system calls exit, which is used to exit the process, but it is only limited... the creation and prevention of Zombie processes in linux when a process stops its own life by calling the exit command, it is not actually destroyed, but left a dead process (Zombie) (The system calls exit to exit the process, but it is only limited to converting a normal process into a zombie process and cannot completely destroy it ). 1. when a zombie process exits, the kernel releases all resources of the process, including opened 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 it ), it is released only when the parent process is obtained through wait/waitpid. At this time, the Process is in a Zombie state and becomes a Zombie Process (Zombie Process ). This ensures that the parent process can obtain the status information at the end of the child process. In the Linux process status, www.2cto.com has abandoned almost all the memory space, no executable code, and cannot be scheduled, only one location is retained in the process list, and information such as the exit status of the process is recorded for collection by other processes. In addition, the dead process no longer occupies any memory space. It requires its parent process to collect dead parts for it. if its parent process does not have the SIGCHLD signal processing function installed, it calls wait or waitpid () to wait until the child process ends, if the signal is not explicitly ignored, it will remain frozen. if the parent process ends, the zombie sub-process will become "orphan process" and pass to process init on process 1, init is always responsible for clearing dead processes, and all dead processes generated by it also disappear (when each process ends, the system will scan all processes running in the current system, check whether a process is a child process of the process that has just ended. If yes, Init will take over the child process and become its parent process ). However, if the parent process is a loop and does not end, the child process remains frozen, which is why many zombie processes sometimes exist in the system. How to view the Zombie process, using the command ps, we can see that a process marked as Z is a zombie process. Www.2cto.com 2. damage to zombie processes if the parent process does not call wait/waitpid, the information retained will not be released, and its process number will be occupied, however, the process number that the system can use is limited. if a large number of dead processes are generated, the system cannot generate new processes because there is no available process number. 3. avoiding zombie processes 1. the parent process waits for the child process to end through functions such as wait and waitpid, which causes the parent process to stop. 2. if the parent process is busy, the signal function can be used to install the signal processing function for SIGCHLD. After the child process ends, the parent process receives the signal. you can call wait to recycle the child process in the signal processing function. 3. if the parent process does not care when the child process ends, you can use SIGCHLD (SIG_IGN) to notify the kernel that you are not interested in the end of the child process. after the child process ends, the kernel will recycle and will not send signals to the parent process www.2cto.com. Or use the sigaction function to set SA_NOCLDWAIT for SIGCHLD. after the process ends, it will not enter the zombie status struct sigaction sa; sa. sa_handler = SIG_IGN; sa. sa_flags = SA_NOCLDWAIT; sigemptyset (& sa. sa_mask); sigaction (SIGCHLD, & sa, NULL); 4. fork twice. the parent process fork is a sub-process and continues to work. the sub-process fork is a Sun process and then exits, the sun process is taken over by init. after The Sun process ends, init will recycle it. However, the parent process is required to reclaim sub-processes. Int nStatus; pid_t pid; pid = vfork (); // generates the sub-process if (pid> 0) // parent process {waitpid (pid, & nStatus, 0 ); // wait until the sub-process ends. Otherwise, the sub-process will become a dead process and will always exist, even if the sub-process has ended executing} else if (0 = pid) // subprocess {pid = vfork (); // generates the child process if (pid> 0) {exit (0); // The Child process exits, after the sun process passes over to the init process, its exit status is also handled by the init process, and it has nothing to do with the original parent process www.2cto.com} else if (0 = pid) // Sun process {if (execlp ("ls", "ls", NULL) <0) {perror ("execlp"); exit (-1 );}} else {perror ("vfork (child)") ;}} else {perror ("vfork (parent )");}}
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