What is the difference between Ctrl-C and SIGINT?

前端 未结 2 529
半阙折子戏
半阙折子戏 2020-12-13 09:35

I have been debugging a Python program which segfaults after receiving a KeyboardInterrupt exception. This is normally done by pressing Ctrl+C from t

相关标签:
2条回答
  • 2020-12-13 09:55

    ^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 %
    
    0 讨论(0)
  • 2020-12-13 09:57

    As described here :

    Python installs a small number of signal handlers by default: SIGPIPE is ignored (so write errors on pipes and sockets can be reported as ordinary Python exceptions) and SIGINT is translated into a KeyboardInterrupt exception. All of these can be overridden.

    so, the behaviour should be the same between sending a SIGINT and a Ctrl + c.

    But, you have to be carefull with the KeyboardInterrupt, if somewhere in your code you've got a

    try:
       ...
    except:   # notice the lack of exception class
       pass
    

    this will "eat" the KeyboardInterrupt exception.

    0 讨论(0)
提交回复
热议问题