Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/bash/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Linux 在bash中将SIGINT从键盘发送到管道命令_Linux_Bash_Signals - Fatal编程技术网

Linux 在bash中将SIGINT从键盘发送到管道命令

Linux 在bash中将SIGINT从键盘发送到管道命令,linux,bash,signals,Linux,Bash,Signals,如果在bash中,我在命令行上运行a | b | c | d,然后按^c,哪个进程会得到信号?简而言之,它们都会得到信号 设置管道时,shell将创建一个^内核的行规程将C解释为用户请求中断当前在前台运行的进程组。向进程组发送信号(如SIGINT)会自动将信号发送到该组中的所有进程。我更喜欢实验: #!/bin/bash # FILE /tmp/bla.sh # trap ctrl-c and call ctrl_c() trap ctrl_c INT MY_ID=$1 # Identifie

如果在bash中,我在命令行上运行a | b | c | d,然后按^c,哪个进程会得到信号?

简而言之,它们都会得到信号


设置管道时,shell将创建一个^内核的行规程将C解释为用户请求中断当前在前台运行的进程组。向进程组发送信号(如
SIGINT
)会自动将信号发送到该组中的所有进程。

我更喜欢实验:

#!/bin/bash
# FILE /tmp/bla.sh
# trap ctrl-c and call ctrl_c()
trap ctrl_c INT

MY_ID=$1 # Identifier for messages

function ctrl_c() {
    echo >&2 "GOODBYE $MY_ID"
    exit
}

# This will continue until interrupted, e.g. if the input/output get closed
cat
# If we somehow got to the end
echo >&2 "grace $MY_ID"
用铁链锁住他们,跑开,打破他们

nitz@mars:~$ /tmp/bla.sh 1 | /tmp/bla.sh 2
^CGOODBYE 2
GOODBYE 1
0

如您所见,两次执行都得到了中断信号,这意味着它们都会被杀死。此外,它们输出它们被杀死的顺序是随机的,例如:

nitz@mars:~$ /tmp/bla.sh 1 | /tmp/bla.sh 2 | /tmp/bla.sh 3 | /tmp/bla.sh 4
^CGOODBYE 2
GOODBYE 4
GOODBYE 1
GOODBYE 3

此外,他们输出他们被杀的顺序是随机的“非常感谢!我认为这真的很重要。