Multi-process Wait

Source: Internet
Author: User
Tags terminates

1. Concept

1, orphan process: A parent process exits, and one or more of its child processes are still running, then those child processes will become orphans. The orphan process will be adopted by the INIT process (process number 1) to ensure that each process will have a parent process. While the init process automatically wait for its child processes, all processes that are taken over by Init do not become zombie processes.

Add: The orphan process is a process without a parent process, and the task of the orphan process falls to the INIT process. Whenever an orphan process occurs, the kernel sets the parent process of the orphan process to init, and the init process loops through its exited child processes. Thus, when an orphan process ends its life cycle, the INIT process will handle all of its aftermath. So the orphan process does not have any harm.

2, Zombie process: a process using fork to create a child process, if the child process exits, and the parent process does not call wait or waitpid to get the state information of the child process, then the process descriptor of the child process is still stored in the system, this process is called a zombie process. When you observe the execution state of a process with the PS command, you see that the status bar for these processes is defunct. The zombie process is a long-dead process, but still occupies a position (slot) in the Process table (processs table).

Supplemental (kernel): After a process terminates, the kernel releases all the storage areas used by the terminating process (called by the exit system call), closes all open files, and so on. However, the kernel holds a certain amount of information for each terminating child process, sets the zombie state to maintain the child process information so that the parent process gets it at a later time, including at least the process ID, the process's terminating state, and the CPU time used by the process. So this information is available when the parent process that terminates the child process calls wait or waitpid. Any child process (except Init) disappears immediately after exit, leaving behind a data structure that is called an outer zombie process, waiting for the parent process to handle it. This is the stage that each child process must undergo. In addition, when the child process exits, it sends a SIGCHLD signal to its parent process.

Strictly speaking, the zombie process is not the root of the problem, but the main culprit is the parent process that spawned a lot of zombie processes. Thus, after killing the parent process that spawned a large number of zombie processes (via kill to send a sigterm or Sigkill signal), the resulting zombie process becomes an orphan process that will be taken over by the INIT process and the INIT process will wait for the orphan process, Frees the resources in the system process tables that they occupy.

2, how to avoid the zombie process?

1, through signal (SIGCHLD, sig_ign) notify the kernel to the end of the child process does not care, collected by the kernel. If you do not want the parent process to hang , you can add a statement to the parent process: signal (sigchld,sig_ign), which indicates that the parent process ignores the SIGCHLD signal, which is sent to the parent process when the child process exits.

2, the parent process calls Wait/waitpid and other functions wait for the child process to end, if there is no child process exit Wait causes the parent process to block . Waitpid can be returned immediately by passing Wnohang so that the parent process does not block .

3, if the parent process is busy can register the signal processing function with signal, in the signal processing function call Wait/waitpid wait for the child process to exit.

4, through two times to call fork. The parent process first calls Fork to create a child process and then waitpid waits for the child process to exit, and then quits after the child process fork a grandchild process. This way the process exits after the parent process waits to be recycled, and for the grandson process its parent process has exited so the grandchild process becomes an orphan process, the orphan process is taken over by the Init process, and Init waits for recycling after the grandchild process is over.

The first method ignores the SIGCHLD signal, which is often used as a technique for concurrent servers, because concurrent servers often fork many sub-processes, and a server process is required to wait for the resource to be cleaned up after the end of the child process. If you set this signal to ignore, it allows the kernel to transfer the zombie subprocess to the Init process, eliminating the use of a large number of zombie processes for system resources.

3. Experiment

The following experiment has been used to understand the parent process wait multiple child processes

#include <stdlib.h>#include<unistd.h>#include<sys/types.h>#include<sys/wait.h>intMain () {pid_t pid; if(fork () = =0) {printf ("1--this is the child process. PID =%d\n", Getpid ()); Sleep (3); printf ("%d exit\n", Getpid ()); Exit (0); }    if(fork () = =0) {printf ("2--this is the child process. PID =%d\n", Getpid ()); Sleep (Ten); printf ("%d exit\n", Getpid ()); Exit (0); }    if(fork () = =0) {printf ("3--this is the child process. PID =%d\n", Getpid ()); Sleep ( -); printf ("%d exit\n", Getpid ()); Exit (0); }    //While (1)// {    //pid = Wait (NULL); //printf ("Parent:%d,return of wait:%d\n", Getpid (), PID); //if (pid = =-1)//     {    //Break ; //     }    // }    //loop wait for each sub-process output, and the last return value is-1 means no child processes, and the parent process exits//while (Wait (NULL)! =-1); //wait for the parent process to exit after the last child process exits//printf ("Parent:%d,return of wait:%d\n", Getpid (), Wait (NULL)); //wait after the first returned process, the parent process returns, the other does not return the process to the orphaned process, which is taken over by the Init process//While (Wait (NULL)! =-1)// {    //printf ("Parent:%d,return of wait:%d\n", Getpid (), Wait (NULL)); // }    //While wait for the first exit child process, printf wait for the second child process output, while wait for the third child process, and finally printf is 1, indicating that there is no child process, the parent process exitssignal (sigchld,sig_ign);//ignore the child process exit, the kernel directly forwards to init processing defunct, so that even if the parent process does not exit, there will be no zombie processSleep -);//The parent process does not wait for any child processes, does not set sig_ign, and is still running while the child process exits, at which point all child processes become zombie processes until the parent process exits and the child process is taken over by the Init processprintf"exit\n"); return 0;}

Summarize:

Call a wait, then the parent process returns after the first child process returns, and the other child processes are still running, why not become a zombie process, but directly by the Init takeover

-----The parent process returns, so the other child processes become orphaned by the process, which is taken over directly by Init, so Ps–ef | grep defunct no zombie process

If the parent process fork multiple child processes, the parent process is still running after the child process exits, but no child processes are waiting, the child process at this time will change to the zombie process until the parent process is finished and then taken over by the INIT process

-----The parent process does not wait to block, and is still running after the child process has ended. In order to not spawn a zombie process, you can set signal (Sigchld,sig_ign) in the parent process; Causes the parent process to ignore the child process to exit, and the process directly to the INIT takeover

Concept reference:

Https://www.cnblogs.com/wuchanming/p/4020463.html

Https://www.cnblogs.com/Anker/p/3271773.html

Multi-process Wait

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.