SIGCHLD Signal Processing

Source: Internet
Author: User

When the process terminates in Linux, the kernel sends a SIGCHLD signal to the parent process with several features:

1. During the operation of a signal processing function, the signal being submitted is blocked.

2. If a signal is generated one or more times during blocking, the signal is usually only submitted once after it has been unblocked, which means that the Linux signal is not queued by default.

Give me a chestnut:

The first child process of the process terminates the resulting signal (1), (1) The second sub-process in the signal processing terminates the signal (2), at which point (1) is in the processing state, (2) is in the waiting process state, and then there is a third child process termination signal (3) (4) (5) ; (1) After the signal processing function is executed, only the (2) signal is processed and the remaining Signals (3) (4) (5) are lost.

When writing a concurrent service program for a Linux multi-process, we need to handle the client exit, and the server fork out the child process into the zombie (zombie) process, usually do:

Signal (SIGCHLD, sig_chld); void sig_chld (int signo) {pid_t pid;    int stat;    while (PID = Waitpid ( -1, &stat, Wnohang)) > 0) {printf ("Child%d terminated\n", PID); } return;

Here we specify the Wnohang option, which tells Waitpid not to block when there are child processes that have not been terminated at runtime. We cannot call wait in the loop because there is no way to prevent wait from blocking when a running child process has not yet terminated. It is also worth noting that it is not recommended to use a non-reentrant function such as printf in a signal processing function.

View Waitpid's Man Handbook, which is described in this section:

posix.1-2001 Specifies that if the disposition of SIGCHLD are set to Sig_ign or the SA_NOCLDWAIT flag are set for SIGCH LD (see Sigaction (2)), then children that terminate does not become zombies and a call to wait () or waitpid () would block unt Il all children has terminated, and then fail with the errno set to Echild.  

Two other methods of zombie process processing were provided: signal (SIGCHLD, sig_ign); or set the SA_NOCLDWAIT flag for SIGCHLD.

The above method, in the high concurrency scenario still has the flaw, the SIGCHLD signal in the above code function, actually only notifies carries on the waitpid call, Waitpid loops processes all already terminates the subprocess. Suppose a situation, the server supports the maximum of 2k concurrency, the full load when the 2k client exits at the same time, according to the above example there will be a large number of signal loss, the resulting zombie process in the next time the parent process receives the SIGCHLD signal is cleared, the server can only provide a small number of concurrent requests (worst case 2 concurrency);

Briefly describe the problem: under high concurrency, the waitpid of the above code is not in time to clean up the zombie process.

One solution is for ourselves to maintain the exit status of the child process. Creates a queue that, when the child process exits, blocks the PID into the queue, blocking the Read and processing queue in the thread.


SIGCHLD Signal Processing

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.