在多线程进程中处理信号的示例

Example of handling signals in multi-threaded process

本文关键字:信号 处理 多线程 进程      更新时间:2023-10-16

有人能给我以下情况的步骤甚至代码吗:

一个包含多个线程的进程,这些线程负责捕获用户定义的信号SIGUSR1。只有这个线程才能接收这个信号,在接收到这个信号后,我会做一些事情。

在我的情况下,信号由内核模块发送到我的进程ID。然后,我的进程有责任将其传递到正确的侦听线程,该线程也建立了信号处理程序,即信号处理程序不在主线程中。

我已经做了一些为单线程进程运行的代码,但在多线程环境中运行它时遇到了问题。

我在Linux Ubuntu 12.04.3内核版本3.8.0-29上运行我的代码。对于进程的创建,我将在Boost线程和POSIX线程API之间进行混合。

#include <unistd.h>
#include <signal.h>
#include <stdio.h>
#include <malloc.h>
#include <stdlib.h>
#include <errno.h>
#include <sys/mman.h>
#include <string.h>
/* Value of the last signal caught */
volatile sig_atomic_t sig_value;
static void sig_handler(const int sig_number, siginfo_t *sig_info, void *context)
{
if (sig_number == SIGSEGV)
{
    error_sys("Error at address 0x%lx", (long)sig_info->si_addr);
    exit(-1);
}
sig_value = sig_number;
}

int init_signal_catcher()
{
struct sigaction sig_action; /* Structure describing the action to be taken when asignal arrives.  */
sigset_t oldmask;  /* Signal mask before signal disposition change.      */
sigset_t newmask;  /* Signal mask after signal disposition change.       */
sigset_t zeromask; /* Signal mask to unblock all signal while suspended. */
/* Define signal mask and install signal handlers */
memset(&sig_action, 0, sizeof(struct sigaction));
sig_action.sa_flags = SA_SIGINFO;
sig_action.sa_sigaction = sig_handler;
/* Examine and change a signal action. */
sigaction(SIGHUP, &sig_action, NULL);
sigaction(SIGINT, &sig_action, NULL);
sigaction(SIGTERM, &sig_action, NULL);
sigaction(SIGSEGV, &sig_action, NULL);
sigaction(SIGUSR1, &sig_action, NULL);
    /* Block SIGHUP, SIGINT, SIGTERM, SIGSEGV and SIGUSR1 signals. */
sigemptyset(&newmask);
sigaddset(&newmask, SIGHUP);
sigaddset(&newmask, SIGINT);
sigaddset(&newmask, SIGTERM);
sigaddset(&newmask, SIGSEGV);
sigaddset(&newmask, SIGUSR1);
/* Examine and change blocked signals. */
pthread_sigmask(SIG_BLOCK, &newmask, &oldmask);
/* Initialize the empty signal set. */
sigemptyset(&zeromask);
sig_value = 0;
while ((sig_value != SIGINT) && (sig_value != SIGTERM))
{
    sig_value = 0;
    /*
     * Go to sleep (unblocking all signals) until a signal is catched.
     * On return from sleep, the signals SIGHUP, SIGINT, SIGTERM and
         * SIGUSR1 are again blocked.
     */
        printf("Suspending on %lu mask.", zeromask);
        // Wait for a signal.
    sigsuspend(&zeromask);
    switch(sig_value)
    {
                printf("Caught Signal %d", sig_value);
        case SIGUSR1:
                    printf("Caught SIGUSR1");
                    break;
    }
}
return 0;
}

信号需要在每个线程中被阻塞。最安全的方法是在创建其他线程之前,在第一个线程中阻止它们。然后,一个专门选择的线程可以调用sigsuspend(),并且只有该线程才能执行信号处理程序。

void *signal_handling_thread(void *whatever) {
  sig_value := 0
  while (sig_value not in (SIGTERM, SIGINT)) {
    sigsuspend(empty_mask)
    ...
  }
  ...
}
int main(int argc, char **argv) {
  block_relevant_signals();               // SIG_BLOCK HUP, TERM, USR1, etc.
  catch_relevant_signals();               // SA_SIGINFO ...
  spawn_signal_handling_thread();         // spawned with relevant signals blocked
  for (int i = 0; i < NUM_WORKERS; i++) {
    spawn_worker_thread();                // spawned with relevant signals blocked
  }
  ...
}

现在是时候重构代码来分解问题了——在一个地方进行全局流程属性操作,在另一个地方发出特定信号的反应,等等。

在信号处理程序中,您正在调用exit(-1)。exit(-1)不是异步信号处理程序安全的。请改用_exit(-1)。

这两个函数的区别在于exit()调用所有注册的atexit()例程(包括C++静态析构函数)。在exit()执行关闭步骤之前,它使用pthread_mutex_lock()来确保线程安全关闭。如果锁恰好被另一个线程持有,则程序将死锁。

_exit()跳过那些atexit例程并终止进程。

我不熟悉error_sys(),但看起来它最终使用了printf()/fprintf(。这些例程也往往受到互斥锁的保护。

下面是一个使用pthread_sigmask组织哪个线程获得哪个信号的示例:http://man7.org/linux/man-pages/man3/pthread_sigmask.3.html