Asynchronous collection of Fork sub-processes (zombie processes)

Source: Internet
Author: User

 What is a zombie process?

When a process calls the exit command to end its own life, it is not actually destroyed by a zombie process, but left a zombie process called Zombie) (The system calls exit to exit the process, but it is only limited to converting a normal process into a zombie process and cannot completely destroy it)

How is a zombie process?

In the status 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. It needs its parent process to collect the corpse for it.

If the parent process does not have the sigchld signal processing function that calls wait or waitpid () and waits for the child process to end, and does not explicitly ignore the signal, it will remain zombie, if the parent process ends at this time, the INIT process will automatically take over the child process and collect the corpse for it. It can still be cleared.

However, if the parent process is a loop and does not end, the child process will remain zombie, which is why many zombie processes sometimes exist in the system. The process number that the system can use is limited. If a large number of zombie processes are generated, the system cannot generate new processes because there is no available process number.

 

Asynchronous zombie process recycling:

After fork (), non-blocking (asynchronous) Wait for the sub-process (zombie recycling ).
After fork (), the sub-process and the parent process are executed in different ways. The zombie process is generated because the parent process has not "zombie" the sub-process, and can be divided into the following two categories based on the degree of hazard:
In general, when the child process ends but the parent process is not completed, the child process will become a zombie process.
(1) After the child process ends, but before the parent process ends, the child process will become a zombie process. After the parent process ends, the zombie will be recycled by the INIT process.
(2) If the child process has ended but the parent process has never ended, the zombie will always exist and as exec occurs, the number of botnets increases.
The following code indicates that the child process will be a zombie within 5 seconds of execution by the parent process:

[CPP]
View plaincopyprint?
  1. # Include <stdio. h>
  2. # Include <stdlib. h>
  3. # Include <signal. h>
  4. # Include <unistd. h>
  5. Int main (){
  6. // PID of the sub-process
  7. Int c_pid;
  8. Int PID;
  9. If (pid = fork ())){
  10. // Parent process
  11. C_pid = PID;
  12. Printf ("the child process is % d \ n", c_pid );
  13. Sleep (5 );
  14. Exit (0 );
  15. } Else {
  16. // Sub-process
  17. Printf ("I'm a child. \ n ");
  18. Exit (0 );
  19. }
  20. }
# Include <stdio. h> # include <stdlib. h> # include <signal. h> # include <unistd. h> int main () {// subprocess PID int c_pid; int PID; If (pid = fork () {// parent process c_pid = PID; printf ("the child process is % d \ n", c_pid); sleep (5); exit (0 );} else {// sub-process printf ("I'm a child. \ n "); exit (0 );}}

The code above indicates that within five seconds of the parent process, the child process has always been a zombie!
Therefore, the zombie process needs to be recycled. The traditional method of recovery is to use the wait () function to wait for the sub-process. The wait () mode is blocked. Before the sub-process ends, wait waits until the following statement is executed.  

[CPP]
View plaincopyprint?
  1. # Include <stdio. h>
  2. # Include <stdlib. h>
  3. # Include <signal. h>
  4. # Include <unistd. h>
  5. # Include <sys/Wait. H>
  6. Int main (){
  7. // PID of the sub-process
  8. Int c_pid;
  9. Int PID;
  10. If (pid = fork ())){
  11. // Parent process
  12. C_pid = PID;
  13. Printf ("the child process is % d \ n", c_pid );
  14. // Block the waiting process
  15. Int status;
  16. If (pid = wait (& Status ))! =-1 & pid = c_pid ){
  17. // The child process is recycled successfully.
  18. Printf ("the child exit with % d \ n", wexitstatus (Status ));
  19. Fflush (stdin );
  20. } Else {
  21. Printf ("Wait () fail. \ n ");
  22. }
  23. Printf ("now, the child has been exit, and I will sleep. \ n ");
  24. Sleep (20 );
  25. Exit (0 );
  26. } Else {
  27. // Sub-process
  28. Printf ("I'm a child. \ n ");
  29. Sleep (5 );
  30. Exit (0 );
  31. }
  32. }
# Include <stdio. h> # include <stdlib. h> # include <signal. h> # include <unistd. h> # include <sys/Wait. h> int main () {// subprocess PID int c_pid; int PID; If (pid = fork () {// parent process c_pid = PID; printf ("the child process is % d \ n", c_pid); // block wait for the sub-process int status; If (pid = wait (& Status ))! =-1 & pid = c_pid) {// The sub-process printf ("the child exit with % d \ n", wexitstatus (Status) is successfully recycled )); fflush (stdin);} else {printf ("Wait () fail. \ n ");} printf (" now, the child has been exit, and I will sleep. \ n "); sleep (20); exit (0);} else {// sub-process printf (" I'm a child. \ n "); sleep (5); exit (0 );}}

The code above indicates that the sub-process is recycled after 5 seconds of execution. Within 20 seconds of the sub-process, the sub-process has ended and is no longer a zombie.
However, this method of blocking wait by using wait () also has some drawbacks, that is, the parent process must wait for the child process and cannot do other things. How can we wait for the child process without blocking?
Man Wait:
When the sub-process exits, it will send a sigchld signal. The default POSIX does not respond. Therefore, we only need to implement the function for processing sigchld. How can this problem be solved?
Signal is used to set rules for processing semaphores (or jump functions)

[CPP]
View plaincopyprint?
  1. Signal (sigchld, Handler );
  2. Void handler (INT num)
  3. {
  4. // I have received the sigchld signal.
  5. Int status;
  6. Int pid = waitpid (-1, & status, wnohang );
  7. If (wifexited (Status ))
  8. {
  9. Printf ("the child exit with code % d", wexitstatus (Status ));
  10. }
  11. }
Signal (sigchld, Handler); void handler (INT num) {// I have received the sigchld signal int status; int pid = waitpid (-1, & status, wnohang ); if (wifexited (Status) {printf ("the child exit with code % d", wexitstatus (Status ));}}

OK. All the code is as follows. Do not block the parent process with wait again!

[CPP]
View plaincopyprint?
  1. # Include <stdio. h>
  2. # Include <stdlib. h>
  3. # Include <signal. h>
  4. # Include <unistd. h>
  5. # Include <sys/Wait. H>
  6. Void handler (INT num ){
  7. // I have received the sigchld signal.
  8. Int status;
  9. Int pid = waitpid (-1, & status, wnohang );
  10. If (wifexited (Status )){
  11. Printf ("the child % d exit with code % d \ n", PID, wexitstatus (Status ));
  12. }
  13. }
  14. Int main (){
  15. // PID of the sub-process
  16. Int c_pid;
  17. Int PID;
  18. Signal (sigchld, Handler );
  19. If (pid = fork ())){
  20. // Parent process
  21. C_pid = PID;
  22. Printf ("the child process is % d \ n", c_pid );
  23. // The parent process does not have to wait. Do your own thing ~
  24. For (INT I = 0; I <10; I ++ ){
  25. Printf ("do parent things. \ n ");
  26. Sleep (1 );
  27. }
  28. Exit (0 );
  29. } Else {
  30. // Sub-process
  31. Printf ("I'm a child. \ n ");
  32. Sleep (2 );
  33. Exit (0 );
  34. }
  35. }

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.