#include <iostream>
#include <signal.h>
#include <unistd.h>
#include <sys/wait.h>
#include <cstdlib>
#include <sys/types.h>
#include <string>
#include <functional>
#include <vector>
using namespace std;
typedef function<void ()> func;
vector<func> callbacks;
uint64_t count = 0;
void showCount()
{
//cout<< "进程捕捉到了一个信号,正在处理中: "<< signum << " pid: " << getpid() <<endl;
cout << "final cout :" << count << endl;
}
void catchSig(int signum)
{
for(auto &f : callbacks)
{
f();
}
alarm(1);
}
//typedef void (*catchSig) (int);
int main(int argc,char* argv[])
{
alarm(1);
signal(SIGALRM,catchSig);
callbacks.push_back(showCount);
while(true) count++;
}
运行结果: