OS resource management and processes · 操作系统资源管理与进程
| English | 中文 | Pinyin · 拼音 |
|---|---|---|
| process/ˈprəʊses/ | 进程 | jìn chéng |
| scheduler/ˈʃedjʊlə/ | 调度器 | diào dù qì |
| multi-tasking/ˈmʌlti ˈtæskɪŋ/ | 多任务 | duō rèn wù |
| spooling/ˈspuːlɪŋ/ | 假脱机 | jiǎ tuō jī |
| caching/ˈkæʃɪŋ/ | 缓存 | huǎn cún |
| round robin/raʊnd ˈrɒbɪn/ | 轮转 | lún zhuàn |
| context switch/ˈkɒntekst swɪtʃ/ | 上下文切换 | shàng xià wén qiè huàn |
| process control block/ˈprəʊses kənˈtrəʊl blɒk/ | 进程控制块 | jìn chéng kòng zhì kuài |
Why the mouse still moves when everything has frozen
- A program locks up. The window will not repaint, the spinner stops, nothing responds. Yet the mouse pointer still glides across the screen exactly as before.
- That is the operating system telling you something: the machine is not stuck, one process 进程 is. The scheduler is still handing out slices of the processor, and the frozen program is simply not doing anything useful with its.
- Every resource in the machine is shared this way: the processor by time, memory by space, the printer by a queue.
- This lesson is how an OS gets the most out of those resources, and how it manages a process from creation to termination.
一切都卡住了,鼠标为什么还在动
- 一个程序死锁了。窗口不再重绘,转圈停住,什么都没反应。可鼠标指针依然像先前一样在屏幕上滑动。
- 那是操作系统在告诉你一件事:机器没有卡住,卡住的是一个进程(process)。调度器仍在分发处理器的时间片,只是那个冻住的程序没有用它做任何有用的事。
- 机器里的每一种资源都是这样共享的:处理器按时间,内存按空间,打印机按队列。
- 这一课讲操作系统怎样把这些资源用到极致,以及它怎样从创建到终止地管理一个进程。
Getting the most from the resources
- Multi-tasking 多任务: the processor is switched rapidly between processes, so several appear to run at once even though only one is executing at any instant.
- Paging: memory is moved between RAM and disk so more programs can be open than physical memory allows.
- Spooling 假脱机: output for a slow device is queued on disk, so the processor never waits for a printer. Caching 缓存 keeps recently used data in fast memory so it need not be fetched again.
- The OS also hides all of this behind a user interface, a command line or a GUI.
把资源用到极致
- 多任务(multi-tasking):处理器在各进程之间快速切换,于是几个程序看起来同时运行,尽管任一瞬间只有一个在执行。
- 分页:内存在 RAM 和磁盘之间移动,于是能打开的程序比物理内存允许的更多。
- 假脱机(spooling):送往慢速设备的输出先在磁盘上排队,于是处理器从不等打印机。缓存(caching)把最近用过的数据留在快速存储里,免得再取一次。
- 操作系统还把这一切藏在用户界面之后——命令行或图形界面。
Multi-tasking lets several programs appear to run at once by: · 多任务让几个程序看起来同时运行,通过:
The OS rapidly switches the single CPU between processes so they all seem to progress together. · 操作系统在进程之间快速切换单个 CPU,所以它们看起来都一起前进。
Worked example: why spooling helps
- Explain how spooling improves the use of a computer's resources. [3]
- A printer is thousands of times slower than the processor, so a program sending output directly would wait for it and the processor would sit idle.
- With spooling the output is written quickly to a queue on disk, and the program continues immediately.
- A separate process then feeds the printer at its own speed, and several jobs can queue while the processor gets on with other work. Name the slow device, the queue, and what the processor does instead.
例题:假脱机为什么有帮助
- 解释假脱机怎样改进计算机资源的使用。[3]
- 打印机比处理器慢几千倍,所以直接送输出的程序会等它,处理器就闲着。
- 有了假脱机,输出被迅速写进磁盘上的队列,程序立刻继续。
- 另一个进程随后按打印机自己的速度喂给它,而且多个作业可以排队,同时处理器去做别的工作。要说出慢速设备、队列,以及处理器改去做什么。
Spooling helps the system because: · 假脱机帮助系统,因为:
Spooling buffers print jobs to disk so the fast CPU is not held up by the slow printer. · 假脱机把打印作业缓冲到磁盘,所以快的 CPU 不被慢的打印机拖住。
Which techniques help an OS get the most from its resources? Select all · 所有 that apply. · 哪些技术帮助操作系统最大化利用资源?选出所有适用的。
Sharing time, queueing for slow devices and keeping hot data close. Killing waiting processes would lose the user's work, not improve utilisation. · 共享时间、为慢设备排队、把热数据放近。杀掉等待中的进程会丢掉用户的工作,而不是提高利用率。
Processes and the scheduler
- A process is a program in execution: its code, its current state, its memory and its open files. A program on disk is not a process; a program running is.
- The scheduler 调度器 decides which ready process runs next and for how long. Its goals are to keep the processor busy, respond quickly to interactive users, and be fair.
- Round robin 轮转 gives each ready process a fixed time slice and then sends it to the back of the queue: simple, fair and responsive. Other policies are first come first served, shortest job first and shortest remaining time.
Everyone gets a turn, and nobody keeps the processor
进程与调度器
- 进程是执行中的程序:它的代码、当前状态、内存和打开的文件。磁盘上的程序不是进程;运行中的程序才是。
- 调度器(scheduler)决定哪个就绪进程下一个运行、运行多久。它的目标是让处理器保持忙碌、对交互用户快速响应,并且公平。
- 轮转(round robin)给每个就绪进程一个固定的时间片,然后把它送到队尾:简单、公平、响应快。其他策略有先来先服务、最短作业优先和最短剩余时间优先。

人人有份,谁也占不住处理器
Round-robin scheduling gives each ready process a fixed time slice, then moves it to the back of the queue — making it fair and responsive. · 轮转调度给每个就绪的进程一个固定的时间片,然后把它移到队列的后面——使它公平且响应快。
Equal time slices in turn stop any one process hogging the CPU, so interactive programs stay responsive. · 轮流的相等时间片阻止任何一个进程霸占 CPU,所以交互式程序保持响应。
What is a process? · 什么是进程?
The file on disk is a program; once it is loaded and running, with state of its own, it is a process. · 磁盘上的文件是程序;一旦被加载运行、有了自己的状态,它就是进程。
The process states
- New: being created. Ready: able to run, waiting only for the processor. Running: currently executing. Blocked: waiting for something else, usually input or output. Terminated: finished.
- The transitions are what the exam asks for. Running to ready when the time slice expires. Running to blocked when the process requests I/O. Blocked to ready when that I/O completes. Ready to running when the scheduler selects it.
- Note the asymmetry: a blocked process never goes straight back to running. It rejoins the ready queue and waits its turn.
Five states, and the reasons for each arrow
进程的状态
-
新建:正在创建。就绪:可以运行,只等处理器。运行:正在执行。阻塞:在等别的东西,通常是输入或输出。终止:已结束。
-
考试要考的是这些转换。时间片用完,运行转就绪。进程请求 I/O,运行转阻塞。那次 I/O 完成,阻塞转就绪。调度器选中它,就绪转运行。
-
注意这里的不对称:阻塞的进程从不直接回到运行。它重新排进就绪队列,等自己的轮次。

五个状态,以及每个箭头的理由
Match each process state to what it means. · 把每个进程状态与它的含义配对。
A process cycles ready → running → (blocked) → ready, until it terminates. · 一个进程循环 就绪 → 运行 →(阻塞)→ 就绪,直到它终止。
Worked example: trace a process
- A process is running when it asks to read a file. Describe what happens to it.
- It moves from running to blocked, because it cannot continue until the data arrives, and the scheduler immediately gives the processor to another ready process rather than waiting.
- When the disk finishes, the process moves from blocked to ready. It does not resume immediately; it joins the ready queue.
- The scheduler later selects it, and it moves from ready to running, continuing from exactly where it stopped.
例题:追踪一个进程
- 一个进程正在运行时请求读取一个文件。描述它会怎样。
- 它从运行转到阻塞,因为数据到达之前它无法继续;调度器立刻把处理器交给另一个就绪进程,而不是干等。
- 磁盘完成后,进程从阻塞转到就绪。它不会立刻恢复运行;它排进就绪队列。
- 调度器稍后选中它,它从就绪转到运行,从停下的地方原样继续。
The life of a process · 一个进程的生命
Tap round the loop a process travels. It only runs when the scheduler picks it; needing I/O sends it to blocked, and finishing its time slice sends it back to ready — round and round until it's done. · 点击绕一个进程经历的循环。它只在调度器挑选它时运行;需要 I/O 把它送到阻塞,完成它的时间片把它送回就绪——一圈又一圈直到它完成。
A running process asks to read a file. Put what happens to it in order. · 一个运行中的进程请求读取文件。把它的经历按顺序排列。
A blocked process never returns straight to running: it rejoins the ready queue and waits its turn. · 阻塞的进程绝不会直接回到运行:它重新排进就绪队列,等自己的轮次。
Context switching
- To change which process is running, the OS saves the current process's state, its registers, program counter and status, into its process control block 进程控制块, then loads the next process's state from that process's own block.
- That is a context switch 上下文切换. It has a real cost: during it the processor is doing housekeeping, not the user's work.
- So the time slice is a trade-off. Too long and interactive programs feel sluggish; too short and the machine spends its time switching instead of computing.
上下文切换
- 要更换正在运行的进程,操作系统把当前进程的状态——寄存器、程序计数器和状态字——保存到它的进程控制块(process control block),再从下一个进程自己的块里载入它的状态。
- 这就是上下文切换(context switch)。它有实实在在的代价:切换期间处理器在做内务,而不是用户的工作。
- 所以时间片是个权衡。太长,交互程序显得迟钝;太短,机器把时间花在切换而不是计算上。
A context switch involves: · 一个上下文切换涉及:
The OS saves the running process's registers/PC to its PCB and loads the next process's — a small overhead each switch. · 操作系统把运行中进程的寄存器/PC 保存到它的 PCB 并加载下一个进程的——每次切换一个小的开销。
During a context switch, a process's registers and program counter are saved into its . · 上下文切换时,进程的寄存器和程序计数器被保存到它的。
The next process's state is then loaded from its own block. The saving and loading is real work, which is what makes switching cost time. · 接着从下一个进程自己的块里载入它的状态。这一存一载是实打实的工作,这就是切换要花时间的原因。
Interrupts and the kernel
- The kernel, the core of the OS, is what actually performs this: it responds to interrupts from devices and from programs, and an interrupt is what returns control to the OS from a running process.
- A timer interrupt at the end of a time slice is what makes pre-emptive scheduling possible at all; without it a program that never yields would keep the processor for ever.
- That is the answer to the opening puzzle: the frozen program holds no more of the processor than its slice, so the mouse keeps moving.
中断与内核
- 内核——操作系统的核心——才是真正执行这一切的东西:它响应来自设备和程序的中断,而中断正是把控制权从运行中的进程交回操作系统的机制。
- 时间片结束时的时钟中断,是抢占式调度得以可能的根本;没有它,一个从不让出的程序会永远占着处理器。
- 这就是开头那个谜题的答案:冻住的程序占不到超过它那一片的处理器,所以鼠标照样在动。
Marks that slip away
- A process is a program in execution, not the file on disk.
- Blocked to ready, never blocked straight to running. The scheduler chooses when it runs again.
- A context switch saves and restores state through the process control block, and it costs time.
- Multi-tasking means processes take turns so quickly they appear simultaneous, not that they truly execute at once on one core.
容易丢掉的分
- 进程是执行中的程序,不是磁盘上的文件。
- 阻塞转就绪,绝不是阻塞直接转运行。它何时再运行由调度器决定。
- 上下文切换通过进程控制块保存和恢复状态,而且它要花时间。
- 多任务意味着进程轮流得快到看起来同时,而不是在一个核上真的同时执行。
You've got it
- the OS maximises resource use by multi-tasking, paging, spooling to a disk queue and caching
- a process is a program in execution; the scheduler picks the next ready one, with round robin giving each a fixed time slice
- states new, ready, running, blocked, terminated: slice expiry sends running to ready, an I/O request sends it to blocked, and I/O completion sends it back to ready
- a context switch saves and restores state via the process control block and costs time, which is why the slice length is a trade-off
你掌握了
- 操作系统靠多任务、分页、写入磁盘队列的假脱机和缓存来最大化资源利用
- 进程是执行中的程序;调度器选出下一个就绪进程,轮转给每个一个固定的时间片
- 状态为新建、就绪、运行、阻塞、终止:片用完把运行送到就绪,I/O 请求把它送到阻塞,I/O 完成把它送回就绪
- 上下文切换通过进程控制块保存和恢复状态,而且要花时间,这就是片长是一个权衡的原因