The OpenNET Project / Index page

[ новости /+++ | форум | теги | ]

Поиск:  Каталог документации

Next Previous Contents

6. Terminal Special Files such as /dev/tty

"tty" is an abbreviation for "Teletype". The first terminals were Teletypes (like remotely controlled typewriters). See subsection Teletypes. A list of Linux devices (the stuff in the /dev directory) may be found in "Linux Allocated Devices" which should be included with kernel sources. It "describes" what each device used for in only a word or two but doesn't tell you how to use them.

6.1 Serial Port Terminals

The computer considers each serial port to be a "device". It's sometimes called a terminal device since at one time terminals were the most common use for a serial port. For each such serial port there is a special file in the /dev (device) directory. /dev/tts/0 (or /dev/ttyS0) is the special file for the serial port known as COM1 in the DOS/Windows world. The device filesystem notation: tts/0 is replacing the older ttyS0 notation. But the older notation will be often used in this howto since it is still widely used and often still works (using symbolic links) on the newer device filesystems.

To send text to a terminal you may redirect standard output of some command-line command to the appropriate special file. For example typing "echo test > /dev/ttyS1" at the command prompt should send the word "test" to the terminal on ttyS1 (COM2) provided you have write permission on /dev/ttyS1. Similarly, typing "cat my_file > /dev/ttyS0" will send the contents of the file my_file to COM1 (ttyS0).

6.2 Pseudo Terminals

Pseudo terminals are pairs of devices such as /dev/pty/m3 (formerly /dev/ptyp3) and /dev/pty/s3 (formerly /dev/ttyp3). 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.

You can't just take a pair of programs and make them talk to each other this way without modifying the source C code of one of the pair (for example, the one that talks to m3). Thus it's mainly programmers that must concern themselves with pseudo terminals and most users don't need to worry about them.

For example, if someone connects via telnet to your computer over a network, they may wind up connected to the device m2 (ptyp2) (a pseudo terminal port). The login process logs them in to s2 (ttyp2). Here the login program and the telnet program talk to each other via a "pseudo terminal". In X Window, the terminal emulator program, xterm (or rxvt), uses pseudo terminals. Ham radio programs under Linux also use them. 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 think of s as also 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 filesystem.

While other unix-like systems have a manual page for pseudo terminals (may be named "pty") Linux lacks one. A page devoted to only to pseudo terminals is needed for Linux. There is both a Linux pty module and a /usr/include/pty.h file.

6.3 The Controlling Terminal /dev/tty

/dev/tty stands for the controlling terminal (if any) for the current process. To find out which tty's are attached to which processes use the "ps -a" command at the shell prompt (command line). Look at the "tty" column. For the shell process you're in, /dev/tty is the terminal you are now using. Type "tty" at the shell prompt to see what it is (see manual pg. tty(1)). /dev/tty is something like a link to the actually terminal device name with some additional features for C-programmers: see the manual page tty(4).

6.4 /dev/ttyIN "Terminals"

N stands for an integer. One use of these in Linux is with the ISDN driver package: isdn4linux. The ttyIN is something like ttySN but it emulates a modem and can be given modem commands.

6.5 The Console: ttyN or vc/N

In Linux the PC monitor is usually called the console and has several device special files associated with it: vc/0 (tty0), vc/1 (tty1), vc/2 (tty2), etc. When you log in you are on vc/1. To go to vc/2 (on the same screen) press down the 2 keys Alt(left)-F3. For vc/3 use Left Alt-F3, etc. These (vc/1, vc/2, vc/3, etc.) are called "virtual terminals". vc/0 (tty0) is just an alias for the current virtual terminal and it's where messages from the system are sent. Thus messages from the system will be seen on the console (monitor) regardless of which virtual terminal it is displaying.

You may log in to different virtual terminals and thus have a few different sessions with the computer going on at the same time. Only the system or the root user may write to /dev/vc/0 to which /dev/console is sometimes linked. For more info on the console see The Linux Console.

6.6 Creating a Device with "mknod"

The /dev directory comes supplied with many device special files. If you need something that's not there you may try to create it with the "mknod" command. See the manual page ttys(4) for how to do this for serial ports. To use mknod you must know the major and minor device numbers. You might be able to infer the numbers you need by using the "ls -l" command in the /dev directory. It will display the major and minor numbers of existing special files.

Next Previous Contents

Inferno Solutions
Hosting by

Закладки на сайте
Проследить за страницей
Created 1996-2024 by Maxim Chirkov
Добавить, Поддержать, Вебмастеру