Signal Processing of threads sigaction () function (thread five) __ function

Source: Internet
Author: User
Tags sigint signal signal handler

1.sigaction () function is a signal processing function of a process

Return value: Return 0 if successful, return 1 if error

The function has three parameters

First parameter: Signal

Second parameter: A struct pointer to the struct sigaction type

The structure contains the signal shielding word, signal set processing function and so on.

Processing function of Sa_handler signal machine

The Sa_mask field describes a signal set, which is also a signal screen word.

Third parameter: is also a structure, but is an old state, generally null;

Summary so if you use Sigaction (), you first define a struct signalaction type structure and populate it.

Note that you must first empty the structure before you use it ... It.s a good habit.

At this time completes the signal processing function registration, on the other people to send the signal, sigaction function to capture. When the foreign signal is captured, it is given to the signal processing function to process the signal (the signal processing function is also in the structure of the struct Sigaction Act, and the structure is a union, so the call to the Sigaction function, the last call to the main, send several signals, The signal processing function is called several times, which is specifically described in the following code.

2. Some other processing signal set functions

Sigemptyset () Clear the blocking sign of all signal masks

Sigfillset () Sets the blocking flag for all signal masks

Sigaddset () increase individual signal blocking

Sigdelset () remove individual signal blocking

Sigismember () determines whether a particular signal is flagged as blocked in the mask

Extensions: What is a mask. (Excerpt from Baidu)

The signal mask is under POSIX, and each process has a signal mask (signal mask). Simply put, the signal mask is a "bitmap", each of which corresponds to a signal. If one of the bitmaps is 1, it means that the corresponding signal is temporarily "masked" during the execution of the current signal handler, so that the signal is not nested in response during execution.
Why do you block a signal? Let's take a look at the treatment of Ctrl_c. As you know, when a program is running, click Ctrl_c on the keyboard, the kernel will send a SIGINT signal to the corresponding process, and the default action for this signal is to end the process by Do_exit (). However, some applications may have their own processing for Ctrl_c, so set up a handler for SIGINT to point to a function in the application that responds to the Ctrl_c event in that function. However, in practice, it is found that two times Ctrl_c events tend to be too dense, sometimes just enter the first signal processing, the second SIGINT signal arrived, and the second signal is the default operation is to kill the process, so that the first signal handler is not finished. To avoid this, the signal is automatically shielded during the execution of a signal processing program. The so-called "shielding", and the signal ignored is different, it will only temporarily "cover" the signal, once the shielding removed, the signal has been reached to continue to be processed.

3.sigpromask signal (Mask) processing function for single process

For multithreaded signal (mask) processing functions pthread_sigmask ()

This function is consistent with the Sigprocmask function parameter

First parameter: There are three different forms

Sig_block: Adds a set to the current semaphore mask in which the set represents the signal to block

Sig_unblock: Deletes the set in the current signal mask, where set represents the blocking signal to be canceled

Sig_setmask: Replace with set in current signal mask, where set represents new signal mask

The second parameter, set based on the first selected case, can represent the signal to block, the blocking signal to be canceled, and the new signal mask

Third parameter: Old state, General null.

4 Program Design

General idea: Create two threads in main (), use the Join function to end the wait period, first use Sigaction () for sigquit signal in the child thread with Sig_handler () function processing, and then use Pthread_sigmask () In three cases of thread_fun1 and THREAD-FUN2 functions, the sigquit signals are masked, validated, and different situations sigaction () How to handle the signal processing functions.

Operation Structure Analysis

Although the THREAD_FUN1 and THREWAD_FUN2 functions are sent a sigquit signal in the main function. However, in the THREAD_FUN1 and threwad_fun2 thread functions, the Sigquit function is masked with pthread_sigmask (), so the last function does not run a signal processing function.

If the signal sigquit is not mask processed at thread_fun2

Run results at this time

This runs two times, and the two run results are a bit odd, because I don't have a mask (mask) for the second thread_fun2, so when I run the thread_fun2 function, the sigaction function gets the sigquit signal, And the second parameter of the function is a struct containing a union, so the second runtime calls the Signal Processing 2 function sig_handler2 ().

Looking back at this point, the first time the results of the run is not surprising, because the THREAD_FUN1 run after the call Sigaction () function when Sigaction () has captured the sigquit signal, so call the signal processing 1 function Sig_ Handler1 ().

Extension if we do not block the signal thread_fun1 and threwad_fun2, the result of this operation is

After the operation of the signal processing function was called two times, again explained that Sigaction () to the last run as the standard, send several times signal, the last sig_handler () signal processing function is called several times function.

To here the multithreading signal processing function notes basically finished, to me, these several signal processing function, has wasted my many brain cells, the Linux study Road, any heavy and the road is far ...

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.