cxx signal信号捕获

kill -9 [pid] 该信号不能被捕获
#include <iostream>
#include <csignal>

static void vSignalHandler(int signum) {
    std::cout<<__FUNCTION__<<":"<<signum<<std::endl;
    //start
    
    //stop
    exit(signum);
}

static void vSignalRegister() {

    //SIGINT:ctrl+c
    //auto res = signal(SIGINT,vSignalHandler);
    
    //SIGTERM:kill [pid]
    auto res = signal(SIGTERM,vSignalHandler);
    
    if(res == SIG_ERR) {
        std::cout<<__FUNCTION__<<":false"<<std::endl;
    }
    else {
        std::cout<<__FUNCTION__<<":true"<<std::endl;
    }
}
posted @ 2019-07-12 20:09  JohnRed  阅读(172)  评论(0编辑  收藏  举报