【问题标题】:How to handle SIGABRT signal in unix如何在 unix 中处理 SIGABRT 信号
【发布时间】:2011-05-03 09:43:00
【问题描述】:

我在运行下面的程序时得到核心转储:

$ 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 信号的方法。

【问题讨论】:

标签: unix coredump abort sigabrt


【解决方案1】:

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

如果您真的想处理信号,请安装信号处理程序 见:http://www.manpagez.com/man/2/sigaction/

马里奥

【讨论】:

    【解决方案2】:

    您通常不应该处理它,调用 abort() 的目的是产生核心转储并终止您的程序,就像您的程序一样。

    【讨论】:

      【解决方案3】:
      // 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
      $ 
      

      【讨论】:

      • 请注意,您可以处理 SIGABRT,但不能阻止进程终止(和转储)
      • 我不知道该语句在多大程度上依赖于系统,在我编译并运行上述代码的系统上,没有转储内核。 $ uname -a Linux nas 2.6.32-30-generic #59-Ubuntu SMP Tue Mar 1 21:30:46 UTC 2011 x86_64 GNU/Linux 查看 man 信号,以及 man 7 信号
      猜你喜欢
      • 2012-02-14
      • 2016-03-31
      • 2023-03-28
      • 1970-01-01
      • 2012-04-30
      • 1970-01-01
      • 1970-01-01
      • 2020-03-31
      • 1970-01-01
      相关资源
      最近更新 更多