Why Linux always output “^C” upon pressing of Ctrl+C?

前端 未结 2 2088
生来不讨喜
生来不讨喜 2021-02-15 15:08

I have been studying signals in Linux. And I\'ve done a test program to capture SIGINT.

#include 
#include 
#include 

        
2条回答
  •  花落未央
    2021-02-15 15:27

    The shell echoes everything you type, so when you type ^C, that too gets echoed (and in your case intercepted by your signal handler). The command stty -echo may or may not be useful to you depending on your needs/constraints, see the man page for stty for more information.

    Of course much more goes on at a lower level, anytime you communicate with a system via peripherals device drivers (such as the keyboard driver that you use to generate the ^C signal, and the terminal driver that displays everything) are involved. You can dig even deeper at the level of assembly/machine language, registers, lookup tables etc. If you want a more detailed, in-depth level of understanding the books below are a good place to start:

    The Design of the Unix OS is a good reference for these sort of things. Two more classic references: Unix Programming Environment and Advanced Programming in the UNIX Environment

    Nice summary here in this SO question How does Ctrl-C terminate a child process?

    "when youre run a program, for example find, the shell:

    • the shell fork itself
    • and for the child set the default signal handling
    • replace the child with the given command (e.g. with find)
    • when you press CTRL-C, parent shell handle this signal but the child will receive it - with the default action - terminate. (the child can implement signal handling too)"

提交回复
热议问题