开发者

How to handle SIGABRT signal in unix

I am getting core d开发者_运维百科ump while running below program:

$ cat test2.c

#include <stdio.h>
#include <stdlib.h>


void main()
{

abort();

}

$

$ cc -o test2 test2.c
"test2.c", line 5: warning #2951-D: return type of function "main" must be
          "int"
  void main()
       ^

$ ./test2
Abort(coredump)
$

I have received a SIGABRT signal. Kindly suggest me the ways to handle this SIGABRT signal.


remove abort() from your main... if you want to leave main: return; if you want to leave the program in any place: exit()

if you really want to handle the signal, install a signal handler see: http://www.manpagez.com/man/2/sigaction/

hth

Mario


You normally should not handle it, the purpose of calling abort() is to produce a core dump and terminate your program, just as your program does.


// here's same code w/signal handler
$ cat test.c
#include <stdio.h>
#include <stdlib.h>
#include <signal.h>

void abort_handler(int);

void main()
{
    if (signal(SIGABRT, abort_handler) == SIG_ERR) {
        fprintf(stderr, "Couldn't set signal handler\n");
        exit(1);
    }
    abort();
    exit(0);
}

void abort_handler(int i)
{
    fprintf(stderr, "Caught SIGABRT, exiting application\n");
    exit(1);
}
$ cc -o test test.c
$ ./test
Caught SIGABRT, exiting application
$ 
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