5 Processes and the Shell (进程接口与 Shell)
把 shell 拆成两半看清楚:一半是文本到系统调用的翻译,一半是内核提供的进程原语。理解 fork 为什么返回两次、exec 为什么”成功时不返回”、dup2 的顺序为什么决定成败、管道为什么会因为一个没关的描述符而挂住;再回头看解析为什么也不简单。
English: Split the shell into two halves: text-to-syscall translation, and the process primitives the kernel provides. Why fork returns twice, why a successful exec never returns, why dup2 ordering decides correctness, and why an unclosed pipe descriptor hangs the reader.
5.1 Shell 是一个程序,不是系统的一部分 / The shell is a program
内核只提供约二十个进程相关的系统调用;shell 是普通用户态程序,它把这些调用组合成”命令”这个抽象。ls -l | grep log > out 这一行之所以能工作,是因为 shell 自己做了三件事:分词、创建进程、安排描述符。
这也解释了一个常见困惑:为什么换个 shell(bash → zsh → fish)系统就”感觉”变了?因为语法、补全、展开规则都在用户态程序里,内核完全不知道 | 是什么。
本章的 016 项目实现的是解析这一半——把命令行变成结构化的 Command 对象。执行那一半由内核原语负责,下面用四个实验把它讲透。
English: The kernel offers a couple dozen process calls. The shell is an ordinary user program that composes them into “commands”. Change the shell and the grammar changes; the kernel never learns what a pipe is.
POSIX 对 fork 的定义只有一句,但后面跟着一张例外清单:
fork() creates a new process by duplicating the calling process. The new process is referred to as the child process. The calling process is referred to as the parent process.
The child process and the parent process run in separate memory spaces. At the time of fork() both memory spaces have the same content. Memory writes, file mappings (mmap(2)), and unmappings (munmap(2)) performed by one of the processes do not affect the other.
The child process is an exact duplicate of the parent process except for the following points:
- The child has its own unique process ID, and this PID does not match the ID of any existing process group (setpgid(2)) or session.
- The child’s parent process ID is the same as the parent’s process ID.
- The child does not inherit its parent’s memory locks (mlock(2), mlockall(2)).
“exact duplicate, except” 这个措辞是理解全部陷阱的钥匙:状态看起来一样,但凡是”属于这个进程”的东西都不一样。PID 不一样,内存锁不继承,信号处理函数里的 alarm 也不继承。反过来,凡是”属于文件”的东西都一样——父子共享打开文件表项,所以共享文件偏移量。
5.2 fork 返回两次 / fork returns twice
pid_t pid = fork();
if (pid == 0) { /* 子进程 */ }
else if (pid > 0) { /* 父进程,pid 是子进程号 */ }
else { /* 失败,errno 有原因 */ }一次调用,两个返回值,两个进程从同一行继续。这不是语法糖,而是内核复制了调用者的地址空间、文件描述符表与寄存器状态,只把返回值改成不同的值。
实测(本机真实输出):
[fork] parent sees pid=24396, getpid=24395
[fork] child sees pid=0, getpid=24396, getppid=24395
[fork] parent reaped status: exited=1 code=7
三个数字的关系值得记住:父进程的 getpid 是 24395,子进程的 getpid 是 24396,正好等于父进程拿到的返回值;子进程的 getppid 是 24395。子进程的 PID 是父进程唯一能用来称呼它的东西——内核里没有”名字”,只有编号。
注意输出的顺序:父进程的行先打印,子进程的行后打印。这不是保证,只是调度器的选择。任何依赖”父先子后”的代码都是错的。
man page 对”不返回”的措辞比任何教材都硬:
execve() executes the program referred to by path. This causes the program that is currently being run by the calling process to be replaced with a new program, with newly initialized stack, heap, and (initialized and uninitialized) data segments.
execve() does not return on success, and the text, initialized data, uninitialized data (bss), and stack of the calling process are overwritten according to the contents of the newly loaded program.
“does not return on success” 是这一节最值得记住的一句。失败时它返回 -1(这是唯一能返回的情况),成功时它根本不返回——因为返回的那行代码已经不在进程的地址空间里了。所有写成 execv(...); printf("error") 的代码,printf 只在失败时才可能执行。
5.3 exec 不创建进程 / exec does not create a process
exec 系列做的事是:用一个新的可执行文件替换当前进程的地址空间,包括代码段、数据段、堆栈,以及参数与环境变量。进程本身(PID、父进程、打开的文件描述符、信号处理设置)保持不变。
实测:
child-shell-pid=25094
parent-fork-returned=25094
exit-code=0
父进程 fork 得到的子进程号是 25094,子进程 exec 成 /bin/sh 之后,那个 shell 打印的 $$ 仍然是 25094。PID 没变,程序换了。
由此得到两条实践规则:
- 成功的
exec不返回。 它没有”返回值”这个概念——原程序的代码已经不在了。如果exec返回了,说明它失败了,必须立刻处理(常见写法是_exit(127),因为此时进程已经处于半死状态)。 fork+exec是两件事。 想要”运行一个程序”必须先复制一个自己,再让复制品换掉自己。这也解释了 shell 为什么能连续执行多条命令:父进程(shell 本体)留下来继续读下一行。
English: exec replaces the image, not the process. A successful exec has no return value because there is no caller left. That is why “run a program” always costs a fork first — and why the shell survives its children.
5.4 wait 与退出状态 / wait and the exit status
子进程结束后不会立刻消失,它变成一个僵尸(zombie):除了退出状态和资源统计,其他资源都释放了。父进程通过 wait/waitpid 取走状态,内核才彻底回收。
上面实测里父进程取到的 code=7,就是子进程 _exit(7) 传出来的值——只有低 8 位有效,所以退出码的取值范围是 0–255。这也是为什么 exit(-1) 在 shell 里显示为 255。
状态不是单一整数,而是一个打包结构,必须用宏解读:
| 宏 | 含义 |
|---|---|
WIFEXITED(st) |
是否正常退出 |
WEXITSTATUS(st) |
正常退出时的退出码 |
WIFSIGNALED(st) |
是否被信号杀死 |
WTERMSIG(st) |
哪个信号 |
直接读 st 是常见错误:被信号杀死的进程,其 st 的低位并不是退出码。
5.5 重定向的本质是复制描述符 / Redirection copies descriptors
把 > file 实现成”打开文件,然后让标准输出指向它”,靠的是 dup2(oldfd, newfd):关闭 newfd,再把 oldfd 复制到 newfd 的位置。
顺序决定结果。下面两段代码看起来只差一行,行为完全不同:
/* 错误:先把 stdout 换成文件,再想打印进度 */
dup2(fd, STDOUT_FILENO);
printf("正在处理...\n"); /* 进了文件,用户永远看不到 *//* 正确:留一份原始 stdout 的副本 */
int saved = dup(STDOUT_FILENO);
dup2(fd, STDOUT_FILENO);
printf("payload\n"); /* 进文件 */
dup2(saved, STDOUT_FILENO); /* 恢复 */
printf("进度可见\n"); /* 回终端 */实测输出:
[dup2] child still reached the terminal after redirection
[dup2] file line 1: child: this line goes to the FILE, not the terminal
[dup2] file line 2: child: payload in file
文件里两行、终端一行,与代码意图完全对应。注意第一行(错误顺序)产生的那句话本该是诊断信息,结果写进了文件——这类 bug 的特征是”没有报错,只是信息去了别处”。
规范里还藏着一个专门防止”关掉自己”的特例。POSIX dup(3p) 对关闭顺序的规定是:
… it shall be closed first, unless fildes is equal to fildes2 in which case dup2() shall return fildes2 without closing it. If the close operation fails to close fildes2, dup2() shall return -1 without changing the open file description to which fildes2 refers. If fildes is not a valid file descriptor, dup2() shall return -1 and shall not close fildes2.
Linux 的 dup(2) 把这个特例单独列成一条:
If oldfd is a valid file descriptor, and newfd has the same value as oldfd, then dup2() does nothing, and returns newfd.
这个特例不是冗余。如果实现老老实实写成”先关闭 newfd,再把 oldfd 复制过去”,那么 dup2(fd, fd) 会先关掉 fd 自己,然后去复制一个已经关闭的描述符——返回 -1,而且 fd 永久失效。规范必须显式排除”两个参数相等”的情况,实现才能安全地假设它做了正确的事。
还有一个容易忽略的点:dup2 复制的是描述符,不是文件偏移量。两个描述符共享同一个打开文件表项,因此共享偏移。重定向后再 lseek,读写位置对两者同时生效。
5.6 管道:三个描述符的舞蹈 / Pipes: the descriptor dance
pipe(fds) 返回两个描述符:fds[0] 读端、fds[1] 写端。管道是单向的,要实现 a | b 需要:
pipe创建一对描述符fork出两个子进程- 第一个子进程:
dup2(fds[1], STDOUT),然后 close 掉 fds[0] 和 fds[1] - 第二个子进程:
dup2(fds[0], STDIN),同样 close 掉两个原描述符 - 父进程:close 掉两端,
wait两个孩子
第 3、4 步的 close 不是清理,而是语义必需。管道读端只有在所有写端都关闭之后才会读到 EOF。如果某个进程还留着一份写端(哪怕是它不用的那份),读端就永远等不到 EOF。
实测(写端行为对读端阻塞时间的影响):
[pipe] writer closes: read 2 bytes, blocked 0 ms
[pipe] writer leaks: read 2 bytes, blocked 300 ms
[pipe] leaking the write end costs 300 ms more
两行读到的字节数相同(数据早就到了),差别只在读到 EOF 的时刻:写端关闭后立刻 EOF;写端泄漏时会一直等到写进程自己退出(实验里故意让它持有 300 ms)。
这就是”cat file | head -1 有时会卡住”这类现象的来源。数据早就传完了,卡的是 EOF。
English: A pipe reader sees EOF only when every write end is closed. The bytes arrive either way; what the leaked descriptor blocks is the end-of-file, not the data.
5.7 解析为什么也不简单 / Why parsing is not the easy half
016 的实现只有几十行 Python,但它必须解决四个问题,每一个都能单独成为 bug 来源:
引号内的分隔符失效。 echo 'hello world' 是一个参数,不是两个。str.split() 会拆错,shlex.split 才对。
引号本身要被剥掉。 分词之后参数里不应残留 '。
变量展开的时机。 $HOME 必须在分词之后展开:先展开再分词,含空格的变量值会被拆成多个参数(这正是 shell 里”忘了加引号”的经典事故)。
管道与重定向要先于分词被识别出来。 它们既是语法符号又是普通字符——只有出现在引号外才是语法符号。
016 的实测输出:
[shell] [Command(['cat', 'input.txt'], ...), Command(['grep', 'hello'], ..., out=out.txt)]
[shell] [Command(['echo', 'hello world'], ..., out=log.txt, append=True)]
[shell] [Command(['echo', '/home/user'], ...)]
[shell] pipeline of 3 commands: [['wc', '-l'], ['sort'], ['uniq']]
--- PASS: test_shell
第一行是重定向 + 管道的组合,第二行的 'hello world' 保持为单个参数,第三行说明 $HOME 已被展开,第四行是三级管道。
5.8 解析器与 bash 的差距 / What the parser leaves out
016 的解析器三十几行,能跑通三级管道和引号分词。bash 的解析器是几万行。差距不在代码量,在每一条规则都对应一个用户会真的按下去的键:
| 能力 | 016 的实现 | bash | 漏掉的后果 |
|---|---|---|---|
| 分词 | shlex.split |
自定义词法 + POSIX 规则 | \ 在双引号内的语义不完整 |
| 管道切分 | 按 \| 字符串分割 |
词法分析中识别 | 引号内的 \| 被误切——源码注释里就承认了这是简化 |
| 重定向 | >、>>、< |
2>、2>&1、>|、<<<、<(...) |
诊断信息无法与数据分开 |
| 变量 | $NAME 查字典 |
展开、$?、${#x}、嵌套求值 |
拿不到上一条命令的退出码 |
| 通配符 | 无 | *、?、[] |
ls *.c 不可用 |
| 作业控制 | 无 | 进程组、fg/bg、Ctrl+Z |
只能当批处理器,不能交互 |
| 信号 | 无 | SIGINT 转发给前台进程组 |
Ctrl+C 会连 shell 一起杀 |
最后一行最能说明”解析”与”shell”的边界:解析器只决定命令长什么样,决定谁在什么时刻收哪个信号的是进程组和终端控制。一个只实现解析的程序,永远不能交互式使用——不是功能少,是它根本不在那个位置上。
English: Parsing decides what a command looks like; process groups and terminal control decide who receives which signal. A parser-only program can never be interactive, because it is not in that position at all.
5.9 Fallacies and Pitfalls (常见误解)
fork 之后父子进程共享内存
fork 之后两者是独立地址空间(现代内核用写时复制延迟真正的复制)。子进程改一个变量,父进程看不到。共享的是文件描述符表指向的打开文件——所以父子往同一个文件写会互相覆盖偏移,但不共享变量。
English: Copy-on-write shares pages until someone writes; variables are never shared, open files are.
exec 之后原来的代码还会继续跑
成功的 exec 不返回。写完 exec(...) 之后再写 printf("done") 是错的——那行只在失败时执行,而失败时你最该做的是报错并退出。
English: Code after a successful exec is unreachable; code after a failed exec is your error path.
只 close 自己不用的一端还不够——dup2 之后原始的那两个描述符也要 close。留着就是泄漏的写端,读端永远等不到 EOF。症状是”程序不报错,就是卡住”。
English: dup2 duplicates; it does not consume. Close the originals or the reader waits forever.
wait 的返回值当退出码直接读
status 是打包结构。必须 WIFEXITED 判断是否正常退出,再用 WEXITSTATUS 取码。被信号杀死的进程,直接读低位会得到无意义的值。
English: Decode status with the macros; raw bits are not an exit code.
5.10 Self-Check (自测题)
- 为什么
fork要返回两次?如果只返回子进程号,shell 该怎么区分父与子? exec成功时为什么不返回?如果它”返回了”,说明什么?- 实现
ls > out.txt时,dup2(fd, 1)应该放在exec之前还是之后?为什么? a | b中,如果a的进程忘记关闭管道的读端,会发生什么?- 一个进程
exit(300),shell 里echo $?显示什么?
答案
- 父子需要执行不同代码,而它们从同一条指令继续。返回值是最廉价的区分手段:0 表示”我是复制品”,正数表示”我是原件,这是复制品的编号”。
- 因为原程序的代码段已被替换,没有”下一条指令”可返回。它返回只可能是失败(找不到文件、没有执行权限等),此时必须处理错误。
exec之前。exec之后原程序的代码已经不存在,任何后续调用都不会执行;而且重定向必须作用于即将运行的新程序。a自己仍持有写端的一份副本,但读端在a手里没有意义。真正的问题是任何进程持有写端未关:b会一直等 EOF,直到a退出(并关闭它的写端)。若a是长驻进程,b就永久挂起。- 显示
44。退出码只有低 8 位有效:300 = 256 + 44。
5.11 Summary (本章小结)
shell 的两半都值得单独理解。进程这一半的核心是三条语义:fork 复制并返回两次、exec 替换映像且成功不返回、dup2 复制描述符因此顺序决定成败。管道把这三条串起来,而它的正确性依赖”关闭所有不用的端”这个容易被当成清理的步骤。解析这一半的核心是作用域:引号让分隔符失效,变量展开必须在分词之后。
English: fork copies and returns twice; exec replaces and never returns; dup2 copies, so order is semantics. Pipes compose all three, and their correctness hinges on closing what you do not use. Parsing is about scope: quotes disable delimiters, expansion comes after tokenization.
5.12 参考来源 / Sources
本章引用的是 man page 原文,不是教材转述:
fork(2)· Linux System Calls Manual — “creates a new process by duplicating the calling process”,以及 “exact duplicate of the parent process except for the following points” 的例外清单。execve(2)· Linux System Calls Manual — “does not return on success”,以及地址空间各段被覆盖的完整说明。dup(2)· Linux System Calls Manual —oldfd == newfd时 “dup2() does nothing, and returns newfd”。dup(3p)· POSIX Programmer’s Manual — 关闭顺序的规定:先关闭fildes2,“unless fildes is equal to fildes2”。
取原文的方式写下来,方便复现(版本随发行版变化,引用时应注明):
zcat "$(man -w fork)" | groff -man -Tutf8 | sed 's/\x1b\[[0-9;]*m//g'
zcat "$(man -w execve)" | groff -man -Tutf8 | sed 's/\x1b\[[0-9;]*m//g'
zcat /usr/share/man/man3p/dup.3p.gz | groff -man -Tutf8 | sed 's/\x1b\[[0-9;]*m//g'注意 man -w fork 在装了 gawk 的机器上可能给出 fork.3am(Gawk 的扩展模块),而不是系统调用——取 man page 之前要确认是哪一页。本章的引用来自 man2/fork.2.gz。
转述与原文有出入,是一个应当被报告的缺陷。
English: These are man page quotations, not paraphrases. Getting them is a one-liner, but
man -w forkmay hand you the gawk extension page instead of the syscall — confirm which page you are quoting.