IoT*_*IoT 5 c++ linux multithreading signals boost-thread
任何人都可以给我以下情况的步骤甚至代码:
包含多个线程的进程以及这些线程负责捕获用户定义的信号SIGUSR1.只有这个线程应该能够接收到这个信号,并且在接收到这个信号后我会做一些事情.
在我的情况下,内核模块将信号发送到我的进程ID.然后我的过程负责将它传递给正确的监听线程,该线程还建立了信号处理程序,即信号处理程序不在主线程中.
我已经为一个单线程进程运行了一些代码,但是在多线程环境中运行它时遇到了问题.
我在Linux Ubuntu 12.04.3上使用内核版本3.8.0-29运行我的代码.为了创建流程,我将在Boost Threads和POSIX threads 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;
}
Run Code Online (Sandbox Code Playgroud)
需要在每个线程中阻止信号。最安全的方法是在创建任何其他线程之前在第一个线程中阻止它们。然后一个专门选择的线程可以调用sigsuspend(),并且只有该线程将执行信号处理程序。
void *signal_handling_thread(void *whatever) {\n sig_value := 0\n while (sig_value not in (SIGTERM, SIGINT)) {\n sigsuspend(empty_mask)\n ...\n }\n ...\n}\n\nint main(int argc, char **argv) {\n block_relevant_signals(); // SIG_BLOCK HUP, TERM, USR1, etc.\n catch_relevant_signals(); // SA_SIGINFO ...\n\n spawn_signal_handling_thread(); // spawned with relevant signals blocked\n\n for (int i = 0; i < NUM_WORKERS; i++) {\n spawn_worker_thread(); // spawned with relevant signals blocked\n }\n ...\n}\nRun Code Online (Sandbox Code Playgroud)\n\n是时候重构您的代码来分解关注点 \xe2\x80\x94 在一个地方进行全局进程属性操作,在另一个地方进行特定于信号的反应,等等。
\n| 归档时间: |
|
| 查看次数: |
9052 次 |
| 最近记录: |