0

Here is my bash session:

lrwx------ 1 stas stas 64 Mar  5 18:18 0 -> /dev/pts/0
lrwx------ 1 stas stas 64 Mar  5 18:18 1 -> /dev/pts/0
lrwx------ 1 stas stas 64 Mar  5 18:18 2 -> /dev/pts/0

We see that bash both reads from and writes to /dev/pts/0. My question is: how come the process doesn't read what it just wrote there? What makes only master pty receive the data?

  • That's the same for other types of text terminals. E.g. a serial line with an old green screen terminal. Whatever the process writes to that serial line is read by the terminal. Whatever the terminal sends to the serial line can be read by the process. In your case, the master replaces the old VT100. – berndbausch Mar 06 '21 at 03:46
  • @berndbausch, I was wondering about the logic at a lower level. But maybe I got it already.. Do I understand it correctly that since it's the same `fd` - the file position is shared for both reading & writing operations. So when we write something - the process won't read it because writing moved the position further down the file? – Stanislav Bashkyrtsev Mar 06 '21 at 05:31
  • If you share the file descriptor, you also share the position. That far, that's correct. However, in your case, there are three file descriptors, namely 0, 1 and 2. But this is not relevant anyway. The process doesn't read what it wrote because a pseudo terminal slave doesn't act like a regular file, but like a serial line. – berndbausch Mar 06 '21 at 05:46
  • @berndbausch, ok, got it, thanks! – Stanislav Bashkyrtsev Mar 06 '21 at 08:05

1 Answers1

1

/dev/pts/0 is not a regular file but a character device node, as can be seen in the first column of ls -loutput:

v
crw--w---- 1 tilman tty  136, 0 Mär  6 20:25 /dev/pts/0

As such, read and write operations are not accessing some file on disk, but are instead processed by a piece of software called a device driver which can do pretty much anything its author wanted it to do, from just nothing at all (as in the case of the driver behind /dev/null) to elaborate actions on some piece of hardware in your computer (as in the case of actual hardware drivers.)

In the specific case of /dev/pts/0 that driver is written to (very simplified):

  • If a process writes some data to the slave device, provide that data as the result to the next read operation on the master device.
  • If a process writes some data to the master device, provide that data as the result to the next read operation on the slave device.

It is not written to provide data written to the slave device back to a read operation on the slave device, therefore that does not happen.

Tilman Schmidt
  • 4,101
  • 12
  • 27