Chinaunix首页 | 论坛 | 博客
  • 博客访问: 157487
  • 博文数量: 76
  • 博客积分: 1513
  • 博客等级: 上尉
  • 技术积分: 755
  • 用 户 组: 普通用户
  • 注册时间: 2011-11-25 15:15
文章分类

全部博文(76)

文章存档

2012年(2)

2011年(74)

我的朋友

分类: LINUX

2011-11-25 20:53:41

  1. #include<iostream>

  2. #include<signal.h>

  3. #include<stdlib.h>

  4. #include<unistd.h>

  5. using namespace std;

  6. // Linux下的signal函数: (void)signal(信号类型,调用函数)

  7. //signal里面调用函数: void 函数名(int sig){ }

  8. void fun(int sig)

  9. {

  10. cout<<"the terminal will exit in 5 secs!"<<endl;

  11. sleep(5);

  12. (void) signal(SIGINT,SIG_DFL);//再次输入中断命令CTRL+C,则执行SIGINT默认的行为即终端中断

  13. }

  14. int main()

  15. {

  16. (void) signal(SIGINT,fun);//信号处理函数收到CTRL+C处理函数,则执行fun()函数

  17. while(1)

  18. {

  19. cout<<"hello"<<endl;

  20. sleep(1);

  21. }

  22. }

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 ~]$ 

阅读(591) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~