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.
- IntNstatus;
-
- Pid_t PID;
-
-
- PID=Vfork();//Generate sub-process
- If (PID>0)//Parent Process
-
- {
- 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
-
- }
- Else If (0==PID)//Sub-Process
-
- {
-
- PID=Vfork();//Generate sun Process
- If (PID>0)
-
- {
-
- 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
- }
-
- Else If (0==PID)//Sun Process
-
- {
- If (Execlp("Ls", "Ls", Null) <0)
-
- {
-
- Perror("Execlp");
- Exit(-1);
-
- }
-
- }
-
- Else
-
- {
- Perror("Vfork (child )");
-
- }
-
- }
-
- Else
-
- {
-
- Perror("Vfork (parent )");
-
- }
- }