哪个线程处理信号? [英] Which thread handles the signal?
问题描述
我有2个线程(线程1和线程2).而且我有SIGINT
的信号配置.每当SIGINT
发生时,线程2应该处理该信号.为此,我在程序下面写了
I have 2 threads(thread1 and thread2). And I have signal disposition for SIGINT
. Whenever SIGINT
occurs thread 2 should handle the signal. For that I wrote below program
void sig_hand(int no) //signal handler
{
printf("handler executing...\n");
getchar();
}
void* thread1(void *arg1) //thread1
{
while(1) {
printf("thread1 active\n");
sleep(1);
}
}
void * thread2(void * arg2) //thread2
{
signal(2, sig_hand);
while(1) {
printf("thread2 active\n");
sleep(3);
}
}
int main()
{
pthread_t t1;
pthread_t t1;
pthread_create(&t1, NULL, thread1, NULL);
pthread_create(&t2, NULL, thread2, NULL);
while(1);
}
我编译并运行了程序.每1秒钟打印一次"thread1 active",每3秒钟打印"thread2 active".
I compiled and and run the program. for every 1 second "thread1 active" is printing and for every 3 seconds "thread2 active" is printing.
现在我生成了SIGINT
.但是它会像上面那样打印"thread1 active"和"thread2 active"消息.再次生成SIGINT
,现在每3秒仅打印一次"thread2 active"消息.再次生成SIGINT
,现在所有线程都被阻塞.
Now I generated SIGINT
. But its printing "thread1 active" and "thread2 active" messages like above. Again I generated SIGINT
, now for every 3 seconds only "thread2 active" message is printing. Again I generated SIGINT
, now all threads are blocked.
因此,我了解到主线程第一次执行信号处理程序.线程2第二次执行处理程序,线程2最后执行信号处理程序.
So I understood, for first time main thread executing signal handler. For second time thread1 executing handler and lastly thread2 executing signal handler.
如何像发生信号时一样编写代码,只有线程2才能执行我的信号处理程序?
How I can write the code like whenever signal occurs, only thread2 have to execute my signal handler?
推荐答案
如果您向进程发送信号,则该进程中的哪个线程将处理该信号不确定.
If you send a signal to a process, which thread in the process will handle this signal is undetermined.
根据 pthread(7)
:
POSIX.1还要求线程共享一系列其他属性(即,这些属性是进程范围而不是每个线程的属性):
...
-信号处置
...
POSIX.1 also requires that threads share a range of other attributes (i.e., these attributes are process-wide rather than per-thread):
...
- signal dispositions
...
POSIX.1区分了针对整个进程的信号和针对各个线程的信号的概念.根据POSIX.1,进程控制信号(例如,使用kill(2)
发送的信号)应由进程中单个任意选定的线程处理.
POSIX.1 distinguishes the notions of signals that are directed to the process as a whole and signals that are directed to individual threads. According to POSIX.1, a process-directed signal (sent using kill(2)
, for example) should be handled by a single, arbitrarily selected thread within the process.
如果希望进程中的专用线程处理某些信号,请参见 pthread_sigmask(3)
向您展示如何做到这一点:
If you want a dedicated thread in your process to handle some signals, here is an example from pthread_sigmask(3)
shows you how to do it:
下面的程序在主线程中阻止一些信号,然后创建一个专用线程以通过sigwait(3)提取这些信号.以下shell会话演示了其用法:
The program below blocks some signals in the main thread, and then creates a dedicated thread to fetch those signals via sigwait(3). The following shell session demonstrates its use:
$ ./a.out &
[1] 5423
$ kill -QUIT %1
Signal handling thread got signal 3
$ kill -USR1 %1
Signal handling thread got signal 10
$ kill -TERM %1
[1]+ Terminated ./a.out
程序源
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <signal.h>
#include <errno.h>
/* Simple error handling functions */
#define handle_error_en(en, msg) \
do { errno = en; perror(msg); exit(EXIT_FAILURE); } while (0)
static void *
sig_thread(void *arg)
{
sigset_t *set = arg;
int s, sig;
for (;;) {
s = sigwait(set, &sig);
if (s != 0)
handle_error_en(s, "sigwait");
printf("Signal handling thread got signal %d\n", sig);
}
}
int
main(int argc, char *argv[])
{
pthread_t thread;
sigset_t set;
int s;
/* Block SIGQUIT and SIGUSR1; other threads created by main()
will inherit a copy of the signal mask. */
sigemptyset(&set);
sigaddset(&set, SIGQUIT);
sigaddset(&set, SIGUSR1);
s = pthread_sigmask(SIG_BLOCK, &set, NULL);
if (s != 0)
handle_error_en(s, "pthread_sigmask");
s = pthread_create(&thread, NULL, &sig_thread, (void *) &set);
if (s != 0)
handle_error_en(s, "pthread_create");
/* Main thread carries on to create other threads and/or do
other work */
pause(); /* Dummy pause so we can test program */
}
这篇关于哪个线程处理信号?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!