The most detailed illustration of Linux terminal and Line discipline

小云云
Release: 2017-12-18 11:17:51
Original
5133 people have browsed it

Line discipline, there is no unified standard whether it should be translated into line discipline or line discipline. I choose line discipline, but this does not mean that the unit of operation is one line. This is hereby clarified. In this article, we will share with you the most detailed graphic tutorial on Linux terminal and Line discipline, hoping to help you.

What is a line procedure

When we press the key "l" on a terminal, the terminal just changes the letter "l" The echo came back, and then the key "s" was pressed, and the letter "s" was still echoed. Then we pressed the Enter key, and the echo was no longer the Enter key (how to echo it), but the column All files in the current directory are output and displayed. How are these rules defined?

- When we press the key combination "Ctrl-C", the current process will be terminated. How is this specified? Why not the key combination "Shift-B" to accomplish the same thing?

If you know the keyboard well enough (I know it, but not enough), you will know that there are many key combinations that can be used. These key combinations are used in different systems and different systems. How is it defined on the terminal, and who determines what happens when a key is pressed?

 All these questions can be answered using rules. So what are the rules of conduct?

 Hong Kong rules are a set of agreed-upon agreements. The agreed parties can be computers and terminals (including output devices and human input devices). In this sense, the terminal is the so-called human-machine interface.

Let me explain briefly. Line procedures stipulate the behavioral specifications between input and output devices such as keyboards, serial ports, printers, and monitors, and programs such as user-mode Shells. Key events on the keyboard are interpreted by line procedures into input that the Shell can understand and provide corresponding output. This set of procedures is essential for people to operate computers. It actually stipulates the specifications for information entering the computer from the outside.

You can use the stty command to display the configuration of your terminal line discipline:

The most detailed illustration of Linux terminal and Line discipline


Understanding line procedures

Taking Linux as an example, the following figure shows the position of line procedures in the entire system structure:

The most detailed illustration of Linux terminal and Line discipline

We can see that the direction of information flow is a vertical path, with a computer program at one end and some kind of hardware at the other end (there is also the concept of pseudo-terminal here, see Thorough Understanding of Various Terminal Types and Concepts of Linux ), for comparison, let’s take a look at the data path in another direction, that is, the horizontal path. A typical example is the TCP/IP protocol stack:

The most detailed illustration of Linux terminal and Line discipline

TCP/IP protocol communication is completely different from terminal protocol. Terminal protocol is completely a vertical protocol, while TCP/IP focuses on horizontal peer-layer communication. It is different from terminal protocol as a human-machine interface. , the more important role of TCP/IP is to handle communication between any processes. But taking away this difference in the direction of the data path, the remaining things are consistent, that is, they are all some kind of conventional agreements, and the only difference is whether the two parties to the agreement are homogeneous and equal.

The location of SLIP

As a comprehensive example, let’s look at the content of SLIP.

SLIP is Serial Line Internet Protocol (Serial Line Internet Protocol). Maybe few people use it anymore. After all, now it’s all about Ethernet and fiber optics. Who still wants to use it? A serial port line will be used to transmit network data packets.

 But it has existed as a dedicated link connecting hosts running the TCP/IP protocol for a long time.

## We know that TCP/IP is a peer-to-peer communication protocol, but the final data packet has to be transmitted through some physical medium, so a vertical protocol is needed. Peer layer communication is enabled. We call the horizontal peer layer protocol a communication protocol, and the vertical protocol a transmission protocol. The line protocol is actually a transmission protocol. SLIP is actually a line protocol. The SLIP line protocol interprets the characters transmitted on the serial port line. into IP datagrams and delivered upwards. The relationship between this procedure and TCP/IP is as follows:

The most detailed illustration of Linux terminal and Line discipline

It can be seen that SLIP, as a Bank of China procedure, does not continue to submit the parsed data to the TTY buffer bound to it, but directly delivers the parsed data as an IP datagram to The TCP/IP protocol stack is used to handle it.

##   Here is an excerpt from Baidu Encyclopedia:

<span style="font-size: 16px;">SLIP只是一个包组帧协议,仅仅定义了在串行线路上将数据包封装成帧的一系列字符。它没有提供寻址、包类型标识、错误检查/修正或者压缩机制。<br></span>
Copy after login
Copy after login

Broadly speaking, in fact, something like the Ethernet specification can also be called A certain kind of protocol, after all, it is also a protocol that stipulates behavioral specifications between IP layer software and transmission media. It plays a role in data formatting. However, because today's ultra-high-speed transmission has long been completely based on bit stream sequences, Data is no longer encapsulated by defining blocks bounded by characters, so it is a bit unclear to call it a line protocol, but it is essentially the same. It is a protocol that defines how to encapsulate data packets on a certain medium.


About unix98 pseudo-terminal

