How Linux cleans up zombie processes

Source: Internet
Author: User
Tags signal handler terminates

Today, while maintaining the server, we found 5 nova-novncproxy of zombie processes. 26327?        s      0:05  \_/usr/bin/python/usr/bin/nova-novncproxy--config-file=/ etc/nova/nova.conf 4765?        z      0:00      \_ [Nova-novncproxy] <defunct> 4766?        z      0:00      \_ [Nova-novncproxy] <defunct> 4767 ?        z      0:00      \_ [Nova-novncproxy] <defunct> 4768?        z      0:00      \_ [Nova-novncproxy] <defunct> 4769 ?        z      0:00      \_ [Nova-novncproxy] <defunct> previous knowledge of the zombie process Not deep, hurriedly find a related article to learn, how to deal with. Definition in UNIX System terminology, a process, have terminated,but whose parent have not yet waited for it, is called a zombie .      In a UNIX system, a process is over, but his fatherThe process does not wait (call Wait/waitpid) him, then he will become a zombie process.   in the fork ()/execve () process, the child process becomes a zombie process, assuming that the parent process is still present at the end of the child process, and that the parent process fork () is not installed before the SIGCHLD signal handler call Waitpid () waits for the child process to end without explicitly ignoring the signal. How to view zombie processes on Linux systems, how many zombie processes are counted? #ps-ef | grep defunct or looks for a process with a status of Z, Z is the meaning of the zombie process, the zombie progression. In addition, using the top command to view a column is S, if the status of Z indicates that it is a zombie process. Tasks:  95 Total,   1 running,  94 sleeping,   0 stopped,   0 zombietop command also counts the zombie process. or use the following command: PS-EF | grep defunct | Grep-v grep | How does wc-l kill the zombie process?

1. Rewrite the parent process and bury it after the child process dies. The concrete approach is to take over the SIGCHLD signal. After the child process dies, the SIGCHLD signal is sent to the parent process, and after the parent process receives this signal, the waitpid () function is executed to corpse the child process.  This is based on the principle that even if the parent process does not call wait, the kernel sends a SIGCHLD message to it, although the default processing is ignored, and if you want to respond to this message, you can set a handler function. SIGCHLD Signal: The parent process will receive this signal at the end of the child process. If the parent process does not process the signal and does not wait for the (wait) child process, although the child process terminates, it also occupies the table entry in the kernel process table, when the child process is called a zombie process.  In this case we should avoid (the parent process either ignores the sigchild signal, or catches it, or wait for its derived child process, or the parent process terminates first, and the termination of the child process is automatically taken over by the Init process).  2. Kill-18 PPID (PPID is its parent process) this signal tells the parent process that the child process is dead, and reclaim the resources assigned to him. Sigcont is also an interesting signal. As mentioned earlier, when the process stops, this signal is used to tell the process to resume running. The interesting part of the signal is that it cannot be ignored or blocked, but can be captured.  The default behavior is to discard the signal. 3. Terminate the parent process if Method 2 cannot be terminated, it can take the method that terminates its parent process (if its parent process does not need it), the zombie process becomes an "orphan process", and the adoptive process init,init will always be responsible for cleaning up the zombie process when the parent process dies.  All of the zombie processes it produces also disappear. First look at its parent process and no other child processes, if any, you may need to kill other child processes, that is, the sibling process.  The method is: kill–15 PID1 PID2 (Pid1,pid2 is the other child of the parent process of the zombie process). Then kill the parent process: kill–15 PPID

4. Using a script to terminate a parent process the general zombie process is difficult to kill directly, but you can kill the zombie father. After the parent process dies, the zombie process becomes the "orphan process", and the adoptive process init,init will always be responsible for cleaning up the zombie process. All of the zombie processes it produces also disappear. PS-E-O Ppid,stat | grep Z | Cut-d ""-f2 | Xargs kill-9 or kill-hup ' ps-a-ostat,ppid | Grep-e ' ^[zz] ' | awk ' {print $} ' of course you can write better shell scripts yourself, and you're welcome to share them. I will nova-novncproxy stop after start, zombie process disappears, problem solved. In addition, when the child process dies, the SIGCHLD signal is sent to the parent process, and after the parent process receives this signal, the waitpid () function is executed to corpse the child process. It is based on the principle that even if the parent process does not call wait, the kernel sends a SIGCHLD message to it, and while the default handling of it is ignored, you can set a handler function if you want to respond to this message. How to avoid the zombie process? Processing SIGCHLD signals is not a must. However, for some processes, in particular, server processes often generate child processes to process requests when requests arrive. If the parent process does not wait for the child process to end, the child process becomes a zombie process (zombie) and thus consumes system resources. If the parent process waits for the child process to end, it increases the burden on the parent process and affects the concurrency performance of the server process. The operation of the SIGCHLD signal can be easily set to Sig_ign under Linux. Signal (sigchld,sig_ign) so that the kernel does not spawn a zombie process at the end of the child process. Unlike BSD4, the BSD4 must explicitly wait for the child process to end before releasing the zombie process or using two fork (), and the immediate child process exits, and the grandson process becomes the orphan process, so the INIT process will be responsible for clearing the orphan process.

How Linux cleans up zombie processes

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.