- #include<iostream>
-
-
#include<signal.h>
-
-
#include<stdlib.h>
-
-
#include<unistd.h>
-
-
using namespace std;
-
-
// Linux下的signal函数: (void)signal(信号类型,调用函数)
-
-
//signal里面调用函数: void 函数名(int sig){ }
-
-
void fun(int sig)
-
-
{
-
-
cout<<"the terminal will exit in 5 secs!"<<endl;
-
-
sleep(5);
-
-
(void) signal(SIGINT,SIG_DFL);//再次输入中断命令CTRL+C,则执行SIGINT默认的行为即终端中断
-
-
}
-
-
int main()
-
-
{
-
-
(void) signal(SIGINT,fun);//信号处理函数收到CTRL+C处理函数,则执行fun()函数
-
-
while(1)
-
-
{
-
-
cout<<"hello"<<endl;
-
-
sleep(1);
-
-
}
-
-
}
output:
[xq@m1 ~]$ g++ -o signal_test signal_test.cpp
[xq@m1 ~]$ ./signal_test
hello
hello
hello
hello
the terminal will exit in 5 secs!
hello
hello
hello
hello
hello
[xq@m1 ~]$
阅读(617) | 评论(0) | 转发(0) |