If you run the mount command with no command-line arguments, which displays the file systems mounted on your system, you'll notice a line that looks something like this:
none on /dev/pts type devpts (rw,gid=5,mode=620)
This indicates that a special type of file system, devpts, is mounted at /dev/pts. This file system, which isn't associated with any hardware device, is a "magic" file system that is created by the Linux kernel. It's similar to the /proc file system; see Chapter 7 for more information about how this works.
Like the /dev directory , /dev/pts contains entries corresponding to devices. But unlike /dev, which is an ordinary directory, /dev/pts is a special directory that is created dynamically by the Linux kernel. The contents of the directory vary with time and reflect the state of the running system.
The entries in /dev/pts correspond to pseudo-terminals (or pseudo-TTYs, or PTYs). Linux creates a PTY for every new terminal window you open and displays a corresponding entry in /dev/pts. The PTY device acts like a terminal device—it accepts input from the keyboard and displays text output from the programs that run in it. PTYs are numbered, and the PTY number is the name of the corresponding entry in /dev/pts.
You can display the terminal device associated with a process using the ps command. Specify tty as one of the fields of a custom format with the -o option. To display the process ID, TTY, and command line of each process sharing the sameterminal, invoke ps - opid, tty, cmd.
For example, you can determine the PTY associated with a given terminal window by invoking in the window this command:
% ps -o pid,tty,cmd
PID TT CMD
28832 pts/4 bash
29287 pts/4 ps -o pid,tty,cmd
This particular terminal window is running in PTY 4.
The PTY has a corresponding entry in /dev/pts:
% ls -l /dev/pts/4
crw--w---- 1 samuel tty 136, 4 Mar 8 02:56 /dev/pts/4
Note that it is a character device, and its owner is the owner of the process for which it was created.
You can read from or write to the PTY device. If you read from it, you'll hijack keyboard input that would otherwise be sent to the program running in the PTY. If you write to it, the data will appear in that window.
Try opening a new terminal window, and determine its PTY number by invoking ps -o pid, tty, cmd. From another window, write some text to the PTY device. For example, if the new terminal window's PTY number is 7, invoke this command from another window:
% echo 'Hello, other window!' > /dev/pts/7
The output appears in the new terminal window. If you close the new terminal window, the entry 7 in /dev/pts disappears.
If you invoke ps to determine the TTY from a text-mode virtual terminal (press Ctrl+Alt+F1 to switch to the first virtual terminal, for instance), you'll see that it's running in an ordinary terminal device instead of a PTY:
% ps -o pid,tty,cmd
PID TT CMD
29325 tty1 -bash
29353 tty1 ps -o pid,tty,cmd