2011-05-03 54 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