On 2020-04-08,
yon...@yahoo.com <
yon...@yahoo.com> wrote:
> UNIX and Linux terminals allow text input even when the currently
> running program is still outputing text. For example, inside certain
> applications, such as "perl -d", where you send many lines of "print
> 'a';", or in Oracle's sqlplus or MySQL's mysql, where you send many
> "select 1 from dual;", the input and output are garbled. Some programs
> do a better job, such as bash, python.
This is because the TTY line discipline is in a mode where echo (stty echo)
is turned on.
Regardless of whether echo is turned on, the TTY device receives
characters and buffers them at all times, even when the application
isn't reading. (If the buffer gets full, flow control has to kick in,
failing which, data will be dropped.)
If echo is turned on, then the low level code in the TTY device driver
spits back whatever it receives.
Interactive bash does a "better job" because it puts the tty driver in a
raw mode with disabled echo, in order to have more fully featured
editing. When executing a command, bash puts itself into the background
and restores the "cooked mode" TTY settings.
Echo is very important for the sake of rudimentary editing. Most input
processing programs do not have interactive editing features, yet
sometime you have to interact with them.
For intance, cat:
$ cat
abc_
cat just calls read() and that's it. When you hit Enter to generate
a newline, the read() returns an entire line.
The reason you can see what you type, and use backspace to make
corrections: that's all done in the TTY driver, invisibly to cat.
And the TTY driver, ont he ther hand, does its job without visibility as
to what the cat process is doing. The editing works without regard for
whether some process is calling read() on the file descriptor.
Implementation-wise, it happens in software levels below where file
descriptor reads are handled. There some very low level driver that
could be a serial device or console or the master side of a pseudo-tty.
That low level driver typically spontaneously sends characters to a
higher level, at interrupt time. In that higher level is an mediating
module called the "line discipline" which implements the echo,
backspace, word erase and whatnot. That module, in turn, sends data to a
higher layer still where file descriptor interaction takes place: like
blocking a read() on the availability of data, putting
data in the buffr and unblocking it.
--
TXR Programming Lanuage:
http://nongnu.org/txr
Music DIY Mailing List:
http://www.kylheku.com/diy
ADA MP-1 Mailing List:
http://www.kylheku.com/mp1