I don't know if you remember the concept of blocking and pending signal sets in the process of signal processing, if you have forgotten, please refer to the "blocking signal and pending signal" in the article. 1. The signal in the multithread program
In multiple threads, each thread has its own set of blocked and pending signals. When one thread derives another thread, it inherits the blocking set of the parent thread, but does not inherit the pending signal set, and the new thread clears the pending signal set. 2. Correlation function 2.1 Set function of blocking signal set
In a multithreaded program, if you want to set the blocking signal set for a thread, you can no longer use the Sigprocmask function, but you should use Pthread_sigmask, which is defined as follows:
int pthread_sigmask (int how, const sigset_t *set, sigset_t *oldset);
The use of this function is the same as that of Sigprocmask.
How parameter sig_block this option means adding signals in the signal set indicated by the set parameter to the process blocking set sig_unblock This option is in contrast to the feature Sig_block, which indicates that the message that is specified in the thread blocking signal set is deleted sig_setmask This option indicates that the thread Block signal set set directly for your specified set
Set parameter
Indicates the set of signals you specified Oldset
Returns the old block signal set return value int
0 indicates success,-1 failure. 2.2 functions to get the pending signal
The function is still sigpending and does not change. Its prototype is as follows:
int sigpending (sigset_t *set);
2.3 Signal Sending function
The KILL function can only send functions to the specified process, but instead uses Pthread_kill to send a function to the specified thread. Its prototype is as follows:
int Pthread_kill (pthread_t thread, int sig);
3. The Experiment
Program Th_sig did the following: the main thread blocked the sigquit signal in the FUN1 thread blocking the SIGINT signal in the fun2 thread nothing blocked 3.1 code
Th_sig.c #include <unistd.h> #include <stdio.h> #include <signal.h> #include <pthread.h>//print
Signal set void Printsigset (const sigset_t *set) {int i;
for (i = 1; I <= i++) {if (i==33) Putchar (");
if (Sigismember (set, i) = = 1) putchar (' 1 ');
else Putchar (' 0 ');
} puts ("");}
FUN1 thread void* fun1 (void* arg) {//blocking SIGINT signal sigset_t mask, St;
Sigemptyset (&mask);
Sigaddset (&mask, SIGINT);
Pthread_sigmask (Sig_block, &mask, NULL);
while (1) {printf ("I ' m fun1:\t");
Sigpending (&ST);
Printsigset (&ST);
Sleep (3);
}///Fun2 thread void* fun2 (void* Arg) {sigset_t st;
while (1) {printf ("I ' m fun2:\t");
Sigpending (&ST);
Printsigset (&ST);
Sleep (3);
the int main () {//before the thread is created blocking Sigquit sigset_t mask, St;
Sigemptyset (&mask);
Sigaddset (&mask, sigquit);
Pthread_sigmask (Sig_block, &mask, NULL);
pthread_t Tid1, Tid2; Pthread_create (&TID1, NULL, FUN1, NULL);
Pthread_create (&tid2, NULL, fun2, NULL);
Sleep (2);
Pthread_kill (TID1, SIGINT);
while (1) {printf ("I ' m main:\t");
Sigpending (&ST);
Printsigset (&ST);
Sleep (3);
return 0; }3.2 Compiling and runningCompile
$ gcc Th_sig.c-o th_sig-lpthread
Run
Figure 1 Running results
Note on Figure 1: The left-hand side is the name of the thread, and the right side is the printed pending queue, which is the signal that is not processed by the signal processing function. The 1th column is the sighup signal, the 2nd column represents the SIGINT signal, and the 3rd column represents the Sigquit signal.
When the main thread sleeps for 2 seconds, a SIGINT signal is sent to the thread fun1, at which point the 2nd column of FUN1 's pending signal set becomes 1 (line 5th on Figure 1).
At some later time, CTRL + \ is pressed to indicate that the signal is sent sigquit to the process in the foreground process group. Next, you can see that the 3rd column in all the thread-pending signal sets has changed to 1. This illustrates two things: the thread inherits the parent thread blocking signal set kill signal is sent to all threads in the process 4. Summarizing that each thread has its own blocking signal set and the pending signal set thread inherits the parent thread's blocking signal set, and the new thread clears the pending signal set Pthread_kill Sends a signal to the specified thread kill to send a signal to all threads