Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1741594
  • 博文数量: 297
  • 博客积分: 285
  • 博客等级: 二等列兵
  • 技术积分: 3006
  • 用 户 组: 普通用户
  • 注册时间: 2010-03-06 22:04
个人简介

Linuxer, ex IBMer. GNU https://hmchzb19.github.io/

文章分类

全部博文(297)

文章存档

2020年(11)

2019年(15)

2018年(43)

2017年(79)

2016年(79)

2015年(58)

2014年(1)

2013年(8)

2012年(3)

分类: LINUX

2015-10-04 16:57:09

为了懒省事,我用了apue.h

点击(此处)折叠或打开

  1. #include <apue.h>

  2. void diediedie(int sig)
  3. {
  4.     fputs("Goodby curel word......\n",stdout);
  5.     exit(1);
  6.     }

  7. int catch_signal(int sig,void(*handler)(int))
  8. {
  9.     struct sigaction action;
  10.     action.sa_handler=handler;
  11.     sigemptyset(&action.sa_mask);
  12.     action.sa_flags=0;
  13.     return sigaction(sig,&action,NULL);
  14.     }

  15. int main()
  16. {
  17.     if(catch_signal(SIGINT,diediedie)==-1)
  18.     {
  19.         fputs("Can't map the handler",stderr);
  20.         exit(2);
  21.         }
  22.     char name[30];
  23.     printf("Enter your name:");
  24.     fgets(name,30,stdin);
  25.     printf("Hello %s\n",name);
  26.     if(catch_signal(SIGINT,SIG_DFL)==-1)
  27.     {
  28.         fputs("Can't not restore the handler to default\n",stderr);
  29.         exit(2);
  30.         }
  31.     printf("Enter your name:");
  32.     fgets(name,30,stdin);
  33.     printf("Hello %s\n",name);


  34.     return 0;
  35.     }
下面是一些SIGNAL,可以用kill -l 查看signal.

点击(此处)折叠或打开

  1. SIGINT: The process was interrupted
  2. SIGQUIT: Someone asked the process to stop and dump the memory in a core dump file
  3. SIGFPE: Floating-point error
  4. SIGTRAP: The debugger asks where the process is
  5. SIGSEGV: The process try to access illegal memory
  6. SIGWINCH: The terminal window changed size
  7. SIGTERM: Someone just asked the kernel to kill the process.
  8. SIGPIPE: The process wrote to a pipe that nothing

点击(此处)折叠或打开

  1. #raise() send a signal to the caller
  2. catch_signal(SIGTERM,SIG_DEFL);        #handle SIGTERM in the default way
  3. catch_signal(SIGINT,SIG_IGN);          #ignore the SIGINT signal
  4. #这两个常量是定义在signal.h

这个哥们的signal系列写的不错:
http://blog.chinaunix.net/uid-24774106-id-4064447.html

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