How Linux cleans up zombie processes

Source: Internet
Author: User
Tags signal handler

Zombie Process Definition and lookup

In a UNIX system, a process is over, but his parent process does not wait (call Wait/waitpid) him, then he will become a zombie process. In the fork ()/execve () procedure, the child process becomes a zombie process, assuming that the parent process is still present at the end of the child process, and 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

# PS aux | grep defunct

Or looking for a process with a status of Z, Z is the meaning of the zombie process representing zombie process.
In addition, using the top command to view a column is S, if the status of Z indicates that it is a zombie process. The zombie process is also counted in the top command.
tasks:95 Total, 1 running, 94 sleeping, 0 stopped, 0 zombie

# Top
Top-09:52:28 up, 20:11, 2 users, Load average:0.24, 0.19, 0.18
tasks:92 Total, 4 running, sleeping, 0 stopped, 0 zombie

Kill Zombie Process

The general zombie process is hard to kill directly, but you can kill the zombie dad. 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
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.
Avoid zombie processes

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);

This way, 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.

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.