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

前端 未结 2 2078
生来不讨喜
生来不讨喜 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:16

    It is the terminal (driver) that intercepts the ^C and translates it to a signal sent to the attached process (which is the shell) stty intr ^B would instruct the terminal driver to intercept a ^B instead. It is also the terminal driver that echoes the ^C back to the terminal.

    The shell is just a process that sits at the other end of the line, and receives it's stdin from your terminal via the terminal driver (such as /dev/ttyX), and it's stdout (and stderr) are also attached to the same tty.

    Note that (if echoing is enabled) the terminal sends the keystrokes to both the process (group) and back to the terminal. The stty command is just wrapper around the ioctl()s for the tty driver for the processes "controlling" tty.

    UPDATE: to demonstrate that the shell is not involved, I created the following small program. It should be executed by its parent shell via exec ./a.out (it appears an interactive shell will fork a daughter shell, anyway) The program sets the key that generates the SIGINTR to ^B, switches echo off, and than waits for input from stdin.

    #include <stdio.h>
    #include <string.h>
    #include <termios.h>
    #include <unistd.h>
    #include <signal.h>
    #include <errno.h>
    
    int thesignum = 0;
    void handler(int signum);
    
    void handler(int signum)
    { thesignum = signum;}
    
    #define THE_KEY 2 /* ^B */
    
    int main(void)
    {
    int rc;
    struct termios mytermios;
    
    rc = tcgetattr(0 , &mytermios);
    printf("tcgetattr=%d\n", rc );
    
    mytermios.c_cc[VINTR] = THE_KEY; /* set intr to ^B */
    mytermios.c_lflag &= ~ECHO ; /* Dont echo */
    rc = tcsetattr(0 , TCSANOW, &mytermios);
    printf("tcsetattr(intr,%d) =%d\n", THE_KEY, rc );
    
    printf("Setting handler()\n" );
    signal(SIGINT, handler);
    
    printf("entering pause()\n... type something followed by ^%c\n", '@'+THE_KEY );
    rc = pause();
    printf("Rc=%d: %d(%s), signum=%d\n", rc, errno , strerror(errno), thesignum );
    
    // mytermios.c_cc[VINTR] = 3; /* reset intr to ^C */
    mytermios.c_lflag |= ECHO ; /* Do echo */
    rc = tcsetattr(0 , TCSANOW, &mytermios);
    printf("tcsetattr(intr,%d) =%d\n", THE_KEY, rc );
    
    return 0;
    }
    

    intr.sh:

    #!/bin/sh
    echo $$
    exec ./a.out
    echo I am back.
    
    0 讨论(0)
  • 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)"
    0 讨论(0)
提交回复
热议问题