Processes and threads
แนะนำใหม่
6:44
ระบบปฏิบัติการ
An operating system turns one processor into many machines. The first three units build the illusions of the processor: processes, taking turns, and safe…
บรรยายภาษาอังกฤษ · สลับคำบรรยายภาษาอังกฤษและภาษาจีนได้
ในบทเรียนนี้
| English | ความหมาย |
|---|---|
| operating system/ˈɒpəreɪtɪŋ ˈsɪstəm/ | System software that manages hardware resources and provides services for applications. |
| process/ˈprəʊses/ | กระบวนการ |
| running/ˈrʌnɪŋ/ | กำลังทำงาน |
| states/steɪts/ | สถานะ |
| ready/ˈredi/ | พร้อมทำงาน |
| blocked/blɒkt/ | ถูกขัดขวาง |
| thread/θred/ | เธรด |
| process control block/ˈprəʊses kənˈtrəʊl blɒk/ | บล็อคควบคุมโปรเซส |
| context switch/ˈkɒntekst swɪtʃ/ | การสลับบริบท |
| zombie/ˈzɒmbi/ | a finished process whose exit status has not yet been collected by its parent |
| copy-on-write/ˈkɒpɪ ɒn raɪt/ | sharing memory pages after fork and copying a page only when one process writes to it |
| multiprogramming/ˌmʌltɪˈprəʊɡræmɪŋ/ | Keeping several processes in memory so the CPU runs one while others wait for I/O. |
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.
The PCB and the switch
- To pause a process, the OS saves its process control block 进程控制块 (PCB): registers, program counter, page-table pointer and open files. A context switch 上下文切换 is exactly this save and restore.
- With the course timings, saving and restoring $2000$ bytes at $2$ GB/s costs $1$ µs, plus the cost of a cache full of the wrong data. Switching between threads keeps the same address space, so it is the cheaper switch.
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 fault's journey, and the price of threads
- Process 进程 A needs a page that is not in memory. It goes from running to blocked (waiting for the disk), and the scheduler runs B. When the disk finishes, A becomes ready, and later it runs again. The instruction that faulted starts again, so no work is lost.
- A web server creates a thread for each connection: they share a cache, and creating and switching costs about $50$ µs. As processes it would cost about $500$ µs. Sharing is the whole point of threads, and also their danger (lesson 0703).
Blocked is not ready: a blocked process waits for an event; a ready process waits only for the 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.
เกิดปัญหาอะไรขึ้น? เฉพาะทีมเว็บไซต์เท่านั้นที่เห็นข้อมูลนี้