The basic method of killing zombie process in Linux system

Source: Internet
Author: User
Tags memory usage reserved

In a UNIX system, a process ends, but his parent process does not wait (call Wait/waitpid) him, then he will become a zombie process. In the fork ()/execve () procedure, when the parent process is still present at the end of the child process, and the parent process fork () does not install the SIGCHLD signal-processing function call Waitpid () until the child process ends and the signal is not explicitly ignored, the child process becomes a zombie process.

When a process ends its own life by invoking the Exit command, it is not actually destroyed, but rather leaves behind a data structure called the zombie process (Zombie) (System call exit, which acts as a process exit, but is limited to turning a normal process into a zombie process, and cannot be completely destroyed)

The harm of the process
Because the end of a subprocess and the running of the parent process are an asynchronous process, the parent process will never be able to predict when the child process ends. Is it because the parent process is too busy to wait for the child process, or does it not know when the child process ends and loses the state information at the end of the subprocess? No. Because Unⅸ provides a mechanism to ensure that if the parent process wants to know the state information at the end of the subprocess, it can be obtained. The mechanism is that when each process exits, the kernel releases all resources for the process, including open files, memory usage, and so on. However, some information is still reserved for it (including the process ID, exit status The termination state of the processes, runtime the amount of CPU time taken by the Proces s, etc.). It is not released until the parent process is fetched through wait/waitpid. But this leads to the problem, if the process does not invoke Wait/waitpid, then the reserved piece of information will not be released, its process number will be occupied, but the system can use the process number is limited, if a large number of zombie processes, The system cannot produce a new process because no process number is available. This is the bane of the zombie process and should be avoided.

1. How to view the zombie process?
How do I look at zombie processes on Linux systems and how many zombie processes are counted?

The code is as follows:
#ps-ef | grep defunct
Or look for a process with a state of Z, which is the meaning of the zombie process, the zombies.

In addition, using the top command to view a column of S, if the state is z indicates that it is a zombie process.

The code is as follows:
tasks:95 Total, 1 running, sleeping, 0 stopped, 0 zombie
The top command also counts the zombie process. or use the following command:
The code is as follows:
Ps-ef | grep defunct | Grep-v grep | Wc-l
2, how to kill the zombie process?
It's hard to kill a zombie in general, but you can kill a zombie dad. After the parent process dies, the zombie process becomes an "orphan process", and the adoptive 1th process Init,init is always responsible for cleaning up the zombie process. All the zombie processes it produces are also disappearing.
The code is as follows:
PS-E-O Ppid,stat | grep Z | Cut–d ""-f2 | Xargs kill-9
Or
The code is as follows:
Kill-hup ' Ps-a-ostat,ppid | Grep-e ' ^[zz] ' | awk ' {print $} '
Of course, you can write a better shell script yourself, welcome to share with you.

In addition, when the subprocess dies, the SIGCHLD signal is sent to the parent process, and the parent process receives the signal and executes the waitpid () function to bury the child process. is based on the principle that even if the parent process does not call wait, the kernel sends the SIGCHLD message to it, while the default processing for it is ignored, and if you want to respond to this message, you can set up a handler function.

3. How to avoid zombie process?
Processing of SIGCHLD signals is not necessary. However, for some processes, especially server processes, it is often necessary to generate a subprocess processing request when the request arrives. If the parent process does not wait for the child process to end, the child process becomes a zombie process (zombie) that consumes system resources. If the parent process waits for the child process to end, it will increase the burden on the parent process and affect the concurrency performance of the server process. The operation of the SIGCHLD signal can be simply set to Sig_ign under Linux.
Signal (sigchld,sig_ign);
In this way, the kernel does not produce a zombie process at the end of the subprocess. This is different from the BSD4, BSD4 must explicitly wait for the child process to end to release the zombie process

Or

With two times fork (), and the immediate child process to exit directly, the grandson process becomes an orphan process, thus the INIT process will be responsible for the removal of this orphan 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.