4

My question is more about the conception and design of pty than how it works. I read chapter 64 (pseudo-terminals) of "The Linux Programming Interface" book and I can't understand why we use a pair of pseudo-terminal (ptmx, pts) and not one. When we look at /dev/tty[1,6], the virtual terminal is emulated and use one file. Why not do the same thing for pseudo-terminal and design an emulated system using one file instead of two?

  • If you use one file, how do you indicate if you want the master or the slave side if you pass the path to some program? – dirkt Oct 12 '19 at 04:13
  • Conception, design and function (how it works) I would not know how to keep apart in the art of programming. –  Oct 12 '19 at 06:39
  • @Biswapriyo is that one shot much longer than your comment to summarize? yesterday a video, today books. –  Oct 12 '19 at 14:20
  • @Hisab I wanted to remark that maybe the programming Interface is not the place to start. Even the drivers/tty/Kconfig file has two PTY-versions. PTY has quite evolved it seems. Unix98pty and BSD. Explains lots of misunderstandings. –  Oct 12 '19 at 14:23

2 Answers2

1

I was confused by this at first as well but I believe I understand the conceptual (and practical) answer to this now.

There are two parties involved in communicating here. If you had a single device it would act like a loopback and echo back whatever is written to it on the same device. This would make it difficult for either party to know who sent the data (each one would see the data it just wrote to the device sent right back to it).

This is solved conceptually by having each party write/read from one device that is linked to the other device.

0

To facilitate moving the terminal emulation into userland, while still keeping the TTY subsystem (session management and line discipline) intact, the pseudo terminal or pty was invented.

A guy called Linus Akesson has a TTY demystified page for the whole picture. (I found it via arch linux wiki ADDED: mosvy seems to hate it...).

Few days later I still like that answer I cited above. Even wikipedia has the link. After some discussions (see comments, alas) I have edited freely what follows. The diffs can be seen in the "edited ..." button..."PTMX" and "TTY Subsystem" are the new words. Good official (?) source: drivers/tty/Kconfig


And "terminal emulation in userland" is what xterm (etc.) do. XTerm, together with X and window manager, have to make 'bash' feel like running on a Linux VT.

A direct consequence of X taking care of "everything" from userland is: after starting many xterms/bashes, each xterm is connected to pts/0, and each bash to pts/1, pts/2, ... I can also start child xterms/bashes from inside one with xterm &. Or start a new shell layer within the xterm with bash. I any case a user soon looses track of the dependencies --- so facile is it with PTY to spawn windows with shells.

The VTs (=VCs) from "TTY subsystem" are used more static, with 64 as a (first?) limit, and with only 6 set up after boot. From the point of view of getty/agetty ("opens a tty port, ...") it might look different.


Here are my special files with major 5 in /dev:

crw-rw-rw- 1 root tty      5, 0     tty
crw------- 1 root root     5, 1     console
crw-rw-rw- 1 root tty      5, 2     ptmx

Pairs? That term apparently comes from the BSD style of PTY, when each new pty came in a "p" and a "s" file. With UNIX98_PTY it is:

/dev/ptmx: MX like MULTIPLEX (not MASTER X) for the conceptual master side. And for the client side /dev/pts/1-n. Of /dev/pts/0 I want to say nothing now. One busy MASTER and many busy SLAVES. It is more a 1-to-n relation than a pair.

These three char device special files with major=5 are actually special special files. As files they do not do much. But behind every device file is some code, and here it is drivers/tty/*.c. The keyboard processing is not a big thing. It's VT switching, and line discipline, and both at the same time maybe.

There is a directory /dev/pts/ with another ptmx file and all the major=136 slave device files. Normal /dev/ttyN have major=4.


spinoff from the (deleted?) comments: This is one of the ioctl_console requests the other Q was discussing. "tty is not a virtual terminal", says S.K. in ioctl VT_GETSTATE...tty is not a vt (in a last comment there, sorry)

   VT_GETSTATE
          Get global vt state info.  argp points to a

              struct vt_stat {
                  unsigned short v_active;  /* active vt */
                  unsigned short v_signal;  /* signal to send */
                  unsigned short v_state;   /* vt bit mask */
              };

          For each vt in use, the corresponding bit in the v_state mem‐
          ber is set.  (Kernels 1.0 through 1.1.92.)