For this part of the pseudo-terminal, please refer to: How SVR4/4.3BSD and Linux treat pseudo-terminals Different ways


#Global perspective illustration of several terminals

Okay, it’s time to summarize.

 In the previous article, I thoroughly understood the various terminal types and concepts of Linux. I used a ring method to give a general diagram of the terminal. Later, someone sent me an email to reflect , all terminal types enter one picture. Although it is very unified, it is too complicated, so in this article I decided to separate these, omit more details, and look at the structure of the terminal from a global perspective.

 From the perspective of bash, bash itself does not know whether its terminal is directly connected, serial port connected, or connected via SSH. This means that there is an appropriate terminal in the kernel. Matching layer, above this layer, all terminals look exactly the same, but below this layer, they show their individuality:

The most detailed illustration of Linux terminal and Line discipline

##We Let’s take a look at these differences next.

Local terminal

The most detailed illustration of Linux terminal and Line discipline

Terminal connected by serial cable

The most detailed illustration of Linux terminal and Line discipline

SSH pseudo-terminal

The most detailed illustration of Linux terminal and Line discipline

Tmux pseudo-terminal

Originally I wanted to talk about Tmux and Screen together, but the principles of Screen and Tmux are almost exactly the same, so I omitted Screen and only talked about Tmux here:

The most detailed illustration of Linux terminal and Line discipline

It’s still a bit complicated, isn’t it?

 It is actually quite complicated. You only need to know two points here. First, just treat Tmux Server and its Fork as an uninterrupted SSH. Secondly, when you When an SSH pseudo-terminal runs Tmux Client, Tmux Client is equivalent to lending the pts of the SSH pseudo-terminal to Tmux Server. Tmux Server and SSHd are parallel.

  • Tmux Server opens a pair of pseudo terminals, holds the main device itself, and inherits the secondary device to the Bash it Forks out. This pair of processes enters the background. No longer belongs to any terminal.

The most detailed illustration of Linux terminal and Line discipline

  • Once Tmux Client runs in an SSH terminal, it will store the pts of the current terminal Passed to Tmux Server, allowing Tmux Server to act as a data proxy to pass input and output.

The most detailed illustration of Linux terminal and Line discipline

Once Tmux Client is running, it becomes the foreground process of the current Bash. After redirection, the current Tmux Client It becomes the front end of Bash0, receiving the input and output of Bash0. The process after the file handle is transferred is as follows:

<span style="font-size: 16px;">1. 有人在远端的Windows主机上敲入一个字符***“a”***;2. 字符***“a”***经由SSH客户端加密后传输到Linux SSH服务器SSHd并解密;3. 字符***“a”***通过SSHd的ptmx写入4. Tmux Server从pts/2将字符***“a”***读出并写入ptmx;5. Bash0将字符***“a”***从pts/1读出并执行;6. Bash0将***-bash: a: command not found***按原路返回给Windows。<br></span>
Copy after login
Copy after login

注释:关于文件句柄传递

有人说Linux可以靠一个ioctl系统调用在不同进程之间传递文件描述符,然而当你真的去自己尝试时,却发现这是骗人的。那么同样的功能如何来实现?你如何不通过调用open,accept,socket这类系统调用打开一个文件句柄呢?

  答案就是使用SCM_RIGHTS。具体怎么做,百度一下,你就知道。


题外话

最后,依然说点题外话。

  现如今关于终端等人机接口的内容被淡化了,然而关于TCP/IP的内容却得到了强化,我认为这是不公平的。

  当然,这是近来不断提高软件地位而降低硬件地位的后果,大多数人都知道socket如何到socket,却只有很少的人知道socket如何到网线,而后者才是基础。数据从哪里来?

  没有数据何谈通信?数据不可能凭空就存在于计算机,它必然靠某种设备从外界输入,比如键盘,扫描仪,传感器等,首先这些设备和计算机之间的数据传输协议才基础中的基础,此后针对数据的加工才会用到TCP/IP,IPC这种对等层通信协议。

  随着物联网的发展,关于数据如何进入计算机这个问题还会有很多新的解答,因此以上的局面得以改观。

牢骚

———————–截止2017/12/16 11:08文章已写完———————–

东北极寒天吃饭,要乱炖多点肉喝点酒;河南的冷天吃饭,要吃大烩菜,一人盛一碗,陕西山西冷天吃饭,羊排锅加竹叶青酒超爽…但是现在在华南,深圳的今天气温比较低,这天气说冷不冷,但确实有点寒意,我决定今天穿上长裤…准备出发去吃好爽的火锅底料。
  华南的深圳,北纬22°不到23°,这是热带的边缘,正因为其地理因素,冬季会受到北方冷空气的影响,所以这里被归为一个叫做亚热带的尴尬区域,所谓亚热带主要就是为了照顾北方的冬天,北方人不是怕冷吗?那就为北方制造一个叫做亚热带的地方,可谓仁慈。不管怎样,我现在依然还是短衣短裤的…我是迫于众人的目光压力穿上长裤的,不然会被认为是傻逼,但是在室内或者刚吃完饭后,真觉得热…唉!

