首页 > 其他 > 详细

abort()库函数的实现

时间:2014-08-26 03:04:56      阅读:357      评论:0      收藏:0      [点我收藏+]

根据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;
}


abort()库函数的实现

原文:http://study0tlpi.blog.51cto.com/9307513/1544915

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!