gpt4 book ai didi

linux - 谁在按下ctrl + c,tty驱动程序或shell时将SIGINT发送到前台进程

转载 作者:行者123 更新时间:2023-12-03 09:53:59 25 4
gpt4 key购买 nike

当我在登录Shell执行命令时按ctrl + c时,前台进程将被杀死。
谁发送信号?

TTY驱动程序是否将SIGINT直接发送到前台进程组?
还是TTY驱动程序将SIGINT发送到shell,shell将信号重定向到前台进程组?

最佳答案

tty驱动程序(特别是线路规程)将信号直接发送到前台进程组。这是代码from Linux,您可以在其中看到它只是获取前景组并发出信号:

/**
* [...]
* Called when a signal is being sent due to terminal input.
* [...]
*/

static void __isig(int sig, struct tty_struct *tty)
{
struct pid *tty_pgrp = tty_get_pgrp(tty);
if (tty_pgrp) {
kill_pgrp(tty_pgrp, sig, 1);
put_pid(tty_pgrp);
}
}

这是从同一文件中的输入处理函数调用的,其中 n_tty_receive_signal_char只是调用 __isig的几步:

/**
* [...]
* Process an individual character of input received from the driver.
* This is serialized with respect to itself by the rules for the
* driver above.
* [...]
*/

static int
n_tty_receive_char_special(struct tty_struct *tty, unsigned char c)
{
struct n_tty_data *ldata = tty->disc_data;

/* [...] */

if (L_ISIG(tty)) {
if (c == INTR_CHAR(tty)) {
n_tty_receive_signal_char(tty, SIGINT, c);
return 0;
} else if (c == QUIT_CHAR(tty)) {
n_tty_receive_signal_char(tty, SIGQUIT, c);
return 0;
} else if (c == SUSP_CHAR(tty)) {
n_tty_receive_signal_char(tty, SIGTSTP, c);
return 0;
}
}

关于linux - 谁在按下ctrl + c,tty驱动程序或shell时将SIGINT发送到前台进程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60193762/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com