Linux 编程接口中的信号处理程序示例

Signal Handler Example in The Linux Programming Interface

以下示例来自 Michael Kerrisk 的编程 Linux 编程接口

static void sigHandler(int sig){
    printf("Ouch!\n");
}

int main(int argc, char *argv[])
{
    int j;

    if (signal(SIGINT, sigHandler) == SIG_ERR)
    errExit("signal");

    for (j = 0; ; j++){
        printf("%d\n", j);
        sleep(3);
    }
}

应该在用户键入 Control-C (CTRL+C) 时将 "Ouch!" 打印到终端;在作者自己的示例中,他在最终使用 Control-\ (CTRL+\ ) 退出终端之前输入了两次。

当我这样做时,程序在 第一次执行 CTRL+C 时按预期工作。如果我第二次输入它,就像作者在他的示例中所做的那样,我的程序将退出终端 - 它不会打印 "Ouch!" 也不会继续 运行 (循环)。

我使用的代码与本书网站上给出的代码完全相同:

Ouch.c

通常 signal 需要重新安装信号处理程序。否则,它默认为 SIG_DFL(与信号对应的默认操作)。 SIGINT 的默认操作是终止程序。

请注意,printf(3) 不是异步安全函数之一。所以你可以写 (2) 来做同样的事情。查看 Async-signal-safe functions.

的 POSIX 列表

重新安装它应该可以正常工作:

static void sigHandler(int sig){
    signal(SIGINT, sigHandler);
    write(STDOUT_FILENO, "Ouch!\n", 6);
}

这是您应该避免使用 signal and use sigaction 的原因之一。上述行为并非跨平台通用。因此,也许您 运行 的平台不是作者测试其代码的平台,或者您使用的是不同的 Linux 内核。

您需要在收到信号后重新安装信号处理程序的行为是 System V 行为。但是 BSD 语义不需要重新安装。直到最近,Linux 显示了 System V 行为,但它似乎已在最近的内核中得到修复,我在我的 3.19 内核上看不到它,但可以看到 2.6.32 内核(相当旧)。

Signal 的文档指出:

 The situation on Linux is as follows:

   * The kernel's signal() system call provides System V semantics.

   * By default, in glibc 2 and later, the signal() wrapper function
     does not invoke the kernel system call.  Instead, it calls
     sigaction(2) using flags that supply BSD semantics.  This default
     behavior is provided as long as the _BSD_SOURCE feature test macro
     is defined.  By default, _BSD_SOURCE is defined; it is also
     implicitly defined if one defines _GNU_SOURCE, and can of course be
     explicitly defined.

   * On glibc 2 and later, if the _BSD_SOURCE feature test macro is not
     defined, then signal() provides System V semantics.  (The default
     implicit definition of _BSD_SOURCE is not provided if one invokes
     gcc(1) in one of its standard modes (-std=xxx or -ansi) or defines
     various other feature test macros such as _POSIX_SOURCE,
     _XOPEN_SOURCE, or _SVID_SOURCE; see feature_test_macros(7).)

因此您可以通过定义 _BSD_SOURCE 来获得 BSD 语义。因此,您观察到的行为很可能是因为系统上的 signal 遵循 System V 语义,而最近的 Linux(可能还有 Kerrisk 测试它的内容)遵循 BSD 语义。

您不应在信号和异常处理程序中使用 printf(),因为它们不可重入。 printf 还会在将其放入控制台之前在内存中缓冲数据,因此使用 fflush() 将有助于打印但不推荐。出于测试目的,在处理程序中使用计数器(标志)并在处理程序外部使用 printf。不要使用 signal() 来注册处理程序,因为 unix(BSD,Linux) 的每种风格都不提供相同的实现。而是使用 sigaction。