Zombie process ):In a UNIX system, a process is finished, but its parent process is not waiting (wait/waitpid is called), then it will become a zombie process. however, if the parent process of the process has ended, the process will not become a zombie process, because at the end of each process, the system will scan all processes running in the current system, check whether a process is a child process that has just ended. If yes, init takes over the child process and becomes its parent process.
1) The parent process terminates before the child process: This is the orphan process we used earlier. When the parent process exits, the system connects the INIT process to the pipe process. 2) The child process is terminated before the parent process, and the parent process does not call the wait or waitpid function. In this case, the child process enters the zombie state and will continue until the system restarts. When the sub-process is in the dead state, the kernel only saves the necessary information of the process for the parent process. At this time, sub-processes always occupy resources and reduce the maximum number of processes that can be created by the system. Dead State: a process that has been terminated but has not been processed by its parent process (obtain information about the child process and release the resources it still occupies) is called a dead process (zombie ). The ps command prints the status of the zombie process to Z. 3) The child process is terminated before the parent process, and the parent process calls the wait or waitpid function. At this time, the parent process will wait until the child process ends.
The following program demonstrates how to obtain the sub-process exit status through wait calls.
Before you press enter for the first time, run the ps command to check that the sub-process is in the defunct state (zombie state). Once you press enter, wait is called, the zombie cannot be seen through the ps command. Therefore, the method to prevent zombie process is solved by calling wait/waitpid and so on.
1 #include <stdio.h> 2 #include <unistd.h> 3 #include <stdlib.h> 4 5 int main() 6 { 7 //child process 8 int pid; 9 pid = fork();10 if (pid == 0)11 {12 printf("This is child process, pid=%d,bye!\n", getpid());13 exit(0);14 }15 16 sleep(1);17 printf("Press return to remove zombie process\n");18 getchar();19 20 /*parent process must wait on child process to prevent zombie*/21 wait(NULL);22 printf("Press return to exit\n");23 getchar();24 25 return 0;26 }
Execution result:
Summary of orphan and botnets: http://www.cnblogs.com/Anker/p/3271773.html
How can we prevent zombie processes from occurring?