簡體   English   中英

當按下 ctrl+c、tty 驅動程序或 shell 時,誰將 SIGINT 發送到前台進程

[英]who send SIGINT to foreground process when press ctrl+c, tty driver or shell

當我在登錄 shell 執行命令時按 ctrl+c 時,前台進程被殺死。
誰發出信號?

TTY 驅動程序是否直接將 SIGINT 發送到前台進程組?
或者 TTY 驅動程序是否將 SIGINT 發送到 shell,然后 shell 將信號重定向到前台進程組?

tty 驅動程序(特別是線路規則)將信號直接發送到前台進程組。 這是來自 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僅幾步之__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;
        }
    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM