terminal
The basic concept of the terminal
On UNIX systems, the user gets a Shell terminal login process after the adoption of the system, this terminal becomes the controlling terminal Shell process (Controlling Terminal)In "Introduction" section talked about, the terminal control information is stored in the PCB, and we know that fork
will copy the information in the PCB, the control terminal of other processes started by the Shell process is also the terminal. By default (no redirects), each process standard input, standard output and standard error output point to control terminal, a process that is read from the standard input read the user's keyboard input, processes to standard output or standard error output also write It is output to the display. Furthermore, in Chapter 33 signal is also mentioned, in some special control terminal controls the input signal to a foreground process can, for example, Ctrl-C represent SIGINT
, Ctrl- \ expressed SIGQUIT
.
In Chapter 28 file I / O in said before, each process through a special device file /dev/tty
access its control terminal. In fact each terminal device corresponds to a different device file, /dev/tty
provides a common interface, a process to access it either through the control terminal /dev/tty
can also be accessed through the device file corresponding to the terminal device. ttyname
Function can be isolated by the file descriptor corresponding to the file name, the file descriptor must refer to a terminal apparatus and the file can not be arbitrary. Here we look at a variety of different experiments corresponding to a terminal device file name.
Example 34.1. See file name corresponding to the terminal device
#include <unistd.h>
#include <stdio.h>
int main()
{
printf("fd 0: %s\n", ttyname(0));
printf("fd 1: %s\n", ttyname(1));
printf("fd 2: %s\n", ttyname(2));
return 0;
}
Run in graphical terminal window this program, you may get
$ ./a.out
fd 0: /dev/pts/0
fd 1: /dev/pts/0
fd 2: /dev/pts/0
Then open a terminal window and run this program, you may get will
$ ./a.out
fd 0: /dev/pts/1
fd 1: /dev/pts/1
fd 2: /dev/pts/1
Switching Ctrl-Alt-F1 to the terminal character run this program, the result is
$ ./a.out
fd 0: /dev/tty1
fd 1: /dev/tty1
fd 2: /dev/tty1
Readers can try again in a character terminal Ctrl-Alt-F2 or in telnet
or ssh
run the program under the landing network terminal and see what results are.
Terminal login process
A PC typically only a single keyboard and a display, which is only a terminal device, but can Ctrl-Alt-F1 ~ Ctrl-Alt-F6 switch to terminal 6 characters, corresponding to six sets of virtual terminal equipment, they share the same set of physical terminal device, the corresponding device file are /dev/tty1
~ /dev/tty6
, so called virtual terminal (virtual terminal). Device file /dev/tty0
represents the current virtual terminal, such as switching to a character terminal Ctrl-Alt-F1's /dev/tty0
says /dev/tty1
, when the terminal switches to the character of Ctrl-Alt-F2 /dev/tty0
says /dev/tty2
, just /dev/tty
as is a common interface, but it can not be represented Graphics a terminal corresponding to the terminal window.
As another example, often used for embedded serial terminal development, each port corresponding to a target board terminal device, for example /dev/ttyS0
, /dev/ttyS1
like the host and target connection together with a serial line, it can be passed over the host Linux the minicom
or Windows HyperTerminal tool to log on to the target system board.
Kernel processing terminal device driver module comprises hardware and line protocol (Line Discipline), As shown below (from the FIG. [APUE2e] ).
FIG 34.1 The terminal device module
Hardware driver is responsible for reading and writing the actual hardware devices such as read characters from the keyboard and to output to the display character, the line discipline like a filter for some special characters are not directly through it, but to do special processing, such as press Ctrl-Z on the keyboard, the corresponding character will not be the user program read
to read, but was intercepted line discipline, interpreted as a SIGTSTP
signal to the foreground process, generally makes the process stops. What characters should be filtered and line discipline to do what special treatment is configurable.
Input terminal and the output queue buffer, as shown (FIG from [APUE2e] ).
FIG 34.2. Terminal buffer
In the input queue, for example, by the line discipline of characters entered from the keyboard into the input queue after filtration, the user program to read the character in FIFO order from the queue, in general, when the input queue is full will be lost and then the input character , and the system will sound an alarm. The terminal may be configured to echo (the Echo)Mode. In this mode, the input queue for each character not only to the user program is also sent to the output queue, so we in the command line, type character, which not only can be read by the program, we can also screen see the echo of the character.
Now let's terminal login process:
1, when the system starts, init
the process according to the configuration files /etc/inittab
needed to determine which terminal is opened. For example configuration file contains this line:
1:2345:respawn:/sbin/getty 9600 tty1
And /etc/passwd
the like, with each field :
separated by number. 1 is the id of the beginning of the configuration of the line, and usually tty
the same suffix, configure tty2
the line should be the id of 2. The second field 2345 indicates the operating level 2 to 5 perform this configuration. The last field /sbin/getty 9600 tty1
is the init
process to fork
/ exec
command, open a terminal /dev/tty1
, is 9600 baud (baud rate only on serial port and terminal meaningful Modem), then prompts the user to enter account. The middle of the respawn
field indicates the init
process will monitor the getty
operation status of the process, once the process is terminated, init
will again fork
/ exec
command, so we Log from the terminal you will be prompted to enter the account again.
Some of the new Linux distributions do not have /etc/inittab
this configuration files, such as Ubuntu with /etc/event.d
configuration files in the directory to configure init
.
2, getty
according to the command line parameters to open the terminal as its control terminal, the control terminal point file descriptors 0, 1, and prompts the user account. After the user enters the account, getty
the task is complete, it re-execute login
the program:
execle("/bin/login", "login", "-p", username, (char *)0, envp);
3, login
the program prompts the user to enter a password (input password during the off terminal echo), and then verify the correctness of the account password. If the password is correct, the login
process terminates, init
re fork
/ exec
a getty
process. If the password is correct, the login
program set up some environment variables, set the home directory for the user's current working directory, and then execute Shell:
execl("/bin/bash", "-bash", (char *)0);
Note that argv[0]
the parameters of the program name plus a front -
, so bash
you know they are launched as a login Shell, Shell's startup scripts to perform the login. From getty
start exec
to login
, and then exec
to bash
, in fact, is the same process, the control terminal has not changed, still pointing to the file descriptor 0,1,2 control terminal. Since fork
copies PCB information, so other processes started by Shell also true.
Network login process
The number of virtual terminal or serial terminal is limited, virtual terminal in general is /dev/tty1
- /dev/tty6
six, the number of serial terminal does not exceed the number of serial ports. However, the number of network terminals or graphic terminal window is unrestricted, this is accomplished by pseudo-terminal (Pseudo TTY)Achieved. A device composed of a master pseudo-terminal (PTY Master)And a slave device (PTY Slave)composition. The equivalent of the master keyboard and monitor in concept, but it's not really a hardware but a kernel module, its operation is not a user but another process. And from the device described above /dev/tty1
is similar to this terminal block, but its underlying hardware driver is not accessible but access to the master device. By Example 34.1, "view corresponding to the terminal device filename" you can see the results, Shell process network terminal or the graphic terminal window and it starts the other processes will think their own control terminal is a pseudo-terminal device from, for example /dev/pts/0
, /dev/pts/1
and so on. Below telnet
an example using a pseudo terminal and network login process (FIG from [APUE2e] ).
FIG 34.3. Pseudo-terminal
-
Users by
telnet
connecting to the server client. If the server is configured as a standalone (Standalone)模式,则在服务器监听连接请求是一个telnetd
进程,它fork
出一个telnetd
子进程来服务客户端,父进程仍监听其它连接请求。另外一种可能是服务器端由系统服务程序
inetd
或xinetd
监听连接请求,inetd
称为Internet Super-Server,它监听系统中的多个网络服务端口,如果连接请求的端口号和telnet
服务端口号一致,则fork
/exec
一个telnetd
子进程来服务客户端。xinetd
是inetd
的升级版本,配置更为灵活。 -
telnetd
子进程打开一个伪终端设备,然后再经过fork
一分为二:父进程操作伪终端主设备,子进程将伪终端从设备作为它的控制终端,并且将文件描述符0、1、2指向控制终端,二者通过伪终端通信,父进程还负责和telnet
客户端通信,而子进程负责用户的登录过程,提示输入帐号,然后调用exec
变成login
进程,提示输入密码,然后调用exec
变成Shell进程。这个Shell进程认为自己的控制终端是伪终端从设备,伪终端主设备可以看作键盘显示器等硬件,而操作这个伪终端的“用户”就是父进程telnetd
。 -
当用户输入命令时,
telnet
客户端将用户输入的字符通过网络发给telnetd
服务器,由telnetd
服务器代表用户将这些字符输入伪终端。Shell进程并不知道自己连接的是伪终端而不是真正的键盘显示器,也不知道操作终端的“用户”其实是telnetd
服务器而不是真正的用户。Shell仍然解释执行命令,将标准输出和标准错误输出写到终端设备,这些数据最终由telnetd
服务器发回给telnet
客户端,然后显示给用户看。
If the telnet
network between the client and server delay is large, we observed that after pressing a key for a few seconds to return to the screen. This shows that we press a key every telnet
client will be immediately sent to the server the character, then this character through the pseudo-terminal master device and are read from the device after the Shell process, while echoing from the pseudo-terminal device, echoing the character again after the master pseudo-terminal device, telnetd
server and network back to telnet
the client, shown to the user. You might be surprised, but really this: Each time you press a key should go back and forth on the web!
BSD UNIX in series /dev
to create the directory number ptyXX
and ttyXX
device files, XX
letters and numbers, ptyXX
a master device corresponding to ttyXX
that from the number of devices, pseudo terminal depends kernel configuration. In the SYS V Series UNIX, pseudo-terminal device is a master /dev/ptmx
, "mx" represents Multiplex, Meaning that a plurality of master file multiplexed with a device, open each time /dev/ptmx
, the kernel allocates a master device, while /dev/pts
creating a file from the directory device, when it is closed from the terminal /dev/pts
deletes the corresponding file from the directory device. Linux supports both the pseudo-terminal, the current standard tend to pseudo-terminal SYS V's.