Generation and avoidance of Linux zombie process

Source: Internet
Author: User
Tags signal handler

 
Lingdxuyan Source: Chinaunix Technology Blog, this article copyright by Lingdxuyan all, if necessary to reprint, please indicate the source.

When a process calls the Exit command to end its own life, it 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 only to turn a normal process into a zombie process, and cannot be completely destroyed).
"The production of zombie processes At the time each process exits, the kernel frees all of the resources of the process, including open files, memory, etc., but still retains certain information about it (including the process ID, exit status, the
Termination status of the process, runtime the amount of the CPU time taken by the process, etc.) until the parent process is picked up by Wait/waitpid to be released. At this point the process is in a zombie state and the process becomes a zombie process (Zombie processes). This ensures that the parent process can get state information at the end of the child process.
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, and can not be dispatched, only in the process list to keep a location, record the process's exit status and other information for other processes to collect, in addition, Zombie processes no longer occupy any memory space. 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 in a zombie state, if the parent process is finished, the zombie child process becomes "orphan process" , adoptive to process Init,init 1th will always be responsible for cleaning up the zombie process, it produces all the zombie process also disappeared (at the end of each process, the system will scan the current system is running all the processes, see if there is no process is just finished the process of the child process, if so, It is up to Init to take over him and become his father's process). But if the parent process is a loop and does not end, then the child process remains zombie, which is why there are sometimes a lot of zombie processes in the system. How to view the zombie process, using the command PS, you can see the marked Z process is a zombie process.
"The danger of zombie processes If the parent process does not call Wait/waitpid, then the reserved piece of information will not be freed, its process number will be occupied, but the system can use a limited number of processes, if a large number of zombie processes, because there is no available process number will cause the system can not produce a new process.
"The avoidance of zombie processes
    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, the kernel can be notified with signal (SIGCHLD, sig_ign), and the end of the child process is not interested, and the kernel recycles and no longer sends a signal to the parent process when the subprocess ends.

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);

4.fork two times, the parent process fork a child process, and then continue to work, the child process fork a grandchild process after exiting, 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. int nstatus; pid_t pid;

PID = Vfork (); Generating child processes
if (PID > 0)//parent process
    {
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
    }
else if (0 = = pid)//Sub-process
    {
pid = Vfork (); Generate grandchild Process
if (PID > 0)
        {
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
        }
else if (0 = = pid)//grandchild process
        {
if (EXECLP ("ls", "ls", NULL) < 0)
            {
Perror ("EXECLP");
exit (-1);
            }
        }
Else
        {
perror ("vfork (Child)");
        }
    }
Else
{
perror ("vfork (parent)");
    }
}

Generation and avoidance of 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.