The concept of Linux programming learning notes----terminal and serial programming basis

Source: Internet
Author: User
Tags telnet program

Reprint please indicate the source, thank you!

The related concepts of terminal and serial port under Linux are:

TTY, console, virtual terminal, serial, console (console terminal) detailed

Part of the content is organized in the network.

Terminals/Consoles

Neither the terminal nor the console is the concept of personal computers, but the concept of a small, medium-sized mainframe computer shared by many people.

1. Terminal

A host, even a lot of terminals, the terminal for the host to provide a human-computer interface, everyone through the terminal using the host resources. The terminal has two kinds of character dumb terminal and graphic terminal.

Console is another human interface, not through the terminal and connected to the host, but through the display card-display and keyboard interface respectively connected to the host, which is the human control of the host's first man-machine interface.
and PC only console, no terminal. Of course, you can connect the one or two-character dumb terminal on the serial port. But Linux to the POSIX standard to use the personal computer as a minicomputer, then on the console through the Getty software Virtual Six Characters dummy terminal (or call console terminal Tty1-tty6) (the number can be adjusted in/etc/inittab) and a graphic terminal, In the virtual graphics terminal can also through the software (such as RXVT) and then virtual unlimited number of virtual characters dumb terminal (pts/0 ...). Remember, it's all virtual, it's the same, but it's not.
So on a personal computer, there is only one actual console, no terminal, and all terminals are simulated with software on the console.
The personal computer when the host and then through the serial port or network card even the real physical terminal can also, but because the real physical terminal is not cheaper than the PC itself, generally no one to do so.

2. Console

Like other Unix class systems, Linux itself is based on the command line. Try "Ctrl" + "Alt" + "f[1-7]". This is the console , the true nature of Linux. As for the use of the method, in addition to the multi-login logout, the other operation and we are in the Linux graphical Interface (X-window) terminal operation is the same, when the X-window problem or do not run X-window, the operation is mainly done here.
Linux provides more than one (character-dumb) terminal under the console, enabling multiple users to log in at the same time, including logging on at the same time. The console "Alt" + "Fx" is capable of switching to the X (character dummy) terminal. If you need to jump from X-window to the first (character dumb) terminal, you need "Ctrl" + "Alt" + "Fx". In general, if you want to return Xwindow from the console, "" Alt "+7" can be returned to the Xwind graphical interface. (The Linux distribution provides 7 virtual screens, the 1~6 is the console terminal ((character mute) terminal), the 7th one runs X-window. )

3. Terminal Equipment Type

Linux has a variety of terminal equipment types, including the current terminal, front-end control terminal, serial port, virtual terminal master and slave equipment.

My OS such as: Use the Cat/proc/tty/drivers command.

These endpoints are managed by the TTY to shield the hardware implementation while mapping these devices into files that can be accessed and manipulated using file-managed I/O functions.

4. Console Terminal

Control Terminal (/dev/tty) This is a concept in the application, the foreground process has a control terminal, corresponding to this. However, it does not refer to any physical terminal, in fact/dev/tty will be mapped to the current device (through the TTY command can be seen), such as if you are in the console interface (that is, under the character interface) then Dev/tty is mapped to dev/ Tty1-6 between the two (depending on your current console number), but if you are now in the graphical interface (xwindows), then you will find that the current/dev/tty map to the/dev/pts pseudo terminal. For example, you can enter a command #tty then display the current mapping terminal such as:/dev/tty1 or/dev/pts/0, etc.


Console Terminal (/dev/ttyn,/dev/console) in UNIX systems, computer monitors are often referred to as console terminals (consoles). It simulates a type of Linux terminal (Term=linux), and there are some device special files associated with it:tty0, Tty1, Tty2 , and so on. When you log in on the console, you are using Tty1. When using ALT+[F1-F6], we can switch to Tty2, Tty3, and so on. Tty1–tty6 is called a virtual terminal, and Tty0 is an alias of the virtual terminal that is currently used, and the information generated by the system is sent to the terminal. Therefore, regardless of which virtual terminal is currently in use, the system information is sent to the console terminal.
You can log in to a different virtual terminal, which allows the system to have several different session periods at the same time. Only system or Superuser root can write to/dev/tty0

The information is as follows:

The console is a buffering concept that is actually provided for the kernel to print. Our PC, the terminal commonly used is the display and the keyboard composition, our user print and the kernel print all from this terminal to reflect to the user. So, here,/dev/console is connected to/dev/tty0, in fact there are 2 concepts, console and TTY this 2, how to achieve, in fact, console this structure has a device, here is actually tty0 corresponding to a virtual terminal equipment. If, we have a specialized printing kernel device (for example, through the serial port), we put that serial port Register_console, then/dev/console to this serial device. At this time, the kernel printing to this serial device, and the user's printing is related to the above/dev/tty, if the/dev/tty corresponds to/dev/tty0, then the user Print in the window appears. So /dev/console is used to add the console.

