2

A shell in Linux (for example: bash) have its stdin and stdout and stderr file descriptors all point to the same device file, for example, the following are thestdin and stdout and stderr file descriptors for bash:

enter image description here

Now /dev/tty1 is not a "real" file that you can read from and write to, it is a device file that points to a file or to a buffer in memory or to something else.

Now my question is, does /dev/tty1 points to only one file, or does it point to two files?

What I mean is, when bash reads from /dev/tty1 (stdin), and when bash writes to /dev/tty1 (stdout or stderr), is it reading from and writing to the same file, or does /dev/tty1 points to two files, one is used when reading from /dev/tty1, and the other is used when writing to /dev/tty1?

1
  • 3
    No, device files do not point to files. May 19, 2017 at 17:43

2 Answers 2

7

A device node points to a single device, which in Linux is handled by the kernel. When bash reads from /dev/tty1, it reads from the device driver managing the first terminal; when it writes to it, it writes to the same device driver.

3
  • I assume that when bash reads from the device driver, the device driver will return data stored in a buffer (this data in this buffer is sent by the terminal), and when bash writes to the device driver, the device driver will store this data in another buffer (the terminal will then read from this buffer), am I correct?
    – Steve
    May 19, 2017 at 16:25
  • I’d have to check, but I’d expect reads to be buffered in some way or other (so you don’t lose keystrokes), but writes to go directly to the device (the console in this case). Note that in terms of physical devices, the tty driver interacts with different devices for input and output: the keyboard for input, and the screen for output. May 19, 2017 at 16:31
  • 2
    @Steve Yes, if you look carefully at the termios interfaces, you will see that each tty has both an input and an output buffer.
    – zwol
    May 19, 2017 at 18:49
3

A device file doesn't “point to multiple files”. A device file doesn't even “point” to a single file. A device file points to a driver. The driver contains code that handles read and write operations.

There doesn't have to be a relationship between the data that is read from the device and the data that is written. Some devices have one, others don't. For example, with a disk, the data that is read at a certain position is the data that was previously written to that position. On the other hand, with a serial port, data written to the device is sent on the serial line, and data read from the device was received on the serial line; what is sent is independent from what is received.

A terminal is very much like a serial device. In fact, historically, physical terminals were usually connected through serial lines. When an application reads from a terminal file, it receives data that was sent by the terminal driver or by the terminal emulator — the terminal driver/emulator writes input to its side of the terminal, the application reads input from its side of the terminal. When an application writes to a terminal file, it sends data that is then read by the terminal driver or by the terminal emulator — the application writes output to its side of the terminal, the terminal driver/emulator reads ouput to its side of the terminal.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .