How Linux cleans up zombie processes

Source: Internet
Author: User
Tags signal handler

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 is not deep, hurriedly find a related article to learn, how to deal with.

Defined

In the UNIX System terminology, a process that has terminated,but whose parent have not yet waited for it, is called a zombie.

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 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.

tasks:95 Total, 1 running, 94 sleeping, 0 stopped, 0 Zombie

The zombie process is also counted in the top command. or use the following command:

Ps-ef | grep defunct | Grep-v grep | Wc-l

How to kill the 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

Or

Kill-hup ' Ps-a-ostat,ppid | Grep-e ' ^[zz] ' | awk ' {print $} '

Of course you can write your own better shell script, welcome to share with you.

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

Or

With two fork (), and the immediate child process exits, the grandson process becomes the orphan process, and the INIT process will be responsible for clearing the orphan process.

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.