Linux 信号:如果没有处理程序则进程退出

Linux signal: process exit if there's no handler

我在我的程序中使用信号传输消息,我写的代码是这样的:

union sigval sv;
sv.sival_ptr = (void*) data;
pid_t pid = getpid();
if (sigqueue(pid, SIGUSR2, sv) != 0) {
    Log("failed: %d", errno);
    return 0;
} else {
    // do something
}

而且我发现如果我不注册处理程序,我的程序将在调用 sigqueue 时退出,就像我用 SIGTERM 发送信号一样,但我的代码是发送 SIGUSR2。我检查了文档,没有提到退出,所以我做错了什么?

P.S。该程序可能没有按设计注册处理程序。

"我检查了文档,没有任何内容谈到退出"。

文档 谈论 exit/termination。来自 signal manual:

The entries in the "Action" column of the table below specify the default disposition for each signal, as follows:

Term Default action is to terminate the process.
 
...
 
Signal      Standard    Action   Comment
-----------------------------------------
...

SIGUSR2      P1990      Term     User-defined signal 2

这告诉我们 SIGUSR2 的默认操作是终止进程。这正是您所观察到的。

I checked the doc and nothing talks about the exit, so what did I do wrong?

您只是没有找到正确的文档:-)

如果用户未设置任何处理程序,您将从 man 7 signal 获得信号列表和默认行为。

手册页被盗:

DESCRIPTION
       Linux  supports both POSIX reliable signals (hereinafter "standard sig-
       nals") and POSIX real-time signals.

   Signal dispositions
       Each signal has a current disposition, which determines how the process
       behaves when it is delivered the signal.

       The  entries  in the "Action" column of the table below specify the de-
       fault disposition for each signal, as follows:

       Term   Default action is to terminate the process.

       Ign    Default action is to ignore the signal.

       Core   Default action is to terminate the process and  dump  core  (see
              core(5)).

       Stop   Default action is to stop the process.

       Cont   Default  action  is  to  continue the process if it is currently
              stopped.

并且来自 table 的默认操作:

      Signal      Standard   Action   Comment
      ------------------------------------------------------------------------
 
       SIGABRT      P1990      Core    Abort signal from abort(3)
...
       SIGUSR1      P1990      Term    User-defined signal 1
       SIGUSR2      P1990      Term    User-defined signal 2

如您所见,如果用户未指定其他操作,程序将在收到 SIGUSR1 或 SIGUSR2 时终止。