linux-communication between processes-signal set function "Go"

Source: Internet
Author: User
Tags exit in sigint signal

We already know that we can terminate the process by signal, or through the signal to communicate between processes, the program can also specify the signal's associated processing function to change the signal's default processing mode, you can also block some signals, so that it cannot be passed to the process. So how do we set the signal we need to deal with, and what signals do we need to deal with? The signal set function is to help us solve these problems.   For more information about using signal communication between Linux processes, you can refer to my other article--linux interprocess communication-using signals   The following is a set of signal functions: 1, int sigemptyset (sigset_t *set); The function is to initialize the signal set to NULL.  2, int sigfillset (sigset_t *set); The function is to initialize the signal set to include all the defined signals.  3, int sigaddset (sigset_t *set, int signo); The function is to add the signal Signo to the set of signals, return 0 on success, and 1 on failure.  4, int sigdelset (sigset_t *set, int signo); The function is to remove the signal signo from the set of signals, return 0 on success, return -1. 5, int sigismember on Failure ( sigset_t *set, int signo); This function is to determine whether a given signal signo is a member of the signal set, if it is returned 1, if not, returns 0 if the given signal is invalid, return -1; 6, int sigpromask ( int how, const sigset_t *set, sigset_t *oset); The function can modify the signal screen word of a process according to the method specified by the parameter. The new signal-shielding word is specified by the parameter set (NOT NULL), and the original signal-shielding word is saved in Oset (non-empty). If set is null, how is meaningless, but at this point the function is called and if Oset is not empty, the current signal mask word is saved to oset. The different values and operations of the  how are as follows: If Sigpromask successfully completed return 0, if the How value is invalid return-1, and set errno to Einval.   NOTE: Call this function to change the screen word of the process, the previous function is to change the value of a variable, does not really affect the process of the mask word.  7, int sigpending (sigset_t *set), the function of which is to stay in the blocked signalA set of signals for the state is written to the signal set that the parameter set points to, the successful call returns 0, otherwise returns-1, and the set errno indicates the cause of the error.  8, int sigsuspend (const sigset_t *sigmask); This function suspends the execution of a process by replacing the mask word of the process with the set of signals given by the parameter sigmask. Note that the sequence of operations is to replace the execution of the pending program first. The program will continue to execute after the signal processing function has finished executing. If the received signal terminates the program, Sigsuspend will not return, and if the received signal does not terminate the program, Sigsuspend returns 1, and the errno is set to Eintr.   Special reminder: If a signal is blocked by the process, it will not be passed to the process, but will stay in the pending state, when the process is de-processing signal blocking, the pending signal will be processed immediately.   below with an example to illustrate the use of the above function, the source file is sigset.c, the code is as follows:  [CPP]View PlainCopyprint?
  1. #include <unistd.h>
  2. #include <signal.h>
  3. #include <sys/types.h>
  4. #include <stdlib.h>
  5. #include <stdio.h>
  6. void handler (int sig)
  7. {
  8. printf ("Handle the Signal%d\n", SIG);
  9. }
  10. int main ()
  11. {
  12. sigset_t Sigset; //Used to record screen words
  13. sigset_t IGN; //For recording blocked signal sets
  14. struct Sigaction Act;
  15. //Clear signal set
  16. Sigemptyset (&sigset);
  17. Sigemptyset (&ign);
  18. //Add signal to signal set SIGINT
  19. Sigaddset (&sigset, SIGINT);
  20. //Set processing functions and signal sets
  21. Act.sa_handler = handler;
  22. Sigemptyset (&act.sa_mask);
  23. act.sa_flags = 0;
  24. Sigaction (SIGINT, &act, 0);
  25. printf ("Wait the Signal sigint...\n");
  26. Pause (); //suspend process, wait for signal
  27. //Set the process screen word, in this case, to mask the SIGINT
  28. Sigprocmask (Sig_setmask, &sigset, 0);
  29. printf ("Please press CTRL + C in seconds...\n");
  30. Sleep (10);
  31. //test whether the SIGINT is blocked
  32. Sigpending (&ign);
  33. if (Sigismember (&ign, SIGINT))
  34. printf ("The SIGINT signal has ignored\n");
  35. //delete signal in signal set SIGINT
  36. Sigdelset (&sigset, SIGINT);
  37. printf ("Wait the Signal sigint...\n");
  38. //Reset the screen character of the process to remove the SIGINT
  39. //and suspend process
  40. Sigsuspend (&sigset);
  41. printf ("The app would exit in 5 seconds!\n");
  42. Sleep (5);
  43. Exit (0);
  44. }
The results of the operation are as follows: first, we can cross the Sigaction function to change the default behavior of the SIGINT signal so that it executes the specified function handler, so the statement is output: Handle the signal 2. Then, through the sigprocmask set process signal shielding word, the SIGINT signal shielding up, so after 10 seconds, with the sigpending function to obtain the blocked signal set, detected the blocked signal SIGINT, output the SIGINT signal Has ignored. Finally, the function Sigdelset function is used to remove the signal SIGINT previously added to the sigset with the Sigaddset function, and then the function sigsuspend is called, the screen word of the process is modified again to Sigset (not including SIGINT), and the process is suspended. Because the previous SIGINT signal stays in the pending state, and now the process is no longer blocking the signal, so the process immediately processing the signal, so that at the end, you do not have to input CTRL + C will also appear after the processing of the statement (see the above special reminder), and finally after 5 seconds the program successfully exited.

linux-communication between processes-signal set function "Go"

Related Article

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.