昨晚的圣诞晚会嗨爆全场,灯光音响很棒,然而最终还是没有中奖…回到家已经午夜,喝了一瓶真露想再写篇关于终端的随笔以解惑,但不知不觉就困了,于是就睡了,早上本来想早起,自然醒来已经七点半了,醒来并没有意识到今天很冷,第一件事反而是想中午一家人去吃顿川味火锅底料,这也算是响应老板们的号召了。所以说,我必须在11点前把这篇文章写完。

  Line discipline,到底翻译成行规程还是线路规程,没有统一的标准,我选用行规程,但这并不意味着操作的单位就是一行,特此澄清。

什么是行规程

当我们在一个终端上按下按键“l”的时候,终端只是把字母“l”回显了回来,紧接着按下按键“s”,依然是回显字母“s”,随后我们按下回车键,回显的不再是回车键(请问怎么回显),而是列出并显示了当前目录下的所有文件,这些规则是如何定义的?

  当我们按下组合键“Ctrl-C”的时候,当前的进程就终止了,这个又是如何规定的?为什么不是组合键“Shift-B”来完成同样的事?

  如果你对键盘足够了解(我了解,但并不足够),就会知道有很多的组合键可以使用,这些组合键分别在不同的系统,不同的终端上如何定义的,谁来规定按下什么键发生什么事?

  所有这些问题都可以用行规程来回答。那么什么是行规程?

  行规程是一套约定俗成的协议。约定双方可以是计算机和终端(包括输出设备和人体输入设备)。在这个意义上,终端就是所谓的人机接口。

  我来简单解释一下。行规程规定了键盘,串口,打印机,显示器等输入输出设备和用户态Shell等程序之间的行为规范,键盘上的按键事件被行规程解释成了Shell可以理解的输入并给出相应的输出。人们要想操作计算机,这套规程是必不可少的,它事实上规定了信息从外部进入计算机的规范。

  可以使用stty命令来展示你的终端行规程的配置:

The most detailed illustration of Linux terminal and Line discipline


理解行规程

以Linux为例,下图是行规程在整个体系结构中的位置:

The most detailed illustration of Linux terminal and Line discipline

我们可以看出信息流的方向,是一个纵向的通路,一端是计算机程序,另一端是某种硬件(这里还有伪终端的概念,参见彻底理解Linux的各种终端类型以及概念),作为对比,我们来看看另外一种方向的数据通路,即横向的通路,典型的例子就是TCP/IP协议栈:

The most detailed illustration of Linux terminal and Line discipline

TCP/IP协议通信和终端行规程完全不同,终端行规程完全是一个纵向的协议,而TCP/IP则侧重于横向的对等层通信,和终端行规程作为人机接口不同的是,TCP/IP更重要的作用是处理任意进程之间的通信。但抽掉这种数据通路方向上的不同,剩下的东西它们就是一致的了,即它们都是某种约定俗成的协议,约定的双方是否同质对等造成了唯一的区别。

SLIP的位置

作为一个综合的例子,我们来看点关于SLIP的内容。

  SLIP就是Serial Line Internet Protocol(串行线路网际协议)可能现在很少有人再使用它了,毕竟现如今都是以太网和光纤的天下了,谁还会用串口线来传输网络数据包。

  但是它在以前很长一段时间一直作为连接运行TCP/IP协议的主机的专用链路存在的。

  我们知道,TCP/IP是对等层通信协议,但是最终的数据包不得不通过某种物理介质传输,因此还需要一种纵向的协议才可以让对等层通信得以实现。我们把横向的对等层协议叫做通信协议,而纵向的协议叫做传输协议,行规程事实上就是一种传输协议,SLIP实际上就是一种行规程,SLIP行规程把串口线上传输的字符解释成IP数据报文并向上递送。这种行规程和TCP/IP的关系如下所示:

The most detailed illustration of Linux terminal and Line discipline

It can be seen that SLIP, as a Bank of China procedure, does not continue to submit the parsed data to the TTY buffer bound to it, but directly delivers the parsed data as an IP datagram to The TCP/IP protocol stack is used to handle it.

##   Here is an excerpt from Baidu Encyclopedia:

<span style="font-size: 16px;">SLIP只是一个包组帧协议,仅仅定义了在串行线路上将数据包封装成帧的一系列字符。它没有提供寻址、包类型标识、错误检查/修正或者压缩机制。<br></span>
Copy after login
Copy after login

