C++通过sigaction捕捉信号

测试程序

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
// 通过sigaction捕捉信号

#include <iostream>

#include <signal.h>
#include <unistd.h>

void sigactionHandle(int sig) {
std::cout << "SIGNAL: " << sig << std::endl;
}

int main(int argc, char *argv[]) {
struct sigaction act, oact;

act.sa_handler = sigactionHandle;
sigfillset(&act.sa_mask);
act.sa_flags = 0;
sigaction(SIGINT, &act, &oact);

// 等待信号
pause();
}