5. Serial port terminal (/DEV/TTYSN)

The serial port terminal (Serial Port Terminal) is a terminal device connected using a computer serial port. The computer sees each serial port as a character device. For a while these serial port devices are often referred to as end-device, because it is used to connect the terminal at that time. The serial port corresponding to the device name is/dev/tts/0 (or/DEV/TTYS0),/DEV/TTS/1 (or/dev/ttys1), etc., the device number is (4,0), (4,1), respectively, corresponding to the DOS system COM1, COM2 and so on. To send data to a port, you can redirect standard output to these special file names on the command line. For example, at the command-line prompt, type: echotest >/dev/ttys1 will send the word "test" to the device connected to the TtyS1 (COM2) port. Can be connected to the serial port to experiment.

That is relative and DOS under the COM1 and COM2, etc...

6. Pseudo-terminal (/dev/pty/)

This is the development of the terminal, in order to meet the current needs (such as the Network Landing, Xwindow window management). Pseudo-terminal (Pseudo Terminal) is a paired logical terminal device, such as/DEV/PTYP3 and/DEV/TTYP3 (or/dev/pty/m3 and/DEV/PTY/S3 in the device file system, respectively). They are not directly related to the actual physical device. If a program considers TTYP3 as a serial port device, its read/write operation on the port is reflected in the other upper (TTYP3) of the logical terminal pair. The TTYP3 is a logical device that another program uses for read and write operations. In this way, two programs can communicate with each other through this logical device, and one of the programs that uses TTYP3 thinks that it is communicating with a serial port. This is like a pipe operation between logical device pairs. For TTYP3 (S3), any program designed to use a serial port device can use the logical device. However, for programs that use PTYP3, you need to specifically design a ptyp3 (m3) logical device.

For example, if someone uses the Telnet program to connect to your computer on the Internet, the Telnet program may start connecting to the device Ptyp2 (m2) (on a pseudo-terminal port). At this point a Getty program should run on the corresponding TTYP2 (S2) port. When Telnet obtains a character from the far end, the character is passed to the Getty program via M2, S2, and the Getty program returns the "login:" String information to the network via S2, M2, and Telnet programs. In this way, the login program communicates with the Telnet program through a "pseudo-terminal". By using the appropriate software, two or more pseudo-terminal devices can be connected to the same physical serial port.

Prior to the use of device filesystem, HP-UX AIX used more complex file name naming methods in order to obtain a large number of special files for pseudo-terminal devices.

7. Virtual Terminal

In most cases, using Linux is a way to connect to the server over a network. If you are running commands in the image interface of the server host, you typically need to open a virtual terminal window, which is the type of terminal that is displayed on the server as a virtual network terminal. Complete the virtual network terminal with two virtual devices:/dev/ptmx/&/dev/pts/

/DEV/PTMX Virtual Appliance

is a character file that is used to create a virtual terminal for the device's master and salve paired devices. To open an unused virtual terminal, use the call POSIX_OPENPT () function, open the device/dev/ptmx/each open will return a separate master (master) device file descriptor, and can find the corresponding salve device, and the salve device will be in/ Created under the dev/pts/directory.

/dev/pts/Virtual Terminal

Through the network Telnet to the Linux host or xwindows environment open terminal, will be in the/dev/pts/directory to create a virtual network terminal, the corresponding exit a virtual terminal, device information as follows:


Summary

First: Two modes: Character mode: corresponding to the console, the device file is/dev/tty1-6 (is a console terminal device files), and/dev/tty0 is the current terminal equipment file alias (I think can be said to be linked bar, that is, tty0 refers to the current device files). Graphics mode (Xwindow): corresponding to the Tty7, but in the Xwindow terminal (that is, the window of the input command) corresponds to the device file is/dev/pts/0-(is a pseudo-terminal device file)

About Dev/tty is actually very simple: is a link to the current terminal equipment (a bit like/dev/tty0 bar, but he seems more powerful), we can do this experiment:

Open multiple terminals under Xwindow (Enter the command window): #tty命令, found in each window displayed as # dev/pts/0 #dev/pts/1 #dev/pts/2 ....

Then enter the echo "test" in the #dev/pts/0 terminal to display test >/dev/pts/0 results. Then you enter the echo "test" >/dev/tty in the #dev/pts/0 terminal and you will see that the test string is also displayed in the current window. This means that Dev/tty is actually a link to the current device file.


About/dev/console should be more like a buffer result, to achieve the kernel of the printing, such as the kernel to print the content into the buffer, and then by the console to decide where to print (such as tty0 or serial port, etc.). So /dev/console is used to add the console







Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.