10.7 SIGCHLD Definition

Source: Internet
Author: User
Tags posix signal handler

Often confusing two signals are SIGCLD and SIGCHLD, the signal SIGCLD originated from system V, the meaning of the signal and from the BSD signal sigchld inconsistent. The posix.1 signal is also known as SIGCHLD. The semantics of the signal sigchld from BSD are normal, and when the signal appears, the state of the child process changes, and then we need to call a wait function to see what happened.

System V has been different from other signals since the processing history of SIGCLD, Svr4i-based systems maintain this problematic tradition if we use function signal or sigset (the function used to set signal processing functions in SVR3) to set the processing function of the signal, SIGCLD early processing includes the following behaviors:

    1. If the signal processing function is specified as sig_ign, the child process of the calling process will never spawn a zombie process. Note This differs from the default processing of the signal and is ignored in Figure 10.1 when it refers to its default processing. Instead, when the child process terminates, the state of the child process is discarded, and if its parent process then calls one of the wait functions, the calling process is blocked until all the child processes are terminated, and then the wait function returns-1, and errno is set to Echild. (Note: The signal is ignored by default processing, but the default processing does not produce the above behavior, but we must set the processing function of the signal to sig_ign.)

      POSIX.1 does not specify what happens when SIGCHLD is ignored, so the above processing is also allowed, and the XSI option requires that the processing be supported by the signal sigchld.
      4.4BSD always generates a zombie process when SIGCHLD is ignored, and if we want to avoid a zombie process, we have to wait for all of the child processes, in SVR4, as long as the signal is called or one of the Sigset sets the SIGCHLD handler to Sig_ IGN, the zombie process will never be produced, and the 4 platforms described in this book are the same as the SVR4.
      Using the function sigaction, we can set the SA_NOCLDWAIT flag to avoid the zombie process, which is supported for the 4 platforms described in this book.

    2. If we set SIGCLD's handler function, the kernel will immediately check if there is a child process waiting to be processed, and if so, call the SIGCLD function immediately.

The second of these changes the way we write the signal-processing functions, as we will in the following example.

Example

As mentioned in section 10.4, the first thing in the signal processing function is to call the signal function again, so that in order to minimize the signal processing function is reverted to the default processing time window, Figure 10.6 is a program written according to this rule, but the program in the traditional system V platform does not work well, its output will be a continuous string "SIGCLD received", most of the process due to stack space overflow and abnormally terminated.

  
 
  1. #include "apue.h"
  2. #include <sys/wait.h>
  3. static void sig_cld(int signo);
  4. int main()
  5. {
  6. pid_t pid;
  7. if(signal(SIGCLD, sig_cld) == SIG_ERR)
  8. perror("signal error");
  9. if((pid = fork()) < 0)
  10. {
  11. perror("fork error");
  12. }
  13. else if(pid == 0)
  14. {
  15. /* child process*/
  16. sleep(2);
  17. _exit(0);
  18. }
  19. pause(); /*parent process*/
  20. exit(0);
  21. }
  22. static void sig_cld(int signo)
  23. {
  24. pid_t pid;
  25. int status;
  26. printf("SIGCLD received\n");
  27. if(signal(SIGCLD, sig_cld) == SIG_ERR) /*reestablish handler*/
  28. perror("signal error");
  29. if((pid = wait(&status)) < 0) /*fetch child status*/
  30. perror("wait error");
  31. printf("pid = %d\n", pid);
  32. }

The problem with the above procedure is that the function signal is called at the beginning of the signal processing function, which corresponds to the second item discussed above – The kernel checks to see if there is a child process that requires wait for (which is the case in the above procedure because we are processing the SIGCLD signal), So the call to the signal function produces another call to the signal handler function, which in turn invokes the signal function, and the entire process repeats itself.
In order to solve the problem of the above procedure, we must move the call to the function signal to the wait function, and by doing so we call the signal function after capturing the terminating state of the child process, and the signal will be generated only when the other child processes are terminated.

POSIX.1 statement: When we establish SIGCHLD signal processing function, if there is a child process that has not yet been stopped wait, whether the signal needs to be generated and not specified, so the above is allowed, but for posix.1, after the signal is generated, the signal processing function will not be reset to the default function (Assuming that we are using the posix.1 sigaction function to set the signal handler function), there is no need to call the function signal again in the signal handler function. So the above problem disappears.



From for notes (Wiz)

10.7 SIGCHLD Definition

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.