Linux zombie Process

Source: Internet
Author: User

Http://blog.chinaunix.net/uid-23089249-id-210808.html

 

When a process calls the exit command to end its own life, it is not actually destroyed, but it leaves a data structure called Zombie (the system calls exit, its function is to exit the process, but it is only limited to converting a normal process into a dead process and cannot completely destroy it ).

 

I. Creation of zombie Processes
When each process exits, the kernel releases all resources of the process, including open files and occupied memory, however, some information (including the process ID, exit the status of
Termination status of the process, run time the amount of CPU time taken by the process, etc.), not released until the parent process is obtained through wait/waitpid. At this time, the process is in a zombie state and becomes a zombie process (zombie process ). This ensures that the parent process can obtain the status information at the end of the Child process.
In the Linux Process status, the zombie process is a very special one. It has abandoned almost all the memory space and has no executable Code It cannot be scheduled. It only keeps a location in the process list and records information about the process's exit status for other processes to collect. In addition, the zombie process no longer occupies any memory space. It requires its parent process to collect dead parts for it. If its parent process does not have the sigchld signal processing function installed, it calls wait or waitpid () to wait until the child process ends, if the signal is not explicitly ignored, it will remain frozen. If the parent process ends, the zombie sub-process will become "orphan process" and pass to process init on process 1, init is always responsible for clearing dead processes, and all dead processes generated by it also disappear (when each process ends, the system will scan all processes running in the current system, check whether a process is a child process of the process that has just ended. If yes, init will take over the child process and become its parent process ). However, if the parent process is a loop and does not end, the child process remains frozen, which is why many zombie processes sometimes exist in the system. How to view the zombie process, using the command ps, we can see that a process marked as Z is a zombie process.

 

Search for all dead processes

PS-a-o stat, ppid, PID, CMD | grep-e '^ [zz]' 

 

Ii. dangers of zombie Processes
If the parent process does not call wait/waitpid, the information retained will not be released, and the process number will be occupied, but the process number that the system can use is limited, if a large number of dead processes are generated, the system cannot generate new processes because there is no available process number.

 

3. Avoiding zombie processes
1. The parent process waits for the child process to end through functions such as wait and waitpid, this causes the parent process to be suspended
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 ends, the parent process receives the signal. You can call wait to recycle the child process in the signal processing function.
3. If the parent process does not care about when the child process ends, you can use signal (sigchld, sig_ign) notify the kernel that it is not interested in the end of the Child process. After the child process ends, the kernel will recycle it and will not send a signal to the parent process
.
or use the sigaction function to set sa_nocldwait for sigchld, so that the process will not become frozen after the process ends.
struct sigaction SA;
SA. sa_handler = sig_ign;
SA. sa_flags = sa_nocldwait;
sigemptyset (& SA. sa_mask);
sigaction (sigchld, & SA, null);
4. Fork twice. The parent process fork is a sub-process and continues to work, the child process fork exits after a sun process, and the sun process is taken over by init. After the sun process ends, init will recycle it. However, the parent process is required to reclaim sub-processes.

  1. IntNstatus;
  2. Pid_t PID;
  3. PID=Vfork();//Generate sub-process
  4. If (PID>0)//Parent Process
  5. {
  6. Waitpid(PID, &Nstatus,0);//Wait until the sub-process ends,Otherwise, the child process becomes a zombie process.,Always exist,Even if the sub-process has ended
  7. }
  8. Else If (0==PID)//Sub-Process
  9. {
  10. PID=Vfork();//Generate sun Process
  11. If (PID>0) 
  12. {
  13. Exit(0);//Sub-process exited,Sun process passed through to INIT process,The exit status is also handled by the INIT process.,Irrelevant to the original parent process
  14. }
  15. Else If (0==PID)//Sun Process
  16. {
  17. If (Execlp("Ls", "Ls", Null) <0)
  18. {
  19. Perror("Execlp");
  20. Exit(-1);
  21. }
  22. }
  23. Else
  24. Perror("Vfork (child )"); 
  25. }
  26. Else
  27. Perror("Vfork (parent )"); 
  28. }

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.