signal

Standard C Library (libc, -lc)

C++ 信号处理库提供了 signal 函数,用来捕获突发事件。以下是 signal() 函数的语法:

#include <signal.h>

void (*signal(int sig, void (*func)(int)))(int);

这个函数接收两个参数:第一个参数是一个整数,代表了信号的编号;第二个参数是一个指向信号处理函数的指针。

让我们编写一个简单的 C++ 程序,使用 signal() 函数捕获 SIGINT 信号。不管您想在程序中捕获什么信号,您都必须使用 signal 函数来注册信号,并将其与信号处理程序相关联。看看下面的实例:

#include <iostream>
#include <csignal>
#include <unistd.h>

void handler(int sig){
    std::cout<<"signal:"<<sig<<std::endl;
}
int main(int argc, char const *argv[])
{
    // 注册信号 SIGINT 和信号处理程序
    signal(SIGINT,handler);
    while(1) {
        sleep(1);
        std::cout << "sleep 1 second" << std::endl;
    }
    return 0;
}

可以看到ctrl+c会输出signal 2,主程序是个死循环(疯狂输出sleep 1 second),永远不会退出,即使狂按ctrl+c

signal 2

改善代码

#include <iostream>
#include <csignal>
#include <unistd.h>

void signal_handler(int signum){
    std::cout<<"signal:"<<signum<<std::endl;
     // 清理并关闭
    // 终止程序 
    exit(signum);
}

int main(int argc, char const *argv[])
{
    // 注册信号 SIGINT 和信号处理程序
    signal(SIGINT,signal_handler);
    while(1) {
        sleep(1);
        std::cout << "sleep 1 second" << std::endl;
    }
    return 0;
}

这下ctrl+c就输出正常

sleep

Sleep 函数

功能:执行挂起一段时间,也就是等待一段时间在继续执行

用法:Sleep(时间)

注意:

(1)Sleep是区分大小写的,有的编译器是大写,有的是小写。 (2)Sleep括号里的时间,在windows下是已毫秒为单位,而LInux是已秒为单位。

小知识点: Linux 用 #include <unistd.h>sleep(),Windos 用 #include <windows.h>Sleep()

常见signal

man signal
     No    Name         Default Action       Description
     1     SIGHUP       terminate process    terminal line hangup
     2     SIGINT       terminate process    interrupt program
     3     SIGQUIT      create core image    quit program
     4     SIGILL       create core image    illegal instruction
     5     SIGTRAP      create core image    trace trap
     6     SIGABRT      create core image    abort program (formerly SIGIOT)
     7     SIGEMT       create core image    emulate instruction executed
     8     SIGFPE       create core image    floating-point exception
     9     SIGKILL      terminate process    kill program
     10    SIGBUS       create core image    bus error
     11    SIGSEGV      create core image    segmentation violation
     12    SIGSYS       create core image    non-existent system call invoked
     13    SIGPIPE      terminate process    write on a pipe with no reader
     14    SIGALRM      terminate process    real-time timer expired
     15    SIGTERM      terminate process    software termination signal
     16    SIGURG       discard signal       urgent condition present on
     17    SIGSTOP      stop process         stop (cannot be caught or
     18    SIGTSTP      stop process         stop signal generated from
     19    SIGCONT      discard signal       continue after stop
     20    SIGCHLD      discard signal       child status has changed
     21    SIGTTIN      stop process         background read attempted from
     22    SIGTTOU      stop process         background write attempted to
     25    SIGXFSZ      terminate process    file size limit exceeded (see
     26    SIGVTALRM    terminate process    virtual time alarm (see
     27    SIGPROF      terminate process    profiling timer alarm (see
     28    SIGWINCH     discard signal       Window size change
     29    SIGINFO      discard signal       status request from keyboard
     30    SIGUSR1      terminate process    User defined signal 1
     31    SIGUSR2      terminate process    User defined signal 2

results matching ""

    No results matching ""