The Linux zombie process

  

Someone may realize that after a process calls exit, the process does not disappear immediately, leaving a data structure called a zombie. In the five states of the Linux process, the zombie process is a very special kind. It has given up almost all memory space, has no executable code, can't be scheduled, just keeps a position in the process list, records the process. The information such as the exit status is collected by other processes, and the zombie process no longer occupies any memory space.

The reason for the zombie process, going back to Unix, Unix designers are not designing this thing because they have nothing to do with the cool stuff. As mentioned above, the zombie process holds a lot of information that is very important to programmers and system administrators. First, how does this process die? Is it a normal exit, or is there an error, or is it forced to withdraw by other processes? In other words, what is the exit code for this program? Second, what is the total system CPU time and total user CPU time occupied by this process? The number of page faults and the number of received signals. This information is stored in the zombie process. Imagine if there is no zombie process, how long the process is executed. We don't know, once it exits, all related information is immediately cleared from the system, and if the parent process or The system administrator needs to use it and just has to blink.


So, after the process exits, the system will change the state of the process to Zombie, and then give the time to wait for the parent process to collect its exit information, because the parent process may be Busy with other things can't be collected, so using the Zombie state indicates that the process exits and is waiting for the parent process to collect information.

Zombie process can not be clear with the kill command, 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 to be cleared by the kernel. Because Zombie's process also occupies a process ID number, if there are many such processes, it is not conducive to the system's process scheduling.

Here, let's take a look at an example:
/* zombie.c */#include <sys/types.h>#include <unistd.h> main(){ pid_t pid ; pid=fork(); if(pid<0) { /* If the error is */printf("error occurred!\ "); }else if(pid==0){ /* If it is a child process*/Exit(0); }else{ /* If it is the parent process */sleep(60); /* sleep 60 seconds*/wait(NULL); /* collect zombie process*/}}

Compile this program:< Br>$ cc zombie.c -o zombie

Run the program in the background so that we can execute the next command
$ ./zombie &[1] 1217

list the processes in the system
$ ps -ax... ...1137 pts/0 S 0:00 -bash1217 pts/0 S 0:00 ./zombie1218 pts/0 Z 0:00 [zombie]1578 pts/0 R 0:00 ps - Ax

where "&" is the flag of the zombie process, which means that process 1218 is now a zombie process.

Collecting information about Zombie processes and terminating these zombie processes requires us to use waitpid calls and wait calls in the parent process. The role of both is to collect the information left by the zombie process while making the process disappear completely.

Copyright © Windows knowledge All Rights Reserved