Ctrl-C和SIGINT有什么区别? [英] What is the difference between Ctrl-C and SIGINT?

查看:218
本文介绍了Ctrl-C和SIGINT有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在调试一个收到KeyboardInterrupt异常后出现段错误的Python程序.这通常是通过从外壳程序按 Ctrl + C 来完成的.为了测试特定代码的更改是否修复了该错误,我有一个小的shell脚本,该脚本在启动后的随机时间将SIGINT发送到程序.我的问题是,发送 Ctrl + C 似乎对程序的影响与发送信号SIGINT的影响不同,因此不会导致错误的出现,所以我很想知道两者之间的区别是什么然后在两个动作之间.

该程序根本不捕获任何键盘操作,而只是一个其中包含一些线程/进程的python程序.它不安装信号处理程序(尽管Python会安装),并且stty -a给出intr = ^C.我怀疑可能是 Ctrl + C SIGINT发送到所有子进程/线程,而kill -INT仅发送到主进程,但这是我的怀疑. >

这是发送kill -INT的shell脚本.

 wait
while :; do
    seconds="$(python -c 'import random; print random.random()*4')"
    ./mandos --debug --configdir=confdir \
             --statedir=statedir --no-restore --no-dbus &
    pid=$!
    { sleep $seconds; kill -INT $pid; } &
    fg %./mandos
    status=$?
    if [ $status -gt 1 ]; then
        echo "Failed exit $status after $seconds seconds"
        break
    fi
    wait
done
 

解决方案

^CSIGINT发送到前台进程组中的所有进程.要使用kill执行等效操作,应将信号发送到进程组(操作系统级别的概念):

kill -SIGINT -<pid>

或工作(shell级概念,管道以&结尾):

kill -SIGINT %

I have been debugging a Python program which segfaults after receiving a KeyboardInterrupt exception. This is normally done by pressing Ctrl+C from the shell. To test if a particular code change fixed the bug, I had a small shell-script that sent SIGINT to the program at random time after start-up. The problem I have is that sending Ctrl+C seems to have a different effect on the program than sending the signal SIGINT and is thus not causing the bug to appear, so I quite wonder what the difference is then between the two actions.

The program does not catch any keyboard actions at all, and is just a python program with some threads/processes in them. It installs no signal handlers (though Python does), and stty -a gives intr = ^C. I suspect it might be that Ctrl+C sends SIGINT to all the sub-processes/threads while kill -INT only sends to the primary process, but that is as far my suspicions go.

Here is the shell script which sends the kill -INT.

wait
while :; do
    seconds="$(python -c 'import random; print random.random()*4')"
    ./mandos --debug --configdir=confdir \
             --statedir=statedir --no-restore --no-dbus &
    pid=$!
    { sleep $seconds; kill -INT $pid; } &
    fg %./mandos
    status=$?
    if [ $status -gt 1 ]; then
        echo "Failed exit $status after $seconds seconds"
        break
    fi
    wait
done

解决方案

^C sends a SIGINT to all the processes in the foreground process group. To do the equivalent with kill, you should send the signal to the process group (OS-level concept):

kill -SIGINT -<pid>

or to the job (shell-level concept, the pipeline ended with &):

kill -SIGINT %

这篇关于Ctrl-C和SIGINT有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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