abort()库函数的实现

根据abort()在manpage中的描述:

The  abort()  first  unblocks  the SIGABRT signal, and then raises that signal for the calling process.  This results in the abnormal  termination  of the process unless the SIGABRT signal is caught and the signal handler does not return (see longjmp(3)).

If the abort() function causes process termination,  all  open  streams are closed and flushed.

If  the SIGABRT signal is ignored, or caught by a handler that returns, the abort() function will still terminate the process.  It does this by restoring the default disposition for SIGABRT and then raising the signal for a second time.

#include <stdio.h>
#include <signal.h>
#include <setjmp.h>
#include "utils.h"
#define USE_SIG_LONG_JMP
static sigjmp_buf senv;
void handler(int sig)
{
    printf("In the handler.\n");
#ifdef USE_SIG_LONG_JMP
    siglongjmp(senv, 1);
    printf("Never see this.\n");
#endif
}
void m_abort(void)
{
    sigset_t set;
    sigemptyset(&set);
    sigaddset(&set, SIGABRT);
    sigprocmask(SIG_UNBLOCK, &set, NULL);
    printf("m_abort: raise first SIGABRT.\n");
    raise(SIGABRT);
    struct sigaction sa;
    sigemptyset(&sa.sa_mask);
    sa.sa_flags = SA_RESETHAND;
    sa.sa_handler = SIG_DFL;
    sigaction(SIGABRT, &sa, NULL);
    printf("m_abort: raise second SIGABRT.\n");
    raise(SIGABRT);
}
int main(void)
{
    printf("Now call the abort.\n");
    struct sigaction sa;
    sigemptyset(&sa.sa_mask);
    sa.sa_flags = 0;
    sa.sa_handler = handler;
    sigaction(SIGABRT, &sa, NULL);
#ifdef USE_SIG_LONG_JMP
    if (sigsetjmp(senv, 1) == 0)
        m_abort();
    else
        printf("abort failed.\n");
#else
        m_abort();
        printf("abort failed.\n");
#endif
    return 0;
}
时间: 2024-07-29 07:45:32

abort()库函数的实现的相关文章

APUE: 进程相关的系统调用和库函数

进程正常终止5种方式: 1.main函数返回 2.调用exit库函数 3.调用_exit或_Exit系统调用 4.最后一个线程从其启动例程返回 5.最后一个线程调用pthread_exit库函数 进程异常终止3种方式: 1.调用abort库函数,产生abort信号. 2.接到一个信号并终止 3.最后一个线程对取消请求做出响应 init进程:pid=1的进程,如果父进程先于子进程终止,子进程就被init进程收养. 孤儿进程:父进程先于子进程退出,子进程被init进程收养,这个子进程就是孤儿进程.

APUE: 信号相关系统调用和库函数

信号就是软件中断,信号提供一种处理异步事件的方法. 信号出现时按照下列方式处理: 1.忽略此信号,有两个信号不能忽略. 2.捕捉此信号,有两个信号不能被捕捉. 3.默认处理,少数默认处理是忽略,大部分默认处理是终止. ctrl+D组合键,不是信号,只是EOF字符 linux中1-31为普通信号:34-64为实时信号. trap-l 命令查看所有信号,64个 信号从1开始,没有0. SIGHUP:终端接口检测到连接断开发出该信号, SIGINT:ctrl+c,终端中断符,一般用来停止一个失去控制的

外传篇1 异常处理深度解析

1. main函数中抛异常 [编程实验]异常的最终处理 #include <iostream> using namespace std; class Test { public: Test(){ cout << "Test()" << endl; } ~Test(){ cout << "~Test()" << endl; } }; int main() { static Test t; throw 1; r

C++语言学习(十八)——异常处理

C++语言学习(十八)--异常处理 一.C语言异常处理 异常是指程序在运行过程中产生可预料的执行分支.如除0操作,数组访问越界.要打开的文件不存在.Bug是指程序中的错误,是不被预期的运行方式.如野指针.堆空间使用结束未释放.C语言中处理异常的方式一般是使用if....else...分支语句. double divide(double a, double b) { const double delta = 0.000000000000001; double ret = 0; if( !((-de

C语言中最常用标准库函数

标准头文件包括: <asset.h>      <ctype.h>       <errno.h>       <float.h> <limits.h>      <locale.h>       <math.h>        <setjmp.h> <signal.h>     <stdarg.h>      <stddef.h>      <stdlib.h>

C语言库函数大全及应用实例七

原文:C语言库函数大全及应用实例七 [编程资料]C语言库函数大全及应用实例七 函数名: getw 功 能: 从流中取一整数 用 法: int getw(FILE *strem); 程序例: #i nclude #i nclude #define FNAME "test.$$$" int main(void) { FILE *fp; int word; /* place the word in a file */ fp = fopen(FNAME, "wb"); if

C++常用库函数

C++常用库函数  转自:http://blog.csdn.net/sai19841003/article/details/7957115 1.常用数学函数 头文件 #include <math> 或者 #include <math.h>   函数原型 功能 返回值 int abs(int x) 求整数x的绝对值 绝对值 double acos(double x) 计算arcos(x)的值 计算结果 double asin(double x) 计算arsin(x)的值 计算结果 d

C++库函数大全

C++常用库函数   1.常用数学函数 头文件 #include <math> 或者 #include <math.h>   函数原型 功能 返回值 int abs(int x) 求整数x的绝对值 绝对值 double acos(double x) 计算arcos(x)的值 计算结果 double asin(double x) 计算arsin(x)的值 计算结果 double atan(double x) 计算arctan(x)的值 计算结果 double cos(double x

C语言库函数大全及应用实例二

原文:C语言库函数大全及应用实例二                                              [编程资料]C语言库函数大全及应用实例二 函数名: bioskey 功 能: 直接使用BIOS服务的键盘接口 用 法: int bioskey(int cmd); 程序例: #i nclude #i nclude #i nclude #define RIGHT 0x01 #define LEFT 0x02 #define CTRL 0x04 #define ALT 0x0