【问题标题】:Which thread handles the signal?哪个线程处理信号?
【发布时间】:2014-03-27 04:12:55
【问题描述】:

我有 2 个线程(线程 1 和线程 2)。我对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);
}

我编译并运行了程序。每 1 秒“thread1 active”正在打印,每 3 秒“thread2 active”正在打印。

现在我生成了SIGINT。但它的打印“thread1 active”和“thread2 active”消息如上所示。我再次生成了SIGINT,现在每 3 秒只打印一次“thread2 active”消息。我再次生成了SIGINT,现在所有线程都被阻塞了。

所以我明白了,第一次主线程执行信号处理程序。第二次thread1执行handler,最后thread2执行signal handler。

我如何编写代码,例如每当信号发生时,只有 thread2 必须执行我的信号处理程序?

【问题讨论】:

  • printf 不是异步安全库调用,这意味着它不能由信号处理程序调用...如果是,则行为未指定(可能会发生坏事)。就此而言,getchar 也不是异步安全的。

标签: c linux multithreading pthreads signals


【解决方案1】:

如果你向一个进程发送一个信号,进程中的哪个线程将处理这个信号是不确定的。

根据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

节目来源

#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 */
}

【讨论】:

  • 所以你想说,我们无法改变他们的行为
  • @SGG 把什么行为改成什么?
  • 好吧,您可以sigprocmaskpthread_sigmask 发出信号(进程范围),然后sigwait() 并在您选择的线程中处理。
  • 我想如果信号 2 发生,线程 2 应该小心。如果信号 3 发生 thread1 应该小心。我不想随机线程无法执行我的特定信号
  • @SGG 您可以阻止除线程中允许该信号的特定信号之外的所有信号。
【解决方案2】:

仔细阅读signal(7) & pthread(7) & pthread_kill(3) & sigprocmask(2) & pthread_sigmask(3) - 您可以使用它们(在不需要的线程中阻止SIGINT)。另请阅读pthread tutorial

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

如果其中一个线程运行 event loop(例如在 poll(2)... 附近),请考虑使用 signalfd(2)

【讨论】:

  • 您的答案有一半是指向其他地方描述的链接。请总结您第一段中的链接以及它们如何在此处解决问题。
猜你喜欢
  • 2014-08-29
  • 2013-12-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-30
  • 1970-01-01
相关资源
最近更新 更多