Processing of SIGCHLD signal in UNP TCP client server back-up program

Source: Internet
Author: User

In the fifth chapter, there is an example that simulates a client's concurrent termination of a TCP connection, and the server captures and processes the SIGCHLD signal and calls the WAITPID function to prevent the zombie process from appearing. The core sentence in the Signal processing function is:

1      while (PID = waitpid (-10  )2    {3             printf ("  Wait child pid:%ld\n", (long) PID); 4     }

This is an improved method used when the wait function is not resolved when the N sub-process terminates at the same time, resulting in only 1 child processes being wait while other N-1 sub-processes become zombie processes, using wait's signal processing function

1 pid = Wait (&statloc);

  What is a zombie process?

The zombie process is that the process itself exits, and its parent process does not exit, and the parent process does not wait for the class processing, causing the process to occupy the resources, such as the process PID, record the process information structure is not released, the process into a dead state, such a process is called the zombie process.

  The danger of zombie process

Once the zombie process accumulates, such as having a long-running program that constantly produces zombie processes, it eventually exhausts the PID and memory available to the system so that new processes cannot be created. Even if these are not exhausted, the memory consumption of excessive zombie processes can also affect the performance of the machine.

  How to prevent the zombie process from appearing?

When the parent process calls the Wait class function, the child process's resources can be freed, and the child process will not become a zombie process. The wait class function is usually called in the signal processing function by capturing the SIGCHLD signal.

First man down wait and waitpid

       #include <sys/types.h>       <sys/wait.h>       pid_t Wait (int *status);        int int options);

Wait

The function blocks until either one of the child processes is terminated or is interrupted by a signal. The exit state of the terminating child process returns the low byte stored in the parameter status, and the return value is the end of the subprocess PID.

Waitpid:

Respond differently depending on the PID and options.

PID > 0,wait Process ID for a child process with PID

PID = 0,wait Process Group ID is any child process that invokes the process ID

PID = -1,wait any one child process

PID <-1, wait process group ID is |pid| of any child process

The options also offer a different option, which can be combined with 0 or one or more options

    Wnohang

       Returns immediately when no child process is found to have exited.

wuntraced

wcontinued

  

Since the Linux signaling mechanism is not queued, the signal handler can catch up to 2 times for multiple simultaneous signals, while the wait function processes up to one subprocess at a time, so a zombie process will inevitably occur when there are more than 2 child processes at the same time. So why 1 while (PID = Waitpid (-1, &statloc, Wnohang)) > 0 ) can solve this problem ?

Suppose there are 5 sub-processes, P1,P2....P5, first P1,P2 end, capture to the signal, the above WHILE+WAITPID statement will be a one-time to complete all the finished child processes, there are still 3 child processes are not finished, then waitpid return 0, exit the loop, If, during the processing of a function call, there is another P3 end, and wait until the handler returns, the signal is immediately captured, and the handler function is processed again to P3, so that the signal can be captured regardless of whether the child process ends at the end of the processing function or before Whether the end is 1 sub-processes or many sub-processes, each can be waitpid processing, so there is no longer a zombie process appears.

Why use while (pid = waitpid (-1, &statloc, Wnohang)) > 0 ) instead of ' >= 0 '?? equals 0 refers to a child process that still exists, but the child process is not finished, and if >=0 is used, the handler will loop in while if one of the child processes is not finished so that the parent process cannot be executed, so no >= is used.

The fundamental reason why the use of while+waitpid to prevent the zombie process from appearing is that I think once the signal is lost, then the child process with it will not be waitpid processed. In fact, the signal here is just a notification function, for example, I can use the parent process to poll waitpid, without the signal processing function can wait for all the end of the child process. I ended a sub-process one hours ago, one hours before using waitpid to deal with, so that the dead state of the sub-process can be handled correctly.

The discussion on the above questions http://bbs.chinaunix.net/thread-828942-1-1.html this post for a better explanation.

Processing of SIGCHLD signal in UNP TCP client server back-up program

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.