Home  >  Article  >  Operation and Maintenance  >  What causes zombies in Linux?

What causes zombies in Linux?

WBOY
WBOYOriginal
2022-05-17 16:34:384017browse

The reason why Linux generates a zombie (zombie process) is: after the parent process generates a child process, the child process exits before the parent process, but the parent process does not process the exit signal sent by the child process, so the child process will be It is called a zombie process; at this time, the process cannot be killed even with the root identity. It can be solved by killing the parent process of the child process.

What causes zombies in Linux?

#The operating environment of this tutorial: linux7.3 system, Dell G3 computer.

What is the reason why Linux generates zombie

Cause:

Zombie process, the English name is zombie process, as the name suggests, it is a dead process. So what is the origin of the zombie process?

The reason why the zombie process is generated is that after the parent process creates a child process, the child process exits before the parent process, but the parent process does not process the data sent by the child process due to various reasons. Exit signal, then this child process will become a zombie process.

This is the so-called zombie (zombie process). It means that during 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 processing 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 and cannot end normally. At this time, even kill -9 as root cannot kill the zombie process. The remedy is to kill the parent process of the zombie process (the parent process of the zombie process must exist). 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.

The zombie process refers to the parent process that has exited, and if no process accepts the process after it is dead, it becomes a zombie process. (zombie) process

Solution:

(1) The parent process waits for the child process to end through functions such as wait and waitpid, which will cause the parent process to hang.

When the wait() or waitpid() system call is executed, the child process will return its data in the process table to the parent process immediately after termination. At this time, the system will immediately delete the entry point. In this case, the defunct process will not be generated.

(2) If the parent process is very busy, you can use the signal function to install a handler for SIGCHLD. After the child process ends, the parent process will receive the signal and can call wait in the handler to recycle.

(3) If the parent process does not care when the child process ends, it can use signal (SIGCLD, SIG_IGN) or signal (SIGCHLD, SIG_IGN) to notify the kernel that it is not interested in the end of the child process. Then after the child process ends, the kernel will recycle and no longer send signals to the parent process

(4) Fork twice, the parent process forks a child process, and then continues to work, the child process forks a grandchild process and then exits , then the grandchild process is taken over by init, and after the grandchild process ends, init will recycle it. However, you have to do the recycling of the child process yourself.

Recommended learning: Linux video tutorial

The above is the detailed content of What causes zombies in Linux?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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