Back to tree

🍽️ 进程与线程:订单与服务员🍽️ Processes & Threads: Orders and Waiters

上一片里,主板把 CPU、内存、硬盘连成了一台整机。硬件齐了,可活儿该怎么派?小电脑开了家餐厅,请来一位经理:操作系统。 Last leaf, the motherboard wired the CPU, memory and storage into one machine. The hardware is ready — but who hands out the work? The little computer opens a restaurant and hires a manager: the operating system.

第 1 步:进程是一桌独立的订单Step 1: A process is one table's order

经理把每个正在运行的程序,都当成一桌客人。The manager treats every running program as one table of guests.

操作系统是经理,每个进程是一桌独立的订单
打开一个程序,就开了一桌。这一桌就叫进程:正在运行的程序。每桌有自己的桌面和餐具,也就是独立的内存。一桌打翻了汤,别桌照吃——浏览器崩了,音乐照放。 Open a program and a table opens. That table is a process: a program that's running. Each table has its own tabletop and cutlery — its own memory. If one table spills its soup, the others keep eating: the browser crashes, the music plays on.

可一桌点了一大堆菜,只派一个服务员跑得过来吗?But one table ordered a pile of dishes. Can a single waiter keep up?

第 2 步:线程是同一桌分头干活的服务员Step 2: Threads are waiters sharing one table

单线程一个一个来 vs 多线程分头同时干,共用汤勺要加锁
线程是进程里真正干活的人,每个进程至少有一个。一个人上 4 道菜要 8 秒;4 人分头上,2 秒搞定。可他们共用这桌的东西,也就是同一份内存。抢同一把汤勺会出乱子,所以要“加锁”:一次只给一人用。 A thread is the one who actually does the work inside a process — every process has at least one. One waiter needs 8 s for 4 dishes; four waiters splitting up need just 2 s. But they share the table's things, which means the same memory. Grabbing the same ladle causes chaos, so we add a “lock”: one waiter at a time.

可桌子有几十张,CPU 核却只有几个,经理怎么排班?Dozens of tables, only a few CPU cores. How does the manager schedule them?

第 3 步:经理轮流排班,快得像同时Step 3: Taking turns so fast it looks simultaneous

任务管理器里的进程与线程,4 核 CPU 轮流切换
打开任务管理器,能看到几十个进程、上千个线程。CPU 只有几个核,经理就把时间切成小段,每段几毫秒到几十毫秒,轮流分给各个线程。切换太快,你就觉得全在同时跑。浏览器也常给每个标签页开独立进程,一页卡死,不会拖垮全部。 Open Task Manager and you'll see dozens of processes and thousands of threads. The CPU has only a few cores, so the manager slices time into tiny turns — a few to a few dozen milliseconds each — and rotates them among the threads. The switching is so fast it all looks simultaneous. Browsers often give each tab its own process too, so one frozen page can't drag down the rest.

🎮 你来当后厨调度(30 秒)🎮 Your turn: run the kitchen (30 seconds)

道理讲完了。先挑几个服务员、点“开工”,看用时怎么变;再闯 4 关:人多什么时候管用,什么时候白搭?Theory done. Pick how many waiters, hit “Serve”, and watch the time change; then clear 4 levels to find out when more hands help — and when they don't.

一句话记住它:进程是一桌独立的订单,线程是桌边分头干活的服务员;互不相干的活人多就快,抢同一样东西就得加锁排队。 Remember it in one line: a process is one table's own order; threads are the waiters splitting its work. Independent jobs speed up with more hands; a shared item needs a lock and a queue.
进程:正在运行的程序,各有独立内存,一个崩了不连累别人Process: a running program with its own memory — one crash doesn't take down the rest 线程:进程里干活的人,共享同一份内存,能同时开工Thread: the worker inside a process — threads share memory and work at the same time 抢同一资源要加锁;几个 CPU 核靠轮流切换,跑起上千个线程Shared resources need a lock; a few CPU cores run thousands of threads by taking turns
内容参考 OpenStax 等公开教材,多来源核对 · AI 生成、人工审核 · 发现错误欢迎指正,帮这片叶子长得更好。 Based on OpenStax and other open textbooks, cross-checked across sources · AI-generated, human-reviewed · Spotted a mistake? Tell us — help this leaf grow.