Linux C catching kill signal for graceful termination

后端 未结 3 1382
無奈伤痛
無奈伤痛 2020-12-05 11:02

I have a process using sockets, database connections and the likes. It is basically a server process relaying between sensor data and a web interface, and as such it is impo

3条回答
  •  醉话见心
    2020-12-05 11:37

    I sometimes like to get a backtrace on SIGSEGV, the catching part goes like:

    #include 
    #include 
    #include 
    
    void sig_handler(int);
    
    int main() {
        signal(SIGSEGV, sig_handler);
        int *p = NULL;
        return *p;
    }
    
    void sig_handler(int sig) {
        switch (sig) {
        case SIGSEGV:
            fprintf(stderr, "give out a backtrace or something...\n");
            abort();
        default:
            fprintf(stderr, "wasn't expecting that!\n");
            abort();
        }
    }
    

    You do want to be very careful handling these things, e.g. make sure you can't trigger another signal.

提交回复
热议问题