Reprinted very good article about terminal

Original link: http://zyan.cc/book/linux_c/html/ch34s01.html

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 forkwill 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/ttyaccess its control terminal. In fact each terminal device corresponds to a different device file, /dev/ttyprovides a common interface, a process to access it either through the control terminal /dev/ttycan also be accessed through the device file corresponding to the terminal device. ttynameFunction 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 telnetor sshrun 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/tty0represents the current virtual terminal, such as switching to a character terminal Ctrl-Alt-F1's /dev/tty0says /dev/tty1, when the terminal switches to the character of Ctrl-Alt-F2 /dev/tty0says /dev/tty2, just /dev/ttyas 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/ttyS1like the host and target connection together with a serial line, it can be passed over the host Linux the minicomor 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

Terminal 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 readto read, but was intercepted line discipline, interpreted as a SIGTSTPsignal 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

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, initthe process according to the configuration files /etc/inittabneeded to determine which terminal is opened. For example configuration file contains this line:

1:2345:respawn:/sbin/getty 9600 tty1

And /etc/passwdthe like, with each field :separated by number. 1 is the id of the beginning of the configuration of the line, and usually ttythe same suffix, configure tty2the 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 tty1is the initprocess to fork/ execcommand, 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 respawnfield indicates the initprocess will monitor the gettyoperation status of the process, once the process is terminated, initwill again fork/ execcommand, 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/inittabthis configuration files, such as Ubuntu with /etc/event.dconfiguration files in the directory to configure init.

2, gettyaccording 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, gettythe task is complete, it re-execute loginthe program:

execle("/bin/login", "login", "-p", username, (char *)0, envp);

3, loginthe 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 loginprocess terminates, initre fork/ execa gettyprocess. If the password is correct, the loginprogram 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 bashyou know they are launched as a login Shell, Shell's startup scripts to perform the login. From gettystart execto login, and then execto 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 forkcopies 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/tty6six, 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/tty1is 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/1and so on. Below telnetan example using a pseudo terminal and network login process (FIG from [APUE2e] ).

FIG 34.3. Pseudo-terminal

Pseudo-terminal

 

  1. Users by telnetconnecting to the server client. If the server is configured as a standalone (Standalone)模式,则在服务器监听连接请求是一个telnetd进程,它fork出一个telnetd子进程来服务客户端,父进程仍监听其它连接请求。

    另外一种可能是服务器端由系统服务程序inetdxinetd监听连接请求,inetd称为Internet Super-Server,它监听系统中的多个网络服务端口,如果连接请求的端口号和telnet服务端口号一致,则fork/exec一个telnetd子进程来服务客户端。xinetdinetd的升级版本,配置更为灵活。

  2. telnetd子进程打开一个伪终端设备,然后再经过fork一分为二:父进程操作伪终端主设备,子进程将伪终端从设备作为它的控制终端,并且将文件描述符0、1、2指向控制终端,二者通过伪终端通信,父进程还负责和telnet客户端通信,而子进程负责用户的登录过程,提示输入帐号,然后调用exec变成login进程,提示输入密码,然后调用exec变成Shell进程。这个Shell进程认为自己的控制终端是伪终端从设备,伪终端主设备可以看作键盘显示器等硬件,而操作这个伪终端的“用户”就是父进程telnetd

  3. 当用户输入命令时,telnet客户端将用户输入的字符通过网络发给telnetd服务器,由telnetd服务器代表用户将这些字符输入伪终端。Shell进程并不知道自己连接的是伪终端而不是真正的键盘显示器,也不知道操作终端的“用户”其实是telnetd服务器而不是真正的用户。Shell仍然解释执行命令,将标准输出和标准错误输出写到终端设备,这些数据最终由telnetd服务器发回给telnet客户端,然后显示给用户看。

If the telnetnetwork 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 telnetclient 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, telnetdserver and network back to telnetthe 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 /devto create the directory number ptyXXand ttyXXdevice files, XXletters and numbers, ptyXXa master device corresponding to ttyXXthat 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/ptscreating a file from the directory device, when it is closed from the terminal /dev/ptsdeletes the corresponding file from the directory device. Linux supports both the pseudo-terminal, the current standard tend to pseudo-terminal SYS V's.

Guess you like

Origin blog.csdn.net/d13579246810b/article/details/102728568