《Operating Systems: Three Easy Pieces》(1)

背景

最近开始看 《Operating Systems: Three Easy Pieces》这本书,为了方便以后自己回顾和总结,想以文章的方式,做成笔记。现在的打算是看完书里的1 到 2 章内容后,写一篇文章,所以这将会是一系列的文章。前天刚好看完前三章,这将是第一篇笔记。

资源

因为是第一篇文章,所以会将自己的学习资源列举一下,这些学习的资源都是开源的:

  • 书籍: 《Operating Systems: Three Easy Pieces》
  • Project: https://github.com/remzi-arpacidusseau/ostep-projects
  • xv6: 一个专门用于学习的操作系统
  • 系统: 类 unix 系统

Process

这本书说的 three pieces,指的是 VirtualizationConcurrencyPersistence。所以我们首先学习的是 Virtualization,这其中最重要的就是 process。之前不管是工作中或是学习中经常会看到 process (也就是进程),会觉得是个非常抽象的概念,根本不知道到底是什么个东西。趁此机会,我们刚好可以仔细研究这个东西。

Definition

it is a running program

process 是一个程序,所以想要明白 process ,我们只需去搞清楚这个程序干了什么。

Why Virtualization

我们先来看下本书的第一部分 Virtualization 到底指什么,process 和这个 Virtualization 有什么关系?
首先,我们都知道任何一个操作系统都可以同时运行多个 program,可以肯定的是 CPU 的数量是远远少于 program。那么操作系统是如何在这种条件下同时运行多个 program ,这个问题的答案就是 process 的功能之一 virtualizing the CPU

CPU Virtualization

操作系统可以在运行一个 process 的时候,停止运行这个 process,然后运行其它 process,循环往复。对于 process 来说,不管物理 CPU 有几个,就好像它都有一个的专门的 virtualized CPU,来运行 process。这种技术也被称为 time sharing of the CPU。既然现在每个 process 都有了自己的 CPU,那在 process 加载了所要运行的 program 之后,对于 program 来说,就好像有一个专门的 CPU 在运行自己。

Implementation

那如何实现这个 CPU Virtualization,要从两方面着手:

  1. low-level machinery mechanisms
  2. policies

第一点书里举了个例子是 context switch,也就是我们如何从一个 process 的相关环境切换到另一个 process 的相关环境,这个之后会重点讲。第二点书里举的例子是 scheduling policy,也就是说我们什么时候去切换 process,切到哪个 process 等等的一些列决策问题。总结来说,我们要写 process 转换的代码和做转换的相关决策()。

Part Of Process

接着我们来看下 process 的组成部分,也就是所谓的 context

  • memory (内存)

One obvious component of machine state that comprises a process is its memory. Instructions lie in memory; the data that the running program reads and writes sits in memory as well. Thus the memory that the process can address (called its address space) is part of the process

  • registers (寄存器)
  • I/O information

Finally, programs often access persistent storage devices too. Such I/O information might include a list of the files the process currently has open.

Process API

之前我们说了 process 是一个程序,那一个程序必然会提供一些 API 来让其它程序使用,我们可以先简单的看几个 process API。

Create: An operating system must include some method to create new processes. When you type a command into the shell, or double-click on an application icon, the OS is invoked to create a new process to run the program you have indicated.
Destroy: As there is an interface for process creation, systems also provide an interface to destroy processes forcefully. Of course, many processes will run and just exit by themselves when complete; when they don’t, however, the user may wish to kill them, and thus an interface to halt a runaway process is quite useful.
Wait: Sometimes it is useful to wait for a process to stop running; thus some kind of waiting interface is often provided.
Miscellaneous Control: Other than killing or waiting for a process, there are sometimes other controls that are possible. For example, most operating systems provide some kind of method to suspend a process (stop it from running for a while) and then resume it (continue it running).
Status: There are usually interfaces to get some status information about a process as well, such as how long it has run for, or what state it is in.

这些 API 看名字应该也能知道大致的作用,这里我们简单地浏览下,后面的章节会去使用并解释这些接口。

Process Creation: A Little More Detail

我们再来看下 process 加载其它 program 的步骤:

  • load its code and any static data from disk into the address space of the process lazily
  • Some memory must be allocated for the program’s run-time stack (or just stack)
  • The OS will also likely initialize the stack with arguments; specifically, it will fill in the parameters to the main() function, i.e., argc and the argv array
  • the OS may also allocate some memory for the program’s heap
  • The OS will also do some other initialization tasks, particularly as related to input/output (I/O)
  • to start the program running at the entry point, namely main()

Process States

process 在运行的时候,有可能会处于不同的状态,我们先来看简化的状态:

  • Running

In the running state, a process is running on a processor. This means it is executing instructions

  • Ready

In the ready state, a process is ready to run but for some reason the OS has chosen not to run it at this given moment

  • Blocked

In the blocked state, a process has performed some kind of operation that makes it not ready to run until some other event takes place

上传一个书中的状态转移图:


processState.png

Data Structures

具体的 process 的数据结构

struct context {
  uint edi;
  uint esi;
  uint ebx;
  uint ebp;
  uint eip;
};

enum procstate { UNUSED, EMBRYO, SLEEPING, RUNNABLE, RUNNING, ZOMBIE };

// Per-process state
struct proc {
  uint sz;                     // Size of process memory (bytes)
  pde_t* pgdir;                // Page table
  char *kstack;                // Bottom of kernel stack for this process
  enum procstate state;        // Process state
  int pid;                     // Process ID
  struct proc *parent;         // Parent process
  struct trapframe *tf;        // Trap frame for current syscall
  struct context *context;     // swtch() here to run process
  void *chan;                  // If non-zero, sleeping on chan
  int killed;                  // If non-zero, have been killed
  struct file *ofile[NOFILE];  // Open files
  struct inode *cwd;           // Current directory
  char name[16];               // Process name (debugging)
};

这段代码来自 xv6 ,可以自己下载源码去看看

结尾

这本书简化很多了东西,读起来也是通俗易懂,可以作为我们操作系统的入门书。但是也不是说一点要求没有,很明显需要知道 C 语言。其次,因为涉及到栈,汇编等等,需要配合 CSAPP 一起看。CSAPP 这本书刚好我也在看,下几篇的文章我在记录操作系统知识点的时候,刚好也可以把在 CSAPP 学到的相关知识点也记录进来,知识的连接才是最重要的。

你可能感兴趣的:(《Operating Systems: Three Easy Pieces》(1))