陷阱为"kill 0&"的分段错误从bash中的subshel​​l [英] Segmentation Fault with trap "kill 0" from subshell in bash

查看:60
本文介绍了陷阱为"kill 0&"的分段错误从bash中的subshel​​l的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试编写一个小脚本来并行构建多个Dockerfile,但是,如果我想取消该脚本,我还想杀死子进程.这样做会导致 segmentation fault (分段错误),不确定原因.

I'm trying to write a little script that builds multiple Dockerfiles in parallel, however if I ever want to cancel the script I want to also kill the child processes. Doing so results in a segmentation fault, not sure why.

为了澄清,我使用 ^ C

trap "kill 0" SIGINT SIGTERM
DIRS=$( find . -name "Dockerfile" -printf "%h\n" )
for file in $DIRS; do  
    (   
        # testing script
        while :
        do
                echo "$file"
                sleep 1
        done
    ) & 
done
wait 

推荐答案

这个简单的脚本:

trap "kill 0" SIGTERM
kill 0

将导致分段错误.
为什么?
kill 命令将SIGTERM发送到该进程.我们在接收SIGTERM的过程中会对此信号执行信号处理程序,该处理程序为 kill 0 .这将SIGTERM再次发送到进程,并再次执行信号处理程序.该过程一直持续到堆栈溢出,这会导致分段错误.
使用 bash -x set -x 运行这样的脚本会产生类似以下内容:

will result in segmentation fault.
Why?
kill command sends SIGTERM to the process. Our process on receiving SIGTERM executes signal handler for this signal, which is kill 0. This sends SIGTERM to the process again and executes the signal handler again. The process is stuck until stack overflow, which results in segmentation fault.
Running such script with bash -x or with set -x results in smth like:

+ trap 'kill 0' SIGTERM
+ kill 0
++ kill 0
+++ kill 0
++++ kill 0
+++++ kill 0
...
+++++ ... so on ... ++++ kill 0
Segmentation fault (core dumped)

可能您是说陷阱"exit 0" SIGTERM .

这篇关于陷阱为"kill 0&"的分段错误从bash中的subshel​​l的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