Chinaunix首页 | 论坛 | 博客
  • 博客访问: 559090
  • 博文数量: 142
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 1452
  • 用 户 组: 普通用户
  • 注册时间: 2013-09-12 16:28
文章分类

全部博文(142)

文章存档

2016年(10)

2015年(60)

2014年(72)

我的朋友

分类: C/C++

2014-09-01 16:31:09

原型:
#include
void abort(void);

描述:
1.abort()函数首先不阻塞SIGABRT信号,然后raise一个信号给调用进程。
  除非SIGABRT信号被捕获并且信号处理函数不返回,否则abort()函数导致进程异常终止
2.如果abort函数导致一个进程终止,所有打开的流文件都关闭并且刷新
3.如果SIGABRT信号被忽略或者被要返回的处理函数获取,那么,abort仍将终止进程。
  它是这样做的:恢复SIGABRT的默认处理并且再次发送一个SIGABRT信号。

APUE里面的实现如下:

点击(此处)折叠或打开

  1. void abort(void)
  2. {
  3.     sigset_t sig_set;
  4.     struct sigaction sigact;

  5.     sigemptyset(&sig_set);

  6.     sigact.sa_mask = sig_set;
  7.     sigact.sa_flags = 0;

  8.     /*Caller can't ignore SIGABRT, if so reset to SIGDFL*/
  9.     sigaction(SIGABRT,NULL,&sigact);
  10.     if(sigact.sa_handler == SIG_IGN) {
  11.         sigact.sa_handler = SIG_DFL;
  12.         sigaction(SIGABRT,&sigact,NULL);
  13.     }

  14.     if(sigact.sa_handler == SIG_DFL)
  15.         fflush(NULL);
  16.     
  17.     /*caller can't block SIGABRT*/    /*1.abort()函数首先不阻塞SIGABRT信号,然后raise(kill(self_pid)一个信号给调用进程*/
  18.     sigfillset(&sig_set);
  19.     sigdelset(&sig_set,SIGABRT);
  20.     sigprocmask(SIG_SETMASK,&sig_set,NULL);
  21.     kill(getpid(),SIGABRT);

  22.     /*If we're here, process caught SIGABRT and returned*/ /*3.如果SIGABRT信号被忽略或者被要返回的处理函数获取,那么,abort仍将终止进程。
      它是这样做的:恢复SIGABRT的默认处理并且再次发送一个SIGABRT信号。*/

  23.     fflush(NULL);
  24.     sigact.sa_handler = SIG_DFL;
  25.     sigaction(SIGABRT,&sigact,NULL);
  26.     sigprocmask(SIG_SETMASK,&sig_set,NULL);
  27.     kill(getpid(),SIGABRT);
  28.     exit(1);
  29. }
实例如下:
一、包含SIGABRT的信号处理函数,并且信号处理函数不返回的情况。

点击(此处)折叠或打开

  1. #include <signal.h>
  2. #include <unistd.h>
  3. #include <stdio.h>
  4. #include <stdlib.h>

  5. void abort(void)
  6. {
  7.     sigset_t sig_set;
  8.     struct sigaction sigact;

  9.     sigemptyset(&sig_set);

  10.     sigact.sa_mask = sig_set;
  11.     sigact.sa_flags = 0;

  12.     /*Caller can't ignore SIGABRT, if so reset to SIGDFL*/
  13.     sigaction(SIGABRT,NULL,&sigact);
  14.     if(sigact.sa_handler == SIG_IGN) {
  15.         sigact.sa_handler = SIG_DFL;
  16.         sigaction(SIGABRT,&sigact,NULL);
  17.     }

  18.     if(sigact.sa_handler == SIG_DFL)
  19.         fflush(NULL);
  20.     printf("----1\n");
  21.     /*caller can't block SIGABRT*/
  22.     sigfillset(&sig_set);

  23.     sigdelset(&sig_set,SIGABRT);

  24.     sigprocmask(SIG_SETMASK,&sig_set,NULL);
  25.     printf("----2\n");
  26.     kill(getpid(),SIGABRT);
  27.     printf("----3\n");

  28.     /*If we are here, process caught SIGABRT and returned*/
  29.     fflush(NULL);
  30.     sigact.sa_handler = SIG_DFL;
  31.     sigaction(SIGABRT,&sigact,NULL);
  32.     sigprocmask(SIG_SETMASK,&sig_set,NULL);
  33.     kill(getpid(),SIGABRT);
  34.     printf("----4\n");
  35.     exit(1);
  36. }

  37. static void sig_abort(int signo)
  38. {
  39.     if(signo == SIGABRT)
  40.         printf("recv signal SIGABRT\n");
  41.     exit(0);
  42. }

  43. int main(int argc, char* argv[])
  44. {
  45.             
  46.     signal(SIGABRT,&sig_abort);

  47.     abort();

  48.     return 0;
  49. }
编译运行:
gwwu@hz-dev2.wgw.com:~/test/signal>gcc -g abort.c -o a
gwwu@hz-dev2.wgw.com:~/test/signal>./a
----1
----2
recv signal SIGABRT

