Talk about zombie processes in Linux systems

Source: Internet
Author: User
Tags terminates

Zombies in the Linux system and the real zombies (although I have not seen) similar, although already dead, but because no one gave them corpse, but also can walk around. A zombie process is a process that has been terminated, but still retains some information, waiting for its parent process to bury it.
How did the zombie process occur?
If a process is to recycle all the resources allocated to it at the time it terminates, the system will not produce the so-called zombie process. So what information do we keep when we say that a process is terminated? Why do we need to keep this information after termination?
A process terminates in many ways, and some information is useful to the parent process and the kernel after the process terminates, such as the ID number of the process, the exit status of the process, the CPU time the process is running, and so on. As a result, when the process terminates, it reclaims all of the kernel's memory allocated to it, closes all files it opens, and so on, but retains only a few of the above information for use by the parent process. The parent process can use system wait/waitpid, such as a, to clean up the child process and do some finishing work.
Thus, the process of a zombie process is that the parent process calls fork to create the child process, the child process runs until it terminates, it is immediately removed from memory, but the process descriptor remains in memory (the process descriptor occupies very little memory space). The state of the child process becomes Exit_zombie, and the parent process sends a SIGCHLD signal, and the parent process should now call the wait () system call to get the exit status of the child process and other information. After the wait call, the zombie process is completely removed from memory. Therefore, a zombie exists in its termination to the parent process call wait, and so on the time of the gap, generally disappear quickly, but if the programming is unreasonable, the parent process never calls wait and other system calls to collect the zombie process, then these processes will always exist in memory.
Under Linux, we can use the PS and other commands to view the zombie process in the system, the status of the zombie process marked ' Z ':

produce a zombie process
According to the above description, it is easy to write a program to generate the zombie process, the following code:
#include <stdio.h> #include <sys/types.h>int main () {
Www.qixoo.qixoo.com/fork a child process
pid_t pid = fork ();

if (PID > 0)//parent process
{
printf ("In the parent process, sleep for one miniute...zz...\n");
Sleep (60);
printf ("After sleeping, and exit!\n");
}
else if (PID = = 0)
{
Child process exit, and to is a zombie process
printf ("In child process, and exit!\n");
Exit (0);
}

return 0;}

The parent process does not write a system call function such as wait, so it becomes a zombie process after the child process exits, and the parent process does not pick up the corpse. We compile and run the process using the following command, and then look at the status of the process on the system:
[Email protected]:~/documents$ gcc zombie.c-o Zombie
[Email protected]:~/documents$./zombie
In the parent process, sleep for one miniute...zz ...
In child process, and exit!

# Open another terminal:
[email protected]:~$ ps aux | Grep-w ' Z '
1000 2211 1.2 0.0 0 0? Z 13:24 6:53 [Chromium-browse] <defunct>
1000 4400 0.0 0.0 0 0? Z October 0:00 [FCITX] <defunct>
10871 0.0 0.0 0 0 PTS/4 z+ 22:32 0:00 [zombie] <defunct>

As can be seen from the above, there is a zombie process in the system. But if the parent process wakes up after waking up, we look at the system process information again and find that the zombie process is missing.
[Email protected]:~/documents$./zombie
In the parent process, sleep for one miniute...zz ...
In child process, and exit!
After sleeping, and exit!
[email protected]:~/documents$ ps aux | Grep-w ' Z '
1000 2211 1.2 0.0 0 0? Z 13:24 6:53 [Chromium-browse] <defunct>
1000 4400 0.0 0.0 0 0? Z October 0:00 [FCITX] <defunct>

What is this for? The parent process has not died for its child process corpse, ah, how the parent process exits, the zombie process disappeared? Does the parent process clean up the child process when it exits? Not actually .... The real reason: After the parent process dies, all its child processes are passed on to the Init process, and the INIT process becomes a new process for that zombie process, and the INIT process periodically calls the wait system call to clear its zombie child. As a result, you will find that the zombie process disappears after the parent process dies in the example above, in fact the INIT process is the body of the corpse!
how to avoid the production of zombie processes
You cannot use kill to pick up the SIGKILL signal. This command kills the zombie process as if it were a normal process, because the zombie process is a dead process and it can no longer receive any signals. In fact, if there are not many zombie processes in the system, we do not need to eliminate them, and a few zombie processes do not have any effect on the performance of the system.
So when programming, if you can avoid a large number of zombie processes in the system? According to the above described, the child process at the end of the parent process will send a SIGCHLD signal, Linux default is to ignore the signal, we can display the installation of this signal, in the signal processing function call wait and other functions to the corpse, so that the zombie process can be avoided long-term presence in the system. The sample code is as follows:
#include <stdio.h> #include <signal.h> #include <string.h> #include <sys/types.h> #include < sys/wait.h>sig_atomic_t child_exit_status;void clean_up_child_process (int signal_num) {
/* Clean up child process */
int status;
Wait (&status);

/* Store It exit status in a global variable */
Child_exit_status = status;} int main () {
/* Handle SIGCHLD by calling Clean_up_child_process */
struct Sigaction sigchild_action;
memset (&sigchild_action, 0, sizeof (sigchild_action));
Sigchild_action.sa_handler = &clean_up_child_process;
Sigaction (SIGCHLD, &sigchild_action, NULL);

/* Fork a child, and let the child process dies before parent */
pid_t C_pid;
C_pid = fork ();
if (C_pid > 0)
{
printf ("In the parent process, and sleep for on mininute...zz...\n");
Sleep (60);
}
else if (C_pid = = 0)
{
printf ("In child process, and exit now\n");
Exit (0);
}
Else
{
printf ("fork failed!\n");
}

return 0;}

References
Http://en.wikipedia.org/wiki/Zombie_process
Http://simplestcodings.blogspot.com/2010/10/story-of-zombie-process.html
http://www.howtogeek.com/119815/

Talk about zombie processes in Linux systems

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.