OSTEP: Ch4: The Abstraction: The Process

 20th August 2020 at 2:19pm

对于现在操作系统,多个程序「同时」运行是很正常的事情。但是 CPU 只有这么多,如何使得多个程序同时运行?

核心问题How to provide the illusion of many CPUs?
解法Time sharing(分时复用)

Time sharing 即是 把 CPU 工作的时间分配到不同应用程序去。类似的还有 space sharing,比如硬盘就是 space sharing 的,分到不同文件中。

进程 是 OS 对于运行程序的抽象。由于 CPU 需要进行 进程切换,因此进程的一些状态信息需要被保存在内存中,比如:

  • CPU 寄存器(包含了 program counter 和 stack pointer,分别用来确定进程运行到哪一条指令,以及栈在内存中的位置)
  • 进程的内存空间中的内容
  • I/O 相关的信息,比如打开了哪些文件、文件是只读还是可写等

由于进程需要被切换,因此进程至少有三种基础 状态:running、ready to run 和 blocked。

操作系统需要调度不同的进程,因此需要维护一个进程列表。进程列表中每个进程的信息叫 process control block (PCB)

ASIDE: KEY PROCESS TERMS

  • The process is the major OS abstraction of a running program. At any point in time, the process can be described by its state: the contents of memory in its address space, the contents of CPU registers (including the program counter and stack pointer, among others), and information about I/O (such as open files which can be read or written).
  • The process API consists of calls programs can make related to processes. Typically, this includes creation, destruction, and other useful calls.
  • Processes exist in one of many different process states, including running, ready to run, and blocked. Different events (e.g., getting scheduled or descheduled, or waiting for an I/O to complete) transition a process from one of these states to the other.
  • A process list contains information about all processes in the system. Each entry is found in what is sometimes called a process control block (PCB), which is really just a structure that contains information about a specific process.