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?

4
  • 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, 2019 at 4:13
  • Conception, design and function (how it works) I would not know how to keep apart in the art of programming.
    – user373503
    Oct 12, 2019 at 6:39
  • @Biswapriyo is that one shot much longer than your comment to summarize? yesterday a video, today books.
    – user373503
    Oct 12, 2019 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.
    – user373503
    Oct 12, 2019 at 14:23

2 Answers 2

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.

14
  • @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).
    – user373503
    Oct 12, 2019 at 7:58
  • Multiplexing: but somehow what is "System Console", conceptually, turns into 64 "Virtual Terminals" alone by kernel and very "say YES" module VT.
    – user373503
    Oct 12, 2019 at 8:00
  • @mosvy sorry for upsetting you so. I am not interested in tmux I have vt and pty,
    – user373503
    Oct 12, 2019 at 8:08
  • @Stephen Kitt You answered a Q a while ago about this VT_GETSTATE ioctl.
    – user373503
    Oct 12, 2019 at 8: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?
    – user373503
    Oct 12, 2019 at 8:42

You must log in to answer this question.

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