C ++中的基本信号处理 [英] Basic signal handling in C++

查看:59
本文介绍了C ++中的基本信号处理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一个非常基本的场景,但是我找不到太多有用的资源.我有一个运行Linux的C ++程序来执行文件处理.读取行,进行各种转换,将数据写入数据库.有一些变量(存储在数据库中)会影响我当前在每次迭代中读取的处理,因为我希望处理尽可能地是最新的,但略有延迟是可以的.但是这些变量很少发生变化,并且随着时间的推移读取成本很高(每天1000万行以上).我可以将读取空间间隔到每个 n 个迭代中,或者在变量发生变化时简单地重新启动程序,但是这些操作似乎有些黑.

我想做的是让程序在收到SIGHUP时触发对变量的重新读取.我正在阅读的有关信号处理的所有内容都在谈论C信号库,但我不确定如何将其绑定到程序的类中.Boost信号库似乎更多地是关于对象间的通信,而不是处理OS信号.

有人可以帮忙吗?看来这应该非常简单,但是我对C ++还是很生疏.

解决方案

我将像处理C语言一样处理它.我认为拥有独立的信号处理程序功能是完全可以的,因为张贴到信号量或设置变量或其他变量,其他线程或对象可以检查该变量以确定是否需要重新读取设置.

  #include< signal.h>#include< stdio.h>/*,或者您可以使用信号量来通知正在等待的线程*/静态易失性sig_atomic_t sig_caught = 0;无效handle_sighup(int signum){/*如果我们为多个信号注册了该处理程序,则*/if(signum == SIGHUP){sig_caught = 1;}}int main(int argc,char * argv []){/*您也可能更喜欢sigaction()而不是signal()*/信号(SIGHUP,handle_sighup);while(1){如果(sig_caught){sig_caught = 0;printf(抓到了一个SIGHUP.我应该重新读取设置.\ n");}}返回0;} 

您可以使用 kill -1`pidof yourapp` 测试发送 SIGHUP .

This is a pretty basic scenario but I'm not finding too many helpful resources. I have a C++ program running in Linux that does file processing. Reads lines, does various transformations, writes data into a database. There's certain variables (stored in the database) that affect the processing which I'm currently reading at every iteration because I want processing to be as up to date as possible, but a slight lag is OK. But those variables change pretty rarely, and the reads are expensive over time (10 million plus rows a day). I could space out the reads to every n iterations or simply restart the program when a variable changes, but those seem hackish.

What I would like to do instead is have the program trigger a reread of the variables when it receives a SIGHUP. Everything I'm reading about signal handling is talking about the C signal library which I'm not sure how to tie in to my program's classes. The Boost signal libraries seem to be more about inter-object communication rather than handling OS signals.

Can anybody help? It seems like this should be incredibly simple, but I'm pretty rusty with C++.

解决方案

I would handle it just like you might handle it in C. I think it's perfectly fine to have a stand-alone signal handler function, since you'll just be posting to a semaphore or setting a variable or some such, which another thread or object can inspect to determine if it needs to re-read the settings.

#include <signal.h>
#include <stdio.h>

/* or you might use a semaphore to notify a waiting thread */
static volatile sig_atomic_t sig_caught = 0;

void handle_sighup(int signum) 
{
    /* in case we registered this handler for multiple signals */ 
    if (signum == SIGHUP) {
        sig_caught = 1;
    }
}

int main(int argc, char* argv[]) 
{
    /* you may also prefer sigaction() instead of signal() */
    signal(SIGHUP, handle_sighup);

    while(1) {
        if (sig_caught) {
            sig_caught = 0;
            printf("caught a SIGHUP.  I should re-read settings.\n");
        }
    }

    return 0;
}

You can test sending a SIGHUP by using kill -1 `pidof yourapp`.

这篇关于C ++中的基本信号处理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