A guide to Ways to kill defunct processes in Linux (zombie processes)

Source: Internet
Author: User
Tags memory usage

One, what is the defunct process (zombie process)
in Linux   system, a process is over, but his parent process does not wait (call the waiting/ waitpid) him, then he will become a zombie process. When you use the PS command to observe the execution state of a process, you see that the status bar for these processes is defunct. The zombie process is a long dead process, but still occupies a position (slot) in the process table (Processs  table).
But if the process's parent process is finished first, the process will not become a zombie process. Because at the end of each process, the system scans all processes running in the current system to see if any process is a child of the process that is just finished, and if so, the Init process takes over and becomes his parent process, ensuring that each process has a parent process. The init process will automatically wait for its child processes, so all processes that are taken over by Init will not become zombie processes.

Ii. How processes work under Linux
each  linux process has an entry point (entry) in the process table, and all information used by the core process to execute the process is stored at the entry point. When you view the process information in the system with PS   commands, you see the relevant data in the process table. When a new process is established with the fork () system call, the core process assigns a entry point to the new process in the process table, and then stores the relevant information in the process table corresponding to the entry point. One of these messages is the identifier of its parent process. The end of the
child process and the run of the parent process are an asynchronous process, that is, the parent process can never predict when the child process ends. Is it because the parent process is too busy to wait for the child process, or does it not know when the child process ends and loses the state information at the end of the subprocess?
not. Because Linux provides a mechanism to guarantee that the parent process can get it if it wants to know the state information at the end of the subprocess. The mechanism is that when a subprocess finishes its lifecycle, it performs an exit () system call, and the kernel releases all resources for the process, including open files, memory usage, and so on. However, it still retains certain information (including the process number the  process ID, exit code exit codes, exit status The Terminationstatus of the  process, run time the amount of The CPU time is taken by the  process, etc.), and the data is persisted until the system passes it to its parent process until the parent process is fetched through wait/waitpid.
In other words, when a process dies, it is not completely gone. The process terminates, it is no longer running, but there is still some residual data waiting for the parent process to retract. After the parent process  fork () a child process, it must wait for the child process to exit, either by Waiting () (or Waitpid ()). It is this wait ()   action that makes the residual data of the child process disappear.

Third, the harm of zombie process
If the parent process does not invoke Wait/waitpid, the reserved piece of information is not freed and its process number is occupied, but the system's process table capacity is limited and the number of process numbers available is limited, if a large number of zombie processes are generated, The system cannot produce a new process because no process number is available.
Therefore, the defunct process not only consumes the memory resources of the system, affects the performance of the system, but also causes the system to be paralyzed if its number is too large. Also, because the scheduler cannot select the defunct process, the defunct process cannot be removed with the kill command, and the only way is to reboot the system.

Iv. How to kill the defunct process
A defunct process is a process in which an error is corrupted, and no more communication between parent and child processes. Sometimes they evolve into "zombie processes" that remain in your system until the system reboots. You can try the "kill-9" command to clear it, but most of the time it doesn't work.
To kill these defunct processes, you have two choices:
1. Reboot your computer
2. Read on ...
Let's first look at whether the defunct process exists in the system:

The code is as follows:

$ ps-a | grep defunct

Suppose the resulting output looks like this:

The code is as follows:
8328? 00:00:00 Mono
8522? 00:00:01 Mono
13132? 00:00:00 Mono
25822? 00:00:00 Ruby
28383? 00:00:00 Ruby
18803? 00:00:00 Ruby

This means that there are 6 defunct processes: 3 mono processes, and 3 ruby processes. These processes exist because the application is poorly written or the user does something unusual, and in my case, I must have written mono C # programs have serious problems: smile:.
Now, let's take a look at the ID of these processes and their parent process ID:

The code is as follows:

$ PS-EF | grep defunct | More

The output of the above command is as follows:

The code is as follows:
UID PID PPID ...
---------------------------------------------------------------
Kenno 8328 6757 0 Mar22? 00:00:00 [Mono]
Kenno 8522 6757 0 Mar22? 00:00:01 [Mono]
Kenno 13132 6757 0 Mar23? 00:00:00 [Mono]
Kenno 25822 25808 0 Mar27? 00:00:00 [Ruby]
Kenno 28383 28366 0 Mar27? 00:00:00 [Ruby]
Kenno 18803 18320 0 Apr02? 00:00:00 [Ruby]

UID: User ID
PID: Process ID
PPID: Parent Process ID
If you use the command   "kill-9 8328"   Try to kill the process with ID 8328, it may not work. To successfully kill the process, you need to execute the KILL command ($ kill-9  6757) on its parent process (ID 6757). Apply the KILL command to the parent process ID of all of these processes and verify the result ($ ps-a | grep defunct).
If the previous command shows no results, then fix it! Otherwise, you may need to reboot the system.

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.