哪个线程处理信号?

gan*_*ars 11 c linux multithreading signals pthreads

我有2个线程(thread1和thread2).我有信号处理SIGINT.每当SIGINT发生线程2应该处理信号时.为此,我写了下面的程序

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);
}
Run Code Online (Sandbox Code Playgroud)

我编译并运行该程序.每1秒"thread1 active"正在打印,并且每3秒"thread2 active"正在打印.

现在我生成了SIGINT.但它上面打印"thread1 active"和"thread2 active"消息.我再次生成SIGINT,现在每3秒只有"thread2 active"消息正在打印.我再次生成SIGINT,现在所有线程都被阻止了.

所以我理解,第一次主线程执行信号处理程序.第二次thread1执行处理程序,最后thread2执行信号处理程序.

我怎样才能编写代码,就像信号发生时一样,只有thread2必须执行我的信号处理程序?

Lee*_*hem 18

如果向进程发送信号,则进程中的哪个线程将处理此信号未确定.

根据pthread(7):

POSIX.1还要求线程共享一系列其他属性(即,这些属性是进程范围而不是每个线程):
...
- 信号处置
...

POSIX.1区分了作为整体指向进程的信号的概念和指向各个线程的信号.根据POSIX.1,过程导向信号(kill(2)例如使用发送)应该由过程中任意选择的单个线程处理.


如果你想在你的过程中有一个专用线程来处理一些信号,这里有一个例子来pthread_sigmask(3)说明你如何做到这一点:

下面的程序阻塞主线程中的一些信号,然后创建一个专用线程来通过sigwait获取这些信号(3).以下shell会话演示了它的用法:

$ ./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
Run Code Online (Sandbox Code Playgroud)

节目来源

#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 */
}
Run Code Online (Sandbox Code Playgroud)


Bas*_*tch 6

仔细阅读signal(7)&pthread(7)&pthread_kill(3) & sigprocmask(2)&pthread_sigmask(3) - 你可以使用(阻止SIGINT不需要的线程).另请阅读pthread教程.

避免使用信号在线程之间进行通信或同步.考虑例如互斥(pthread_mutex_lock等...)和条件变量(pthread_cond_wait等...).

如果其中一个线程运行事件循环(例如,绕轮询(2) ......),请考虑使用signalfd(2).

  • 您的一半答案是指向其他地方描述的链接。请总结您第一段中的链接以及它们如何在此处解决问题。 (4认同)