2011-05-03 51 views
0

我得到核心转储以下运行程序时SIGABRT信号:如何处理在UNIX

$ 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) 
$ 

我收到一个SIGABRT信号。请建议我处理这个SIGABRT信号的方法。

+0

http://en.wikipedia.org/wiki/SIGABRT – 2011-05-03 09:46:01

回答

3

从主删除abort() ... 如果你要离开主:如果你想在任何地方离开程序return;exit()

如果你真的要处理的信号,安装信号处理器 见:http://www.manpagez.com/man/2/sigaction/

心连心

马里奥

3

ÿ OU通常不应当予以受理,呼吁中止()的目的是产生一个核心转储和终止程序,就像你的程序一样。

2
// 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

要知道,你可以处理SIGABRT但你不能从终端(及倾卸)停止进程 – 2011-05-03 11:07:54

+1

我不知道到什么程度该声明是系统相关的,在我编译并运行上述代码的系统上,没有核心被倾倒。
$ UNAME -a Linux的NAS 2.6.32-30-通用#59,Ubuntu的SMP周二3月1日21点30分46秒UTC 2011 x86_64的GNU/Linux的 见人信号,以及人7信号 – bsd 2011-05-03 16:18:04