Home > php教程 > PHP开发 > body text

How to clean up zombie processes in linux

高洛峰
Release: 2016-12-17 11:26:32
Original
1054 people have browsed it

While maintaining the server today, I found 5 zombie processes of nova-novncproxy.

  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>
Copy after login

I didn’t know much about the zombie process before, so I quickly found a related article to learn how to deal with it.

Definition

In UNIX System terminology, a process that has terminated, but whose parent has not yet waited for it, is called a zombie. (Call wait/waitpid) him, then he will become a zombie process. In the fork()/execve() process, it is assumed that the parent process still exists when the child process ends, and the parent process has not installed the SIGCHLD signal before fork() If the handler function calls waitpid() to wait for the child process to end and does not explicitly ignore the signal, the child process becomes a zombie process.

How to check zombie processes on a Linux system and how to count how many zombie processes there are?

#ps -ef | grep defunct

or find the process with status Z. Z stands for zombie process, which means zombie process.

In addition, when you use the top command to view it, there is a column with S. If the status is Z, it means it is a zombie process.

Tasks: 95 total, 1 running, 94 sleeping, 0 stopped, 0 zombie

Zombie processes are also counted in the top command. Or use the following command:

ps -ef | grep defunct | grep -v grep | wc -l

How to kill zombie processes?

Generally, zombie processes are difficult to kill directly, but you can kill zombie dad. After the death of the parent process, the zombie process becomes an "orphan process" and is adopted by process No. 1, init. Init will always be responsible for cleaning up the zombie process. All zombie processes it spawned also disappeared.

ps -e -o ppid,stat | grep Z | cut -d” ” -f2 | ]' | awk '{print $2}'`

Of course, you can write a better shell script yourself, and you are welcome to share it with everyone.

I stopped nova-novncproxy and then started it again. The zombie process disappeared and the problem was solved.

In addition, when the child process dies, it will send the SIGCHLD signal to the parent process. After the parent process receives this signal, it executes the waitpid() function to collect the corpse of the child process. It is based on this principle: even if the parent process does not call wait, the kernel will send it a SIGCHLD message. At this time, although the default processing is to ignore it, if you want to respond to this message, you can set a processing function.

How to avoid zombie processes?

Handling the SIGCHLD signal is not necessary. But for some processes, especially server processes, child processes are often generated to handle requests when requests arrive. If the parent process does not wait for the child process to end, the child process will become a zombie process (zombie) and occupy 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. Under Linux, you can simply set the operation of the SIGCHLD signal to SIG_IGN.

signal(SIGCHLD,SIG_IGN);

In this way, the kernel will not generate a zombie process when the child process ends. This is different from BSD4. Under BSD4, you must explicitly wait for the child process to end before releasing the zombie process

or

using fork() twice, and make the following child process exit directly, so that the grandson process becomes an orphan process, thus The init process will be responsible for cleaning up this orphan process.

For more articles on how to clean up zombie processes in Linux, please pay attention to the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!