Broadly speaking, in fact, something like the Ethernet specification can also be called A certain kind of protocol, after all, it is also a protocol that stipulates behavioral specifications between IP layer software and transmission media. It plays a role in data formatting. However, because today's ultra-high-speed transmission has long been completely based on bit stream sequences, Data is no longer encapsulated by defining blocks bounded by characters, so it is a bit unclear to call it a line protocol, but it is essentially the same. It is a protocol that defines how to encapsulate data packets on a certain medium.


About unix98 pseudo-terminal

For this part of the pseudo-terminal, please refer to: How SVR4/4.3BSD and Linux treat pseudo-terminals Different ways


#Global perspective illustration of several terminals

Okay, it’s time to summarize.

 In the previous article, I thoroughly understood the various terminal types and concepts of Linux. I used a ring method to give a general diagram of the terminal. Later, someone sent me an email to reflect , all terminal types enter one picture. Although it is very unified, it is too complicated, so in this article I decided to separate these, omit more details, and look at the structure of the terminal from a global perspective.

 From the perspective of bash, bash itself does not know whether its terminal is directly connected, serial port connected, or connected via SSH. This means that there is an appropriate terminal in the kernel. Matching layer, above this layer, all terminals look exactly the same, but below this layer, they show their individuality:

The most detailed illustration of Linux terminal and Line discipline

##We Let’s take a look at these differences next.

Local terminal

The most detailed illustration of Linux terminal and Line discipline

Terminal connected by serial cable

The most detailed illustration of Linux terminal and Line discipline

SSH pseudo-terminal

The most detailed illustration of Linux terminal and Line discipline

Tmux pseudo-terminal

Originally I wanted to talk about Tmux and Screen together, but the principles of Screen and Tmux are almost exactly the same, so I omitted Screen and only talked about Tmux here:

The most detailed illustration of Linux terminal and Line discipline

It’s still a bit complicated, isn’t it?

 It is actually quite complicated. You only need to know two points here. First, just treat Tmux Server and its Fork as an uninterrupted SSH. Secondly, when you When an SSH pseudo-terminal runs Tmux Client, Tmux Client is equivalent to lending the pts of the SSH pseudo-terminal to Tmux Server. Tmux Server and SSHd are parallel.

  • Tmux Server opens a pair of pseudo terminals, holds the main device itself, and inherits the secondary device to the Bash it Forks out. This pair of processes enters the background. No longer belongs to any terminal.

The most detailed illustration of Linux terminal and Line discipline

  • Once Tmux Client runs in an SSH terminal, it will store the pts of the current terminal Passed to Tmux Server, allowing Tmux Server to act as a data proxy to pass input and output.

The most detailed illustration of Linux terminal and Line discipline

Once Tmux Client is running, it becomes the foreground process of the current Bash. After redirection, the current Tmux Client It becomes the front end of Bash0, receiving the input and output of Bash0. The process after the file handle is transferred is as follows:

<span style="font-size: 16px;">1. 有人在远端的Windows主机上敲入一个字符***“a”***;2. 字符***“a”***经由SSH客户端加密后传输到Linux SSH服务器SSHd并解密;3. 字符***“a”***通过SSHd的ptmx写入4. Tmux Server从pts/2将字符***“a”***读出并写入ptmx;5. Bash0将字符***“a”***从pts/1读出并执行;6. Bash0将***-bash: a: command not found***按原路返回给Windows。<br></span>
Copy after login
Copy after login

注释:关于文件句柄传递

有人说Linux可以靠一个ioctl系统调用在不同进程之间传递文件描述符,然而当你真的去自己尝试时,却发现这是骗人的。那么同样的功能如何来实现?你如何不通过调用open,accept,socket这类系统调用打开一个文件句柄呢?

  答案就是使用SCM_RIGHTS。具体怎么做,百度一下,你就知道。


题外话

最后,依然说点题外话。

  现如今关于终端等人机接口的内容被淡化了,然而关于TCP/IP的内容却得到了强化,我认为这是不公平的。

  当然,这是近来不断提高软件地位而降低硬件地位的后果,大多数人都知道socket如何到socket,却只有很少的人知道socket如何到网线,而后者才是基础。数据从哪里来?

  没有数据何谈通信?数据不可能凭空就存在于计算机,它必然靠某种设备从外界输入,比如键盘,扫描仪,传感器等,首先这些设备和计算机之间的数据传输协议才基础中的基础,此后针对数据的加工才会用到TCP/IP,IPC这种对等层通信协议。

  随着物联网的发展,关于数据如何进入计算机这个问题还会有很多新的解答,因此以上的局面得以改观。

相关推荐:

Linux终端类型的详解

Linux terminal Chinese displays garbled characters

Linux terminal learning one

The above is the detailed content of The most detailed illustration of Linux terminal and Line discipline. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!