1. Technology
You can opt-out at any time. Please refer to our privacy policy for contact information.

Discuss in my forum

Text-Terminals on Linux


6.2 Pseudo Terminals

Pseudo terminals are pairs of devices such as /dev/pty/m3 and /dev/pty/s3 (or respectively /dev/ptyp3 and /dev/ttyp3 if you're not using the device filesystem). There is no physical device directly associated with either of them, not even a serial port connector. But if a program treats s3 (ttyp3) like it was a serial port, what is read and written to that port appears on the other member of the pair m3 (ptyp3) which another program uses to read and write to. Thus two programs talk to each other via this method and one program (on s3=ttyp3) thinks it's talking to a serial port. It's something like a "pipe" between m3 and s3.

For talking to s3 (ttyp3), any program designed to talk to a serial port will do. But for the other program that talks to m3 (ptyp3), it must have been specially written to talk to m3. It's mainly programmers that must concern themselves with pseudo terminals and most users don't need to worry about them.

Here's an example: If someone connects via telnet to your computer over a network (you are a telnet server), the part of the telnet program handling this connection on your computer may wind up connected to the pseudo terminal m2 (ptyp2). A getty program should be running on the corresponding s2 (ttyp2). Getty thinks it's talking to a terminal. When telnet gets a character from the remote client, it goes thru m2 to s2 to getty which then sends back "login:" routed to s2, m2, your server telnet program, and then out to the network back to the client. Here the login program and the telnet server program talk to each other via a "pseudo terminal". Note that there is no pseudo terminal used on the client computer, just telnet. Of course the server allocates a pseudo terminal (on the server) for each client.

In X Window, the terminal emulator program, xterm (or rxvt), uses pseudo terminals. Ham radio programs under Linux also use them. By using certain application software, it is possible to have 2 or more pseudo terminals attached to the same physical serial port.

For a pseudo terminal pair such as m3 (ptyp3) and s3 (ttyp3), the m... (pty...) is the master or controlling terminal and the s... (tty...) is the slave. The device filesystem notation makes this clear (m is for master, s is for slave). The slave is like a serial port so also think of s as standing for "serial". In the old notation, tty.. is like a serial port ttyS (which in olden days was just tty).

Prior to the device filesystem a complex notation was used in order to get a large number of pseudo terminals. There are only 16 ttyp's: ttyp0-ttypf (f is a hexadecimal digit). To get more pairs, more letters such as q, r, s were used instead of p. For example the pair ttys8, ptys8 was a pseudo terminal pair. Later on, even more letters were added so as to allow even more pseudo terminals. With the device filesystem, we may just use, for example, /dev/pty/m57 instead of /dev/ttys9 for the 58th pty master. People have made the mistake of typing say ttys2 (which is a pseudo serial port) when they meant to type ttyS2 (a real serial port).

The master and slave are really the same "port" but the slave is used by the application program and the master is used by a network program (or the like) which supplies (and gets) data to/from the slave port. The program using the slave port can run "as is" since it thinks it is talking to a serial port.

Unix98 (available on Linux) doesn't use the above but instead uses a "pty master" which might be, for example, /dev/ptm3. It's slave is automatically created as /dev/pts/3. It thus supplies a pty on demand. The /dev/pts directory is considered to be a file system of type devpts and appears in the lists of mounted filesystems. While the "file" /dev/pts/3 looks like it would be an entry in the device filesystem, it's really a wholly different kind of filesystem.

While other unix-like systems have a manual page for pseudo terminals (may be named "pty") Linux lacks one for the general user. But there is a man-page for programmers: (openpty or forkpty) which assumes that you already understand pseudo terminals. There is both a Linux pty module and a /usr/include/pty.h file.

* License

* Text Terminal How-To Index

  1. About.com
  2. Technology
  3. Linux

©2014 About.com. All rights reserved.