Processes and threads
引入
6:44
操作系统
操作系统让一个处理器看起来像多台机器。前三个单元讲解处理器所呈现的假象:进程、轮流运行和安全共享。接下来的两个单元讲内存和存储。最后一个单元构建程序与内核之间的屏障。本课程中的每张表、每种交错执行和每次页表遍历都是确定的,所以你可以自己重新计算。
英文讲解 · 可切换中英文字幕
本课内容
| English | 释义 | 拼音 |
|---|---|---|
| operating system/ˈɒpəreɪtɪŋ ˈsɪstəm/ | 操作系统 | cāo zuò xì tǒng |
| process/ˈprəʊses/ | 进程 | jìn chéng |
| running/ˈrʌnɪŋ/ | 运行 | yùn xíng |
| states/steɪts/ | 状态 | zhuàng tài |
| ready/ˈredi/ | 就绪 | jiù xù |
| blocked/blɒkt/ | 阻塞 | zǔ sè |
| thread/θred/ | 线程 | xiàn chéng |
| process control block/ˈprəʊses kənˈtrəʊl blɒk/ | 进程控制块 | jìn chéng kòng zhì kuài |
| context switch/ˈkɒntekst swɪtʃ/ | 上下文切换 | shàng xià wén qiè huàn |
| zombie/ˈzɒmbi/ | 僵尸进程 | jiāng shī jìn chéng |
| copy-on-write/ˈkɒpɪ ɒn raɪt/ | 写时复制 | xiě shí fù zhì |
| multiprogramming/ˌmʌltɪˈprəʊɡræmɪŋ/ | 多道程序设计 | duō dào chéng xù shè jì |
这张卡片已更正,翻译更新前会显示英文。
The first illusion
- The operating system 操作系统 turns one processor into many machines. A process 进程 is a program that is running: it has its own address space, registers and open files.
- A process passes through five states 状态: new, ready 就绪 (waiting only for the CPU), running 运行, blocked 阻塞 (waiting for an event, such as I/O completion) and terminated. A thread 线程 is one flow of execution inside a process: it shares the process's memory but has its own stack.

A process waiting for disk I/O is —
难度 1/5 · 今日首次完成可获 100 金币及少量额外奖励
Blocked waits on a device; ready waits only for the CPU.
PCB 与切换
- 挂起进程时 OS 要保存进程控制块 process control block——寄存器、程序计数器、页表指针、打开文件。上下文切换 context switch 正是这组存取。
- 按课程时序,2 GB/s 存取 2000 字节花 1 µs——外加缓存污染。切线程跳过地址空间(共享),所以更便宜。
Switching threads is cheaper than switching processes because threads —
难度 1/5 · 今日首次完成可获 100 金币及少量额外奖励
Threads share the address space, so the switch skips it — only stacks and registers move.
一次缺页的旅程与线程的价钱
- 进程 A 缺页:运行转阻塞(磁盘);调度器派发 B;磁盘完成;A 转就绪;稍后再运行——缺页指令重启,工作零丢失。
- Web 服务器每连接一条线程:共享缓存,创建加切换约 50 µs;换成进程约 500 µs。共享是线程的意义——也是 7.3 单元的危险。
阻塞不是就绪:阻塞等设备,就绪只等 CPU。
A crashed thread takes with it —
难度 1/5 · 今日首次完成可获 100 金币及少量额外奖励
One address space: a thread crash is a process crash.
这是一张新卡片,翻译完成前会显示英文。
fork, exec and wait
- On Unix a new process starts with
fork(), which copies the caller. It returns $0$ in the child and the child's PID in the parent.exec()then replaces the child's program, andwait()lets the parent collect the exit status. A finished child that nobody waited for is a zombie 僵尸进程.
pid_t pid = fork();
if (pid == 0) {
/* child: become ls */
execlp("ls", "ls", "-l", NULL);
} else {
/* parent: collect the child */
int status;
wait(&status);
}
- Fork is cheap because of copy-on-write 写时复制: parent and child share pages until one of them writes. A loop that calls
fork()three times makes $2^3 = 8$ processes, because every copy keeps forking.
A program calls fork() four times in a loop, and every process keeps looping. How many processes exist at the end, the original included?
难度 2/5 · 今日首次完成可获 140 金币及少量额外奖励
Each fork doubles the count: 2^4 = 16.
A successful exec() returns to the line after the call.
难度 1/5 · 今日首次完成可获 100 金币及少量额外奖励
exec replaces the whole program image, so there is no old code to return to; it returns only on failure.
这是一张新卡片,翻译完成前会显示英文。
Why run many processes at once
- A process that waits for I/O a fraction $p$ of its time leaves the CPU idle. With $n$ independent processes, the CPU is idle only when all of them wait: utilisation $= 1 - p^n$.
- For I/O-bound jobs with $p = 0.8$: one process keeps the CPU 20% busy, and four keep it $1 - 0.8^4 = \mathbf{59\%}$ busy. That is the reason for multiprogramming 多道程序设计: blocked processes hand the CPU to ready ones.
| Processes $n$ | 1 | 2 | 4 | 8 |
|---|---|---|---|---|
| CPU busy, $1 - 0.8^n$ | 20% | 36% | 59% | 83% |
Four independent processes, each waiting on I/O 80% of the time: CPU utilisation (one decimal, %)?
难度 2/5 · 今日首次完成可获 140 金币及少量额外奖励
$1 - 0.8^4 = 1 - 0.4096 = 0.590$.
One I/O-bound process keeps the CPU busy most of the time.
难度 1/5 · 今日首次完成可获 100 金币及少量额外奖励
在……时 $p = 0.8$ it keeps it only 20% busy; several processes are needed.
这是一张新卡片,翻译完成前会显示英文。
Worked example: count the processes
fork(); fork(); fork();doubles the number of processes 进程 three times: $2^3 = 8$ processes, including the original.if (fork() == 0) fork();is different: only the child forks again, so there are $\mathbf{3}$ processes. Draw the tree, one branch perfork, and count the leaves.
How many processes in total after if (fork() == 0) fork();?
难度 2/5 · 今日首次完成可获 140 金币及少量额外奖励
Parent and child; then the child forks once more.
这是一张新卡片,翻译完成前会显示英文。
Marks that slip away
- Blocked for ready. A blocked 阻塞 process waits for an event; only a ready one waits for the CPU, so the scheduler never picks a blocked one.
- Threads as free processes. Threads share one address space, so one thread's wrong write damages them all.
- Forgetting that
fork()returns twice. It returns $0$ in the child and the child's PID in the parent; test the value to know which one you are.
这是一张新卡片,翻译完成前会显示英文。
Recap
- Process: its own address space; thread: shared memory, its own stack.
- States: new, ready, running, blocked, terminated; blocked waits for an event, ready only for the CPU.
- A context switch 上下文切换 saves and restores the PCB; a thread switch keeps the address space.
出了什么问题?只有网站团队能看到这些信息。