认识 Linux 终端

在 Linux 上打开一个终端界面,我们会看到一个黑色窗口,这个黑色窗口上的画面是由 terminal emulator 直接维护的。terminal emulator 在被打开时往往会顺带打开一个子进程 bash 或者其他 shell,而黑色窗口上的内容就是 bash 的输出。terminal emulator 和 bash 直接使用 pts(伪终端) 设备通信。

伪终端设备成对出现,分为 master 和 slave,在 linux 上分别称为 /dev/ptmx 和 /dev/pts/x。bash 的 stdin/stdout/stderr fd 均指向 /dev/pts/x slave 设备。bash 读 stdin 实际上读到的是 terminal emulator 向 /dev/ptmx 中写入的字符,bash 写 stdout 实际写入 /dev/pts/x,这些内容被 Linux tty 层传递到 /dev/ptmx 由 terminal emulator 读出并显示到交互窗口上。键盘输入会被 terminal emulator 接收并写入 /dev/ptmx,这些数据不仅会被传递到 /dev/pts/x 由 bash 作为输入,也会在内核 tty 层被 echo(echo 与否由 termios 配置决定),重新回到 /dev/ptmx 读端,使得键盘输入的内容也被显示到"黑色窗口"上。

上述内容可以被一些指令输出印证。

bash 复制代码
man 7 pty

查看当前进程的 fd

bash 复制代码
harry@Harry:~$ ls -l /proc/$$/fd
total 0
lrwx------ 1 harry harry 64 Mar 17 17:11 0 -> /dev/pts/9
lrwx------ 1 harry harry 64 Mar 17 17:11 1 -> /dev/pts/9
lrwx------ 1 harry harry 64 Mar 17 17:11 2 -> /dev/pts/9
lrwx------ 1 harry harry 64 Mar 17 17:11 255 -> /dev/pts/9
lrwx------ 1 harry harry 64 Mar 17 17:11 7 -> /dev/ptmx

查看父进程的 fd

bash 复制代码
harry@Harry:~$ sudo ls -l /proc/$PPID/fd
total 0
lrwx------ 1 root root 64 Mar 17 20:21 0 -> /dev/null
lrwx------ 1 root root 64 Mar 17 20:21 1 -> /dev/null
lrwx------ 1 root root 64 Mar 17 20:21 10 -> 'socket:[98106]'
lrwx------ 1 root root 64 Mar 17 20:21 11 -> 'socket:[99936]'
lr-x------ 1 root root 64 Mar 17 20:21 12 -> 'mnt:[4026532464]'
lrwx------ 1 root root 64 Mar 17 20:21 13 -> 'socket:[99931]'
lrwx------ 1 root root 64 Mar 17 20:21 14 -> 'socket:[99932]'
lrwx------ 1 root root 64 Mar 17 20:21 15 -> 'socket:[99933]'
lrwx------ 1 root root 64 Mar 17 20:21 16 -> 'socket:[99934]'
lrwx------ 1 root root 64 Mar 17 20:21 17 -> 'socket:[99935]'
lrwx------ 1 root root 64 Mar 17 20:21 18 -> /dev/ptmx # one of pty master
lrwx------ 1 root root 64 Mar 17 20:21 19 -> 'anon_inode:[signalfd]'
l-wx------ 1 root root 64 Mar 17 20:21 2 -> /dev/kmsg
lrwx------ 1 root root 64 Mar 17 20:21 20 -> /dev/ptmx
l-wx------ 1 root root 64 Mar 17 20:21 3 -> /dev/hvc1
lr-x------ 1 root root 64 Mar 17 20:21 4 -> 'mnt:[4026532219]'
lrwx------ 1 root root 64 Mar 17 20:21 5 -> 'socket:[101332]'
lrwx------ 1 root root 64 Mar 17 20:21 6 -> 'socket:[12298]'
lrwx------ 1 root root 64 Mar 17 20:21 7 -> /dev/ptmx
lrwx------ 1 root root 64 Mar 17 20:21 8 -> 'socket:[12304]'
lrwx------ 1 root root 64 Mar 17 20:21 9 -> 'socket:[12305]'
相关推荐
HLC++10 小时前
Linux的进程间通信
android·linux·服务器
华清远见IT开放实验室11 小时前
实验室建设案例 | 石家庄科技信息职业学院嵌入式实验室——从底层硬件到系统应用,一所应用型高校的嵌入式人才培养这样落地
linux·arm开发·stm32·嵌入式硬件·高校·实验室建设
groundhappy14 小时前
idalib安装和codex ida-mcp配置
linux·开发语言·python
通信小小昕15 小时前
Ubuntu 26.04 中文输入法安装
linux·运维·ubuntu
张小姐的猫16 小时前
【Linux】网络编程 —— HTTP协议(上)
linux·运维·服务器·网络·http·单例模式·策略模式
栩栩云生16 小时前
AI 写代码犯的错,早被写进了错题集
linux·安全·ai编程
imc.1117 小时前
linux基础IO
linux·运维·服务器
BelongPanda19 小时前
Linux Nginx 纯手动 Let‘s Encrypt 泛域名证书配置教程
linux·nginx
酷可达拉斯20 小时前
Linux操作系统-shell编程(0)
linux·运维·服务器·python·云计算
2301_7779983420 小时前
Linux中断机制:操作系统如何高效运行
linux·运维·服务器