ctrl+c 会向 Linux 中的父进程和子进程发送 SIGINT 信号吗?

Will ctrl+c send SIGINT signals to both parent and child processes in Linux?

在终端中,我执行了一个主要的父进程,它将派生一个子进程。在父进程和子进程中,我都实现了 SIGINT 信号处理程序。 那么当我按下 "ctrl+c" 时,两个处理程序会同时被调用吗?或者我是否需要在父进程的处理程序中显式调用子进程的信号处理程序?

我查了这个 post: How does Ctrl-C terminate a child process? 这表示“SIGINT 信号由终端线路规程生成,并广播到终端的前台 进程组 中的所有进程”。我只是不太明白"foreground process group"是什么意思。

谢谢,

In both the parent and child processes I implemented a SIGINT signal handler. So when I press "ctrl+c", will both the handlers be called at the same time?

是的,他们都将收到 SIGINT

Or do I need to call the child process's signal handler explicitly in the parent process's handler?

"Calling" 另一个进程的信号处理程序没有意义。如果这两个进程都安装了处理程序,那么一旦它们收到信号 SIGINT.

,它们就会被调用

I just didn't quite understand what does "foreground process group" means.

通常,与控制终端关联的进程是前台进程,其进程组称为 前台进程组。 当您从命令行启动进程时,它是前台进程进程:

例如

$ ./script.sh # foreground process
$ ./script & # background process

我建议您阅读有关 tty and The TTY demystified 的详细说明。

setpgid POSIX C进程组最小示例

这说明了如果 child 没有用 setpgid 更改其进程组,那么信号是如何发送到 child 的。

main.c

#define _XOPEN_SOURCE 700
#include <assert.h>
#include <signal.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

volatile sig_atomic_t is_child = 0;

void signal_handler(int sig) {
    char parent_str[] = "sigint parent\n";
    char child_str[] = "sigint child\n";
    signal(sig, signal_handler);
    if (sig == SIGINT) {
        if (is_child) {
            write(STDOUT_FILENO, child_str, sizeof(child_str) - 1);
        } else {
            write(STDOUT_FILENO, parent_str, sizeof(parent_str) - 1);
        }
    }
}

int main(int argc, char **argv) {
    pid_t pid, pgid;

    (void)argv;
    signal(SIGINT, signal_handler);
    signal(SIGUSR1, signal_handler);
    pid = fork();
    assert(pid != -1);
    if (pid == 0) {
        /* Change the pgid.
         * The new one is guaranteed to be different than the previous, which was equal to the parent's,
         * because `man setpgid` says:
         * > the child has its own unique process ID, and this PID does not match
         * > the ID of any existing process group (setpgid(2)) or session.
         */
        is_child = 1;
        if (argc > 1) {
            setpgid(0, 0);
        }
        printf("child pid, pgid = %ju, %ju\n", (uintmax_t)getpid(), (uintmax_t)getpgid(0));
        assert(kill(getppid(), SIGUSR1) == 0);
        while (1);
        exit(EXIT_SUCCESS);
    }
    /* Wait until the child sends a SIGUSR1. */
    pause();
    pgid = getpgid(0);
    printf("parent pid, pgid = %ju, %ju\n", (uintmax_t)getpid(), (uintmax_t)pgid);
    /* man kill explains that negative first argument means to send a signal to a process group. */
    kill(-pgid, SIGINT);
    while (1);
}

GitHub upstream.

编译:

gcc -ggdb3 -O0 -std=c99 -Wall -Wextra -Wpedantic -o setpgid setpgid.c

运行 无 setpgid

没有任何 CLI 参数,setpgid 未完成:

./setpgid

可能的结果:

child pid, pgid = 28250, 28249
parent pid, pgid = 28249, 28249
sigint parent
sigint child

程序挂起。

正如我们所见,两个进程的 pgid 相同,因为它在 fork.

之间继承

然后每当你点击:

Ctrl + C

再次输出:

sigint parent
sigint child

这说明了如何:

  • 使用 kill(-pgid, SIGINT)
  • 向整个进程组发送信号
  • 终端Ctrl+C默认对整个进程组发送kill

通过向两个进程发送不同的信号来退出程序,例如SIGQUIT 与 Ctrl + \.

运行 与 setpgid

如果您 运行 带有参数,例如:

./setpgid 1

然后 child 更改其 pgid,现在每次仅从 parent 打印一个 sigint:

child pid, pgid = 16470, 16470
parent pid, pgid = 16469, 16469
sigint parent

现在,每当您点击:

Ctrl + C

只有 parent 也收到信号:

sigint parent

你仍然可以像以前一样使用 SIGQUIT 杀死 parent:

Ctrl + \

然而 child 现在有一个不同的 PGID,并且没有收到那个信号!这可以从:

ps aux | grep setpgid

你必须明确地杀死它:

kill -9 16470

这清楚地说明了信号组存在的原因:否则我们将一直留下一堆需要手动清理的进程。

测试于 Ubuntu 18.04.