Operating Systems
University Computer Science Topic 7 6:44 English narration · English and 中文 subtitles you can switch
Chapters
Transcript
An operating system turns one processor into many machines.
操作系统让一个处理器看起来像多台机器。
The first three units build the illusions of the processor: processes, taking turns, and safe sharing.
前三个单元讲解处理器所呈现的假象:进程、轮流运行和安全共享。
The next two cover memory and storage.
接下来的两个单元讲内存和存储。
The last unit builds the wall between programs and the kernel.
最后一个单元构建程序与内核之间的屏障。
Every table, interleaving and page walk in this course is deterministic, so you can recompute it yourself.
本课程中的每张表、每种交错执行和每次页表遍历都是确定的,所以你可以自己重新计算。
A process moves between five states: new, ready, running, blocked and terminated.
一个进程在五种状态之间转换:新建、就绪、运行、阻塞和终止。
Blocked means waiting for a device; ready means waiting only for the processor.
阻塞意味着等待设备;就绪意味着只在等待处理器。
A context switch saves the process control block: registers, program counter, page-table pointer and open files.
上下文切换时会保存进程控制块中的信息:寄存器、程序计数器、页表指针和打开的文件。
Threads share one address space, so switching between them is about ten times cheaper than switching processes.
线程共享同一个地址空间,因此线程之间切换的开销约为进程之间切换的十分之一。
Walk the loop and name each arc.
沿着这个循环走一遍,并说出每条箭头代表的转换。
Dispatch moves a process from ready to running.
调度使进程从就绪转为运行。
Preemption moves it back when its time is up.
时间用完时,抢占使它转回就绪。
Blocking moves it from running to blocked when it waits for input or output.
等待输入或输出时,阻塞使它从运行转为阻塞。
Waking moves it from blocked back to ready, never straight to running.
唤醒使它从阻塞转回就绪,绝不会直接转为运行。
The scheduler decides who runs next.
调度程序决定接下来由谁运行。
A new process is admitted to the ready queue.
新进程被接纳进入就绪队列。
The scheduler dispatches it to running.
调度程序将它调度到运行状态。
A time-out or interrupt sends it back to ready.
时间片用完或发生中断时,它会返回就绪状态。
If it requests I/O, it becomes blocked, and when the I/O completes it returns to ready, not straight to running.
如果它请求 I/O,就会进入阻塞状态;I/O 完成后,它返回就绪状态,而不是直接进入运行状态。
On exit it terminates.
退出时,它进入终止状态。
Turnaround is finish time minus arrival; waiting is turnaround minus the burst.
周转时间等于完成时间减去到达时间;等待时间等于周转时间减去运行时间。
For bursts of 7, 4, 1 and 4, first-come-first-served averages 11.5, shortest-job-first 7.75, and round robin with a quantum of 2 averages 11.25, but serves the short job much sooner.
对于运行时间分别为 7、4、1 和 4 的作业,先来先服务的平均周转时间为 11.5,最短作业优先为 7.75,时间片为 2 的轮转调度平均为 11.25,但它能让短作业早得多地得到处理。
A quantum that is too small spends the processor on switching instead of work.
时间片过小会让处理器把时间花在切换上,而不是执行工作。
Read the finish times off the bars.
从条形图上读出完成时间。
Under first-come-first-served, the long job runs first and everyone waits behind it.
在先来先服务调度下,长作业先运行,其他作业都要在后面等待。
Under shortest-job-first, the 1-unit job finishes at 1 and the average turnaround falls to 7.75.
在最短作业优先调度下,运行时间为 1 个单位的作业在时刻 1 完成,平均周转时间降到 7.75。
Round robin slices the processor into turns of 2, so no job waits long, at the cost of more switches.
轮转调度将处理器时间分成长度为 2 的时间片,让每个作业都不必等待太久,代价是切换次数增加。
The same three jobs are scheduled two ways.
同样的三个作业用两种方式调度。
First come first served runs the long job first, so the short jobs wait behind it.
先来先服务先运行长作业,因此短作业要在后面等待。
Shortest job first runs the short jobs first, and the average waiting time falls from 6.7 to 2.7 units.
最短作业优先先运行短作业,平均等待时间从 6.7 降到 2.7 个单位。
The cost is that a long job can wait indefinitely.
代价是长作业可能无限期等待。
Adding one to a shared counter means read, modify, write, and that is not atomic.
将共享计数器加一包括读取、修改、写入三个步骤,这不是原子操作。
If two threads both read 5, both write 6, and one increment is lost.
如果两个线程都读到 5,然后都写入 6,就会丢失一次递增。
With four increments each, any final value from 2 to 8 is possible.
如果每个线程各递增四次,最终值可能是 2 到 8 之间的任何值。
Locks fix this.
锁可以解决这个问题。
Deadlock needs four conditions at once; ordering the locks breaks circular wait, the cheapest fix.
死锁需要四个条件同时成立;按固定顺序获取锁可以破坏循环等待,这是代价最低的解决办法。
Count the lost increments yourself.
自己数一数丢失的递增次数。
Without a lock, both threads read 5 before either writes, so both write 6, and one update disappears without any error.
不加锁时,两个线程在任一线程写入之前都读到 5,因此都写入 6,一次更新就这样消失了,却没有任何报错。
With a lock, the second thread must wait until the first has read, added and written.
加锁后,第二个线程必须等第一个线程完成读取、加一和写入。
The race does not crash; it quietly undercounts, which makes it hard to find.
竞争不会导致程序崩溃,而是悄悄地少计数,因此很难发现。
With 16-bit virtual addresses and 256-byte pages, each process sees 256 pages, 64 kilobytes, even if physical memory holds only 16 frames.
使用 16 位虚拟地址和 256 字节页面时,每个进程看到 256 个页面,也就是 64 千字节,即使物理内存只能容纳 16 个页框。
Address 0x0204 is page 2, offset 4; the table maps page 2 to frame 5, giving 0x0504.
地址 0x0204 对应第 2 页、偏移量 4;页表将第 2 页映射到第 5 个页框,得到 0x0504。
A TLB hit skips the walk.
TLB 命中时可以跳过页表遍历。
A page fault means a disk read, a million times slower.
缺页意味着要读取磁盘,速度慢一百万倍。
One address, three outcomes.
一个地址,三种结果。
A TLB hit translates in about a nanosecond.
TLB 命中时,地址转换大约需要一纳秒。
A miss walks the page table, about ten nanoseconds.
未命中时,需要遍历页表,大约十纳秒。
If the entry is invalid, a page fault loads the page from disk, about five milliseconds, evicting another page if needed, and then restarts the instruction.
如果页表项无效,缺页处理会从磁盘加载页面,大约需要五毫秒;必要时会淘汰另一个页面,然后重新执行指令。
Virtual memory works because faults are rare.
虚拟内存之所以有效,是因为缺页很少发生。
Logical memory is divided into pages, and physical memory into frames of the same size.
逻辑内存分成页面,物理内存分成大小相同的页框。
The page table maps each page to any free frame, so a process's frames can be scattered.
页表将每个页面映射到任意空闲页框,因此一个进程的页框可以分散在各处。
There are no awkward gaps between processes, though the last page may be partly empty.
进程之间不会留下难以利用的空隙,不过最后一个页面可能没有填满。
A file name leads through a directory entry to an inode, which points to the data blocks.
文件名通过目录项找到一个索引节点,索引节点再指向数据块。
With 1 kilobyte blocks, four direct pointers and one indirect block of 256 pointers, the largest file is 260 kilobytes.
使用 1 千字节的块、四个直接指针和一个包含 256 个指针的间接块时,最大文件大小为 260 千字节。
Reaching block 6 needs two reads, one for the indirect block.
访问第 6 个块需要读取两次,其中一次是读取间接块。
Journaling writes the intended change first, so a crash can be replayed.
日志机制先写入预期的修改,因此崩溃后可以重放这些修改。
Follow the pointers from a name to the data.
沿指针从名称找到数据。
The directory maps the name to an inode number.
目录将名称映射到一个索引节点编号。
The inode holds the file's size and the first four block numbers directly.
索引节点保存文件大小,并直接保存前四个块的编号。
For larger files, it points to an indirect block full of further block numbers.
对于更大的文件,它会指向一个间接块,里面存放更多块编号。
Count the reads along the way: big files pay one extra.
数一数沿途的读取次数:大文件需要多读一次。
A mode bit separates kernel mode, which can touch devices and page tables, from user mode.
模式位将可以访问设备和页表的内核态与用户态区分开来。
User programs cross into the kernel only through a trap: the system call.
用户程序只能通过陷入进入内核:也就是系统调用。
Each round trip costs about 200 nanoseconds, so a million calls cost 0.2 seconds, which is why libraries buffer.
每次往返大约耗时 200 纳秒,因此一百万次调用要花 0.2 秒,这就是库使用缓冲的原因。
Hand privileged services capabilities, not file names.
应向特权服务传递能力,而不是文件名。
Two rings apart, one door through.
两个特权环之间只有一道入口。
User code runs in the outer ring and cannot touch hardware directly.
用户代码在外环运行,不能直接访问硬件。
The only way in is the system call door, where the kernel checks every request before acting.
唯一的入口是系统调用,内核会在执行操作前检查每个请求。
This is least privilege: each part of the system gets only the access its job needs, and every crossing is validated.
这就是最小权限原则:系统的每个部分只获得完成其工作所需的访问权限,每次跨越边界都要经过验证。
Now test yourself from memory.
现在凭记忆自测一下。
A process is waiting for a disk read: which state is it in?
一个进程正在等待磁盘读取:它处于什么状态?
What is the average turnaround for the course job set under shortest-job-first?
对于课程中的那组作业,最短作业优先的平均周转时间是多少?
With 16-bit addresses and 256-byte pages, how many virtual pages are there?
使用 16 位地址和 256 字节页面时,有多少个虚拟页面?
What is the only way from user mode into the kernel?
从用户态进入内核的唯一方式是什么?
Pause, answer, then check.
暂停,回答,然后核对。
Comments
No comments yet. Be the first to ask or share something.