Terminal I/O

终端Terminal是什么?

简单来,Terminal就是机器作用终结的地方,机器的边缘,人与机器进行交流的接口。由此,在生活中常见的有三种终端:

  1. 本地终端:通过VGA\HTMA连接显示器,USB连接键盘,键盘+显示器的组合就是一个本地终端
  2. 用串口连接的远程终端:通过串口线把主机接到另外一个有显示器和键盘的主机,通过运行一个终端模拟程序—terminal emulator,比如***“Windows超级终端”***来将这台主机的显示器和键盘借给串口对端的主机。
  3. 用TCP/IP等网络进行连接的远程终端:不同于前两类和物理设备直接相连的物理终端,它通过网络连接,不涉及物理设备的连接,因此也叫做伪终端、虚拟终端,例如SSH这样的。

常见的终端有哪些?

Overview

比较传统的Unix terminal如今并不常见,相反X Windows System变成最常用的接口。以前众多设备都是直接连接到电脑上,现在会通过网络来进行连接。在实际的terminal programming当中,需要结合对应的software terminal emulators。

Terminal I/O has two modes:

  1. Canonical mode(规范模式,默认) input processing. In this mode, terminal input is processed as lines. The terminal driver returns at most one line per read request.
  2. Noncanonical mode input processing. The input characters are not assembled into lines.

Shell当中操作stdin和stdout时,默认都是Canonical mode,以行为单位进行读取和写入。 而对于vim这样的全屏操作工具,因为需要输入一些单个的控制字符,所以不能以行为单位来操作,所以处于Noncanonical mode.


每个terminal通常都是由内核当中terminal driver操作,对应的都会有Input queue和Output queue。其次,图中可以看到如果echoing enabled,那么terminal driver会将Input queue当中的内容copy到Output queue这边,使得可以被Terminal输出。

https://img-bed-l.oss-cn-beijing.aliyuncs.com/pic_bed/image-20220101155354555.png

https://img-bed-l.oss-cn-beijing.aliyuncs.com/pic_bed/image-20220101152028396.png