Top handling of the zombie process

Source: Internet
Author: User
Tags signal handler

PS --forest ASCII Art process tree 2 How to clear 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. 2. Kill the parent process. 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. 3 Hazards of the zombie process:in Unix system management , when observing the execution state of a process with the PS command, it is often seen that the status bar of some processes is defunct, which is called a "zombie" process. The zombie process is a long-dead process, but still occupies a position (slot) in the process table (processs table). Because the capacity of the process table is limited, the defunct process not only consumes the memory resources of the system, but also affects the performance of the system, and if the number is too large, it can cause the system to become paralyzed.

<1> phenomenon.

i249 ~ # Top
top-13:35:49 up 738 days, 1:46, 3 users, Load average:3.91, 4.64, 4.94
tasks:175 Total, 1 running, 168 sleeping, 0 stopped, 6 zombie
Cpu0:93.8%us, 0.0%sy, 0.0%ni, 6.2%id, 0.0%wa, 0.0%hi, 0.0%si, 0.0%st
Cpu1:100.0%us, 0.0%sy, 0.0%ni, 0.0%id, 0.0%wa, 0.0%hi, 0.0%si, 0.0%st
Cpu2:21.4%us, 0.0%sy, 0.0%ni, 78.6%id, 0.0%wa, 0.0%hi, 0.0%si, 0.0%st
Cpu3:23.5%us, 5.9%sy, 0.0%ni, 64.7%id, 0.0%wa, 0.0%hi, 5.9%si, 0.0%st
mem:16385312k Total, 15502200k used, 883112k free, 947972k buffers
swap:33574908k Total, 0k used, 33574908k free, 9233612k cached

PID USER PR NI VIRT RES SHR S%cpu%MEM time+ COMMAND
7854 uuwatch 0 2496m 1.2g 11m S 216 7.8 48377:55 java
31880 uuwatch 0 3993m 2.8g 12m S 18.1 1013:10 Java
13279 Root 0 10696 1152 796 R 7 0.0 0:00.03 Top

<2> find.

i249 ~ # ps-a-o stat, Ppid,pid,cmd |grep-e "^[zz]"
Zs 12798 12800 [runscript.sh] <defunct>
Zs 12896 12897 [runscript.sh] <defunct>
Zs 12964 12965 [runscript.sh] <defunct>
Zs 13060 13061 [runscript.sh] <defunct>
Zs 13126 13127 [runscript.sh] <defunct>
Zs 13199 13200 [runscript.sh] <defunct>

<3> resolution.

i249 ~ # ps-a-o stat,ppid,pid,cmd |grep-e "^[zz]" |awk ' {print $} ' |xargs kill-9

Process state.
D= Non-interruptible sleep state
R= Run
S= Sleep
T= Trace/Stop
Z= The situation above the zombie process cannot be solved at all? ---------------------------------------------------------------------------------------fundamentally solve ===>

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 ""-F 2| Xargs kill-9

Or

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

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.

Top handling of the zombie 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.