What is a Linux zombie process?

Source: Internet
Author: User
Few people may realize that after a process calls exit, the process does not disappear immediately, but leaves a data structure called Zombie. Among the five States of a Linux Process, a zombie process is a very special one. It has abandoned almost all the memory space, no executable code, and cannot be scheduled.

Few people may realize that a process callsExAfter it, the process does not disappear immediately, but leaves a data structure called Zombie. Among the five States of a Linux Process, a zombie process is a very special one. It has abandoned almost all the memory space, no executable code, and cannot be scheduled, only one location is retained in the process list, and information such as the exit status of the process is recorded for collection by other processes. In addition, zombie processes no longer occupy any memory space.

The reason for the zombie process goes back to Unix. Unix designers designed this thing not because they had nothing to do with cool installation. As mentioned above, the zombie process stores a lot of information that is very important to programmers and system administrators. First of all, how does this process die? Is it normal to exit, is there an error, or is it forced to exit by other processes? That is to say, what is the exit code of this program? Secondly, what is the total system CPU time used by the process and the total user CPU time? Number of page errors and the number of signals received. This information is stored in zombie processes. If there is no zombie process, we do not know how long it will take. Once it exits, all related information is immediately cleared from the system. If the parent process or system administrator needs to use the information at this time, the system administrator must be stunned.

 

Therefore, after a process exits, the system will change the process status to Zombie, and then give a certain amount of time to wait for the parent process to collect the exit information, because the parent process may be busy with other tasks and cannot be collected, the Zombie status indicates that the process has exited and is waiting for the parent process to collect information.

The Zombie process cannot be used.KillCommandIt is clear that because the process has exited, if you need to clear such a process, you need to clear its parent process, or wait a long time before it is cleared by the kernel. Because Zombie's process still occupies a process ID, if there are many such processes, it is not conducive to system process scheduling.

Next, let's take a look at an example:


 
01/* zombie. c */

02 # INcLude

03 # include Main ()

04 {

05 pId_ T pid;

06 pid = fork ();

07 if (pid <0) {/* if an error occurs */

08 printf (error occurrEd! );

09} eLsE if (pid = 0) {/* if it is a sub-process */

10 exit (0 );

11} else {/* if it is a parent process */

12Sleep(60);/* sleep for 60 seconds */

13 wait (NULL);/* collect zombie processes */

14}

15}

Compile this program:


 
1 $ cc zombie. c-o zombie

Run the program in the background so that we can execute the next command


 
1 $./zombie &

2 [1] 1217

List PROCESSES IN THE SYSTEM


 
1 $ ps-ax

2 ......

3 1137 pts/0 S-bash

4 1217 pts/0 S./zombie

5 1218 pts/0 Z [zombie]

6 1578 pts/0 R ps-ax

Among them, "Z" is a sign of the zombie process, which means that process 1218 is now a zombie process.

Collect information about Zombie processes and terminate these Zombie processes. We need to use waitpid and wait calls in the parent process. Both of them are used to collect information left by the zombie process and completely disappear the process.

Related Article

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.