alicemare / ideas

用Issue来记录一些简单的笔记?
0 stars 0 forks source link

Operating Systems: Three Easy Pieces-Virtualization #1

Open alicemare opened 5 years ago

alicemare commented 5 years ago

不务正业系列,开始用Issues记录一些简单的想法 第一个Issues记录OS: Three Easy Pieces

alicemare commented 5 years ago

之前建立blog的个人愿望之一也是分享自己的想法,记录一些见闻,但是一来是太懒,二来不去学习也写不出来什么东西,其次是个人blog其实并不是很适合wiki等零碎繁多的知识点的记录,更适合在一个问题,一个bug上钻研,于是就开了个repo用来写Issue,将来整理成wiki形式发布成html或者markdown文件,期待自己的努力

alicemare commented 5 years ago

先来看看什么是虚拟化吧(virtualization)

Student: But what is virtualization, oh noble professor? Professor: Imagine we have a peach. Student: A peach? (incredulous) Professor: Yes, a peach. Let us call that the physical peach. But we have many eaters who would like to eat this peach. What we would like to present to each eater is their own peach, so that they can be happy. We call the peach we give eaters virtual peaches; we somehow create many of these virtual peaches out of the one physical peach. And the important thing: in this illusion, it looks to each eater like they have a physical peach, but in reality they don’t. Student: So you are sharing the peach, but you don’t even know it? ... Student: Well, if I was sharing a peach with somebody else, I think I would notice. Professor: Ah yes! Good point. But that is the thing with many eaters; most of the time they are napping or doing something else, and thus, you can snatch that peach away and give it to someone else for a while. And thus we create the illusion of many virtual peaches, one peach for each person!

虚拟化,就是为了充分提高CPU(桃子)的算力(给多个人吃),为每一个进程都抽象了一个虚拟的“桃子”,让所有吃的人都以为自己独享这个桃子,殊不知CPU通过进程切换的形式可以同时给多个进程提供服务,而进程对此是一无所知的

alicemare commented 5 years ago

进程的定义很简单:

The definition of a process, informally, is quite simple: it is a running program 但是,程序和进程是有很大区别的,程序是“没有生命的”,它“静静的躺在硬盘里”,等待着被触发并装载运行,进程就可以理解为程序在执行期间的那段过程

现在的OS都可以同时运行N个进程(这一点可以通过Windows下的任务管理器或者Linux #ps aux来验证),所以,我们创建进程这个抽象所遇到的第一个问题,就是如何抽象出多个CPU的景象

alicemare commented 5 years ago

为了解决需要“创建多个共存的虚拟CPU”的问题 既需硬件上的支持(mechanisms),也需要软件层次(系统)的调度 硬件层面上,以后会提到“上下文切换”,给予OS停止一个进程并切换到另一个进程上下文的能力 而在硬件之上的,需要OS来提供调度算法(scheduling policies),操作系统会根据历史信息(如,这个进程已经运行了多久),进程工作量状态,操作系统本身的性能指标(比如要求优先响应用户的交互式系统)等,来做出决策

tips: 关于分时系统如何实现多个程序的同时运行:

Time sharing is a basic technique used by an OS to share a resource. By allowing the resource to be used for a little while by one entity, and then a little while by another, and so forth, the resource in question (e.g., the CPU, or a network link) can be shared by many. The counterpart of time sharing is space sharing, where a resource is divided (in space) among those who wish to use it. For example, disk space is naturally a spaceshared resource; once a block is assigned to a file, it is normally not assigned to another file until the user deletes the original file.

显然,CPU并不适合space-sharing,因为在进程执行过程中,需要确保寄存器的值正确(这也是并行程序执行中的难点之一),限于寄存器的个数考虑,不太可能用space-sharing的方式同时执行多个进程

alicemare commented 5 years ago

OS对正在运行的程序的抽象即为进程,进程不单只有执行中的代码,它还与进程执行的环境(称为进程上下文)有着重要的联系

we can summarize a process by taking an inventory of the different pieces of the system it accesses or affects during the course of its execution

举例来说,执行两个sum2int程序A和B,接收不同参数,那么它们的进程上下文就不同,CPU在切换进程的时候就会先保留好A的上下文,再把B之前保留的上下文载入,让B从刚才停止的状态继续运行下去

当然,这只是“粗略”,“笼统”甚至有些“理想化”的描述

alicemare commented 5 years ago

插曲:“OS设计-机制与策略相分离”

提供机制,而不是策略 ——《UNIX编程艺术》

In many operating systems, a common design paradigm is to separate high-level policies from their low-level mechanisms [L+75]. You can think of the mechanism as providing the answer to a how question about a system; for example, how does an operating system perform a context switch? The policy provides the answer to a which question; for example, which process should the operating system run right now? Separating the two allows one easily to change policies without having to rethink the mechanism and is thus a form of modularity, a general software design principle.

抽象的来说,策略是接口,而机制是数据流 机制是泥坯,策略是可以定制的各种细节。例如:

  1. 管道是机制,应用管道处理各种数据是策略;
  2. AWK的读入行循环是机制,各种正则式与后附的处理操作是策略;
  3. 输入法是机制,码表是策略;
  4. 乐高积木的凸点与凹槽是机制,各种拼接方式是策略;