From config UNIX98_PTY (Kconfig in drivers/tty)

Typical programs for the master side are telnet servers and xterms.

It's the bash processes that get /dev/pts/1-255, not xterm. xterm and the wm are the brokers between x and bash. It's a joint effort from the start with xinit wm x or xinit client -- server display.

  • @mosvy this citation I might as well take back, it is at least unprecise. But ps shows each xterm process connected to pts/0. For simplicity I left out X. But what about the major numbers: 5, 4 and 136? I thought I has quite straightened it out. Especially after a Q asking about ioctl and why /dev/tty is NOT a virtual terminal (or the other way round). –  Oct 12 '19 at 07:58
  • Multiplexing: but somehow what is "System Console", conceptually, turns into 64 "Virtual Terminals" alone by kernel and very "say YES" module VT. –  Oct 12 '19 at 08:00
  • @mosvy sorry for upsetting you so. I am not interested in tmux I have vt and pty, –  Oct 12 '19 at 08:08
  • @Stephen Kitt You answered a Q a while ago about this VT_GETSTATE ioctl. –  Oct 12 '19 at 08:32
  • @mosvy if you want to tell me "hard use" things about ptys, please use a fresh sheet of paper (ie answer with an answer). And what for is that major=5 file ptmx in /dev/pts/ folder, like a shepherd? A twin of /dev/ptmx? –  Oct 12 '19 at 08:42
  • me neither..... –  Oct 12 '19 at 09:08
  • @mosvy, meh, if there are easily found better resources on the subject, you might as well link to one. If not for the poster, for other readers. – ilkkachu Oct 13 '19 at 13:47
  • @rastafile, what's that "the other Q" you mention discussing ioctl_console? I can see one link to a question in your post, but it doesn't mention that keyword. – ilkkachu Oct 13 '19 at 13:49
  • @ilkkachu: "See the first link in my answer. tty isn’t a virtual terminal." --- says Stephen in the last comment to his answer. –  Oct 13 '19 at 13:52
  • @rastafile, the first link in your answer is to The TTY demystified, not to a question. What I'm trying to point out here, is that your answer is very hard to read for someone coming from the outside, since it seems to be part some other discussion, not a self-contained answer. E.g. that "the other Q" sentence assumes the reader knows what Q you're talking about. – ilkkachu Oct 13 '19 at 14:03
  • @ilkkachu Well that is why I have the line and the personal "@mosvy", and you have the comments. The "other Q" IS this VT_GETSTATE Q. Do you know see clear? Anyway I asked a new Q myself, about orthogonal TTYs. Really. Same topic. Close this one if you don't understand. –  Oct 13 '19 at 14:22
  • @rastafile, yes, that's indeed the thing. Discussions belong in chat, definitely not in questions and answers. Similarly, commenting edits isn't really useful, actually just confusing. Posts should be standalone. – ilkkachu Oct 13 '19 at 14:30
  • @mosvy, you do realize that source code isn't very easy to read for many (unless you meant that the issues are explained in comments in the source code, that would be different). For example, I would be interested in what issues ptmx has with namespaces, but I'm pretty sure my kernel-fu isn't up to figuring that out from the source code. – ilkkachu Oct 14 '19 at 10:02
  • @ilkkachu I edited a bit and took away the personal address. I really think it is a OK answer. This "pair" term really is in man pty, So I mention BSD and UNIX98, now. Before, it had gotten mixed up. Of course, the whole thing is linked with that general TTY/VT/VC discussion. BTW. I see "pseudo-terminals" mentioned in linux 0.01 in a comment. –  Oct 14 '19 at 10:35