Linux inter-process communication-Signal Set Function

Source: Internet
Author: User
Tags exit in sigint signal
We already know that we can terminate processes through signals or communicate between processes through signals, the program can also change the default Signal Processing Method by specifying the correlated processing function of the signal, or shield some signals so that they cannot be passed to the process. So how should we set the signal we need to process, and we don't need to deal with any other problems? The signal set function helps us solve these problems. For more information about how to use signal communication between Linux processes, refer to my other article-Linux Process Communication-use signal below is a signal function set: 1. Int sigemptyset (sigset_t * Set); this function is used to empty the signal set initialization. 2. Int sigfillset (sigset_t * Set); this function is used to initialize the signal set to include all the defined signals. 3. Int sigaddset (sigset_t * Set, int signo); this function is used to add the signal signo to the signal set. If the signal is set successfully, 0 is returned, and-1 is returned if the signal fails. 4. Int sigdelset (sigset_t * Set, int signo); this function is used to delete the signal signo from the signal set. If it succeeds, 0 is returned, -1.5 and INT sigismember (sigset_t * Set, int signo) are returned when a failure occurs. This function is used to determine whether the given signal signo is a member of the signal set. If 1 is returned, if not, return 0. If the given signal is invalid, return-1; 6, int sigpromask (INT how, const sigset_t * Set, sigset_t * oset ); this function can modify the signal shielding characters of a process according to the method specified by the parameter. The new signal shielding word is specified by the parameter set (not empty), and the original signal shielding word is stored in the oset (not empty. If set is empty, how does not make sense. However, this function is called at this time. If oset is not empty, the current signal shielding word is saved to oset. The different values and operations of how are as follows: If sigpromask is successfully completed, 0 is returned. If the value of how is invalid,-1 is returned, and errno is set to einval. Note: You can call this function to change the blocked word of a process. The previous function is only used to change the value of a variable and does not actually affect the blocked word of the process. 7. Int sigpending (sigset_t * Set); this function is used to write a group of signals that are stuck in the pending state in the blocked signal to the signal set pointed to by the parameter set, if the call is successful, 0 is returned. Otherwise,-1 is returned, and errno is set to indicate the cause of the error. 8. Int sigsuspend (const sigset_t * sigmask). This function replaces the blocked word of the process with the signal set given by the sigmask parameter, and then suspends the execution of the process. Note that the operation sequence is to replace and then suspend the execution of the program. The program continues after the signal processing function is executed. If the received signal terminates the program, sigsuspend will not return. If the received signal does not terminate the program, sigsuspend will return-1 and errno will be set to eintr. Note: If a signal is blocked by the process, it will not be passed to the process, but it will stay in the pending status. When the process unblocks the processing signal, the signal to be processed will be processed immediately. The following example illustrates the usage of the above function. The source file is sigset. C, and the code is as follows:
# Include <unistd. h> # include <signal. h> # include <sys/types. h> # include <stdlib. h> # include <stdio. h> void handler (INT sig) {printf ("handle the signal % d \ n", sig);} int main () {sigset_t sigset; // used to record the blocked sigset_t ign; // used to record the blocked Signal Set struct sigaction Act; // cleared the signal set sigemptyset (& sigset); sigemptyset (& IGN ); // Add the signal sigintsigaddset (& sigset, SIGINT) to the signal set; // set the processing function and the signal set act. sa_handler = handler; sigemptyset (& act. sa_mask); Act. SA _ Flags = 0; sigaction (SIGINT, & act, 0); printf ("wait the signal SIGINT... \ n "); pause (); // suspends the process and waits for the signal. // set the process blocking word. In this example, the sigintsigprocmask (sig_setmask, & sigset, 0) is blocked ); printf ("Please press Ctrl + C in 10 seconds... \ n "); sleep (10); // test whether SIGINT is blocked sigpending (& IGN); If (sigismember (& ign, SIGINT )) printf ("The SIGINT signal has ignored \ n"); // Delete the signal sigintsigdelset (& sigset, SIGINT) in the signal set; printf ("wait the signal SIGINT... \ n ");// Reset the blocked word of the process, that is, cancel the blocking of SIGINT // and suspend the process sigsuspend (& sigset); printf ("the app will exit in 5 seconds! \ N "); sleep (5); exit (0 );}
The running result is as follows: first, we can use the sigaction function to change the default behavior of the SIGINT signal and execute the specified function handler. Therefore, the statement handle the signal 2 is output. Then, use sigprocmask to set the signal shielding characters of the process and shield the SIGINT signal. After 10 seconds, use the sigpending function to obtain the blocked signal set, the blocked signal SIGINT is detected, and the SIGINT signal is output.
Has ignored. Finally, use the sigdelset function to remove the signal SIGINT previously added to the sigset using the sigaddset function, call the sigsuspend function, and change the blocked word of the process to sigset again (without SIGINT ), and suspend the process. Because the previous SIGINT signal stays in the pending status, but now the process no longer blocks the signal, the process immediately processes the signal, so at the end, you do not need to enter Ctrl + C, and the subsequent processing statement will appear (see the content mentioned above). After five seconds, the program will successfully exit.

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.