Linux Zombie Process

Source: Internet
Author: User
Tags signal handler

The zombie process is simply: Sonwhen the process exits, the parent process does not properly handle the sigchild signal it emits, causing the child process to stay in a zombie state and wait for its parent process to bury it, and the child process in this state is a zombie process. In the fork ()/execve () procedure, assume that the parent process is still present at the end of the child process, while the parent process fork () did not install the SIGCHLD signal handler function call waitpid () waits for the child process to end, and does not explicitly ignore the signal, the child process becomes a zombie process and does not end properly, even if the root identity kill-9 cannot kill the zombie process. The remedy is to kill the zombie process of the parent process (the zombie process of the parent process must exist), the zombie process becomes an "orphan process", adoptive to the 1th process Init,init will always be responsible for cleaning up the zombie process. In UNIX terminology,a process that has been terminated but its parent process has not yet done so (getting information about terminating a child process, releasing the resources it still occupies) is called a zombie process (zombie)。 Generation: How to produce a zombie process: a process that calls the exit command to end its own life is not actually destroyed, but rather leaves behind a data structure called the zombie process (Zombie) (System call exit, which is the function of making the process exit, But it is only limited to turning a normal process into a zombie process and cannot completely destroy it. In the state of the Linux process, the zombie process is a very special one, it has abandoned almost all memory space, no executable code, can not be adjusted, the zombie process no longer occupies any memory space, except that it retains a location in the process list and records information such as the exit status of the process for other processes to collect. It needs its parent process to bury it, if his parent process does not install the SIGCHLD signal handler call wait or Waitpid () waits for the child process to end, and does not explicitly ignore the signal, then it remains zombie state, if the parent process is over, Then the init process will automatically take over the child process, and for it to corpse, it can still be cleared. But if the parent process is a loop and does not end, then the child process will remain zombie state,This is why there are sometimes a lot of zombie processes in the system. the danger of zombie process
If the parent process does not call Wait/waitpid, then the reserved piece of information will not be released and its process number must be, but the number of processes that the system can use is limited, and if a large number of zombie processes are generated, the system cannot generate new processes because no process number is available。 Clear: How to clear the zombie process: 1. Rewrite the parent process, and then bury it after the child process dies. The concrete approach is to take over the SIGCHLD signal. After the child process dies, the SIGCHLD signal is sent to the parent process, and after the parent process receives this signal, the waitpid () function is executed to corpse the child process. This is based on the principle that even if the parent process does not call wait, the kernel sends a SIGCHLD message to it, although the default processing is ignored, and if you want to respond to this message, you can set a handler function. 2. Kill the parent process. After the parent process dies, the zombie process becomes the "orphan process", and the adoptive process init,init will always be responsible for cleaning up the zombie process. All of the zombie processes it produces also disappear. third, the avoidance of zombie process
1. The parent process waits for the child process to end through functions such as wait and waitpid, which causes the parent process to hang
2. If the parent process is busy, you can use the signal function to install the signal processing function for SIGCHLD. After the child process is finished, the parent process receives the signal and can call the wait recycle in the signal handler function.
3, if the parent process does not care about when the child process ends, then you can use signal (SIGCHLD, Sig_ign) to notify the kernel, the end of their child process is not interested, then the end of the child process, the kernel will be recycled, and no longer to the parent process
Send a signal.
or use the Sigaction function to set the sa_nocldwait for SIGCHLD, which will not enter a zombie state after the process is finished.
struct Sigaction sa;
Sa.sa_handler = sig_ign;
Sa.sa_flags = sa_nocldwait;
Sigemptyset (&sa.sa_mask);
Sigaction (SIGCHLD, &sa, NULL);

Fork two times, the parent process fork a child process, and then continue to work, the child process fork a grandchild process to exit, then the Sun process is init takeover, after the sun process is finished, Init will be recycled. However, the recycling of child processes is also done by the parent process.

  1. int nstatus;
  2. pid_t pid;
  3. PID = Vfork (); Generating child processes
  4. if (PID > 0)//parent process
  5. {
  6. Waitpid (PID, &nstatus, 0); Wait for the child process to end, or the child process will become a zombie process that persists even if the child process has finished executing
  7. }
  8. else if (0 = = pid)//Sub-process
  9. {
  10. PID = Vfork (); Generate grandchild Process
  11. if (PID > 0)
  12. {
  13. Exit (0); The child process exits, the grandchild process is adoptive to the INIT process, and its exit state is also handled by the INIT process, regardless of the original parent process
  14. }
  15. else if (0 = = pid)//grandchild process
  16. {
  17. if (EXECLP ("ls", "ls", NULL) < 0)
  18. {
  19. Perror ("EXECLP");
  20. Exit (-1);
  21. }
  22. }
  23. Else
  24. {
  25. Perror ("Vfork (Child)");
  26. }
  27. }
  28. Else
  29. {
  30. Perror ("vfork (parent)");
  31. }
  32. }
Reference: Http://baike.baidu.com/view/3063491.htm?fr=aladdin

Linux Zombie Process

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.