C – How does input echo work in Linux terminals?

How does input echo work in Linux terminals?… here is a solution to the problem.

How does input echo work in Linux terminals?

Consider the following code:

#include <stdio.h>
#include <termios.h>
#include <unistd.h>

void disable_echoing()
    termios t;
    tcgetattr(STDIN_FILENO, &t);
    t.c_lflag &= ~ECHO;
    tcsetattr(STDIN_FILENO, TCSANOW, &t);

int main()
    printf("disabling echoing\n");

The program sleeps for one second, then disables echo of input characters, and then reads input characters.

If I type an input character after disabling echo, it doesn’t echo that character. So far, not bad.

If I type an input character before echo is disabled, that character will be echoed. However, at this point the program is sleeping. My question is: if the program is sleeping, what is echoing?

My motivation for asking this question is that a similar program on Windows (apparently with a different mechanism for disabling echo) behaves differently: even if I type input characters while the program is sleeping, echo doesn’t happen, and then when the program wakes up from sleep, it disables echo before running the code that executes echo (getchar()), so still no echo happens.


My question is: if the program is sleeping, what is doing the echoing?

The kernel’s tty layer.


and output through a tty (whether a serial port, virtual console, or PTY) is handled by the kernel based on the current configuration of that tty. This processing is exceptionally complex and can include all of the following features:

  • Input and output queuing and processing flow control
  • Baud rate and parity processing (serial lines).
  • Track the size of the terminal in rows and columns
  • Row buffering and editing
  • Echoes the input to the output
  • Character conversion:
    • Converts output line breaks to CR+NL sequences
    • Converts the entered backspace key to DEL characters
    • Converts tabs to spaces (off by default).
    • Converts backspace characters to alternating sequences, such as abc^H^H^H to abc\cba/ (off by default for hard-copy terminals where characters cannot be deleted).
  • Interpret various types of input control sequences:
    • Some send signals to foreground processes, such as ^C and ^Z.
    • Some trigger basic line editing in the kernel, such as ^H for backspace, ^W for kill-word, and ^U kill.
    • Some interact with flow control, such as ^S and ^Q.

In short, the kernel’s tty layer does a lot of work! It does more than just pass input to output.

Windows does not have a tty layer in the same sense as UNIX systems. It does have a console, but it works very differently – my understanding is that it was primarily designed to emulate the text mode of the PC BIOS, not the terminal.

Related Problems and Solutions