Basic signal handling in C++

后端 未结 4 352
隐瞒了意图╮
隐瞒了意图╮ 2021-01-02 02:22

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 t

相关标签:
4条回答
  • 2021-01-02 03:03

    I'd recommend checking out this link which gives the details on registering a signal.

    Unless I'm mistaken, one important thing to remember is that any function inside an object expects a referent parameter, which means non-static member functions can't be signal handlers. I believe you'll need to register it either to a static member function, or some kind of global function. From there, if you have a specific object function you want to take care of your update, you'll need a way to reference that object.

    0 讨论(0)
  • 2021-01-02 03:04

    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`.

    0 讨论(0)
  • 2021-01-02 03:09

    You can define a Boost signal corresponding to the OS signal and tie the Boost signal to your slot to invoke the respective handler.

    0 讨论(0)
  • 2021-01-02 03:12

    There are several possibilities; it would not necessarily be overkill to implement all of them:

    • Respond to a specific signal, just like C does. C++ works the same way. See the documentation for signal().
    • Trigger on the modification timestamp of some file changing, like the database if it is stored in a flat file.
    • Trigger once per hour, or once per day (whatever makes sense).
    0 讨论(0)
提交回复
热议问题