二、包含SIGABRT的信号处理函数,并且信号处理函数返回的情况(在信号处理函数中注释掉exit(0))。


点击(此处)折叠或打开

  1. #include <signal.h>
  2. #include <unistd.h>
  3. #include <stdio.h>
  4. #include <stdlib.h>

  5. void abort(void)
  6. {
  7.     sigset_t sig_set;
  8.     struct sigaction sigact;

  9.     sigemptyset(&sig_set);

  10.     sigact.sa_mask = sig_set;
  11.     sigact.sa_flags = 0;

  12.     /*Caller can't ignore SIGABRT, if so reset to SIGDFL*/
  13.     sigaction(SIGABRT,NULL,&sigact);
  14.     if(sigact.sa_handler == SIG_IGN) {
  15.         sigact.sa_handler = SIG_DFL;
  16.         sigaction(SIGABRT,&sigact,NULL);
  17.     }

  18.     if(sigact.sa_handler == SIG_DFL)
  19.         fflush(NULL);
  20.     printf("----1\n");
  21.     /*caller can't block SIGABRT*/
  22.     sigfillset(&sig_set);

  23.     sigdelset(&sig_set,SIGABRT);

  24.     sigprocmask(SIG_SETMASK,&sig_set,NULL);
  25.     printf("----2\n");
  26.     kill(getpid(),SIGABRT);
  27.     printf("----3\n");

  28.     /*If we are here, process caught SIGABRT and returned*/
  29.     fflush(NULL);
  30.     sigact.sa_handler = SIG_DFL;
  31.     sigaction(SIGABRT,&sigact,NULL);
  32.     sigprocmask(SIG_SETMASK,&sig_set,NULL);
  33.     kill(getpid(),SIGABRT);
  34.     printf("----4\n");
  35.     exit(1);
  36. }

  37. static void sig_abort(int signo)
  38. {
  39.     if(signo == SIGABRT)
  40.         printf("recv signal SIGABRT\n");
  41.     //exit(0);
  42. }

  43. int main(int argc, char* argv[])
  44. {
  45.             
  46.     signal(SIGABRT,&sig_abort);

  47.     abort();

  48.     return 0;
  49. }
编译运行:
gwwu@hz-dev2.wgw.com:~/test/signal>gcc -g abort.c -o a
gwwu@hz-dev2.wgw.com:~/test/signal>./a
----1
----2
recv signal SIGABRT
----3
Aborted (core dumped)
印证了第三条:
3.如果SIGABRT信号被忽略或者被要返回的处理函数获取,那么,abort仍将终止进程。
  它是这样做的:恢复SIGABRT的默认处理并且再次发送一个SIGABRT信号。

三、不包含SIGABRT的信号处理函数

点击(此处)折叠或打开

  1. #include <signal.h>
  2. #include <unistd.h>
  3. #include <stdio.h>
  4. #include <stdlib.h>

  5. void abort(void)
  6. {
  7.     sigset_t sig_set;
  8.     struct sigaction sigact;

  9.     sigemptyset(&sig_set);

  10.     sigact.sa_mask = sig_set;
  11.     sigact.sa_flags = 0;

  12.     /*Caller can't ignore SIGABRT, if so reset to SIGDFL*/
  13.     sigaction(SIGABRT,NULL,&sigact);
  14.     if(sigact.sa_handler == SIG_IGN) {
  15.         sigact.sa_handler = SIG_DFL;
  16.         sigaction(SIGABRT,&sigact,NULL);
  17.     }

  18.     if(sigact.sa_handler == SIG_DFL)
  19.         fflush(NULL);
  20.     printf("----1\n");
  21.     /*caller can't block SIGABRT*/
  22.     sigfillset(&sig_set);

  23.     sigdelset(&sig_set,SIGABRT);

  24.     sigprocmask(SIG_SETMASK,&sig_set,NULL);
  25.     printf("----2\n");
  26.     kill(getpid(),SIGABRT);
  27.     printf("----3\n");

  28.     /*If we are here, process caught SIGABRT and returned*/
  29.     fflush(NULL);
  30.     sigact.sa_handler = SIG_DFL;
  31.     sigaction(SIGABRT,&sigact,NULL);
  32.     sigprocmask(SIG_SETMASK,&sig_set,NULL);
  33.     kill(getpid(),SIGABRT);
  34.     printf("----4\n");
  35.     exit(1);
  36. }

  37. static void sig_abort(int signo)
  38. {
  39.     if(signo == SIGABRT)
  40.         printf("recv signal SIGABRT\n");
  41.     //exit(0);
  42. }

  43. int main(int argc, char* argv[])
  44. {
  45.             
  46.     //signal(SIGABRT,&sig_abort);

  47.     abort();

  48.     return 0;
  49. }
编译运行:
gwwu@hz-dev2.wgw.com:~/test/signal>./a
----1
----2
Aborted (core dumped)



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