OSTEP虚拟化

进程的概念

进程:运行的程序(runing program)

通常情况下,一台pc上需要同时运行多个进程,以便提供易用性。

需要解决的问题:如何提供多个CPU的假象
->虚拟化CPU技术(分时复用CPU time sharing of CPU)

底层机制(mechanisms eg.上下文切换)+上层策略(policies eg.进程调度策略) 实现分时复用

进程的组成: 地址空间(包含指令和数据等)、寄存器(eg. IP、SP等)、持久存储设备

进程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.

进程创建: A Little More Detail

  1. 从磁盘将程序加载到内存地址空间
    早期操作系统: 一次全部加载指令和数据 -> 现代操作系统: 懒加载,每次只加载进程运行时需要的部分指令和数据(paging和swapping技术)

  2. 分配stack空间(C语言中用于局部变量、函数参数、返回地址),操作系统还需要将main;函数参数填充到stack中(eg. argc&argv)

  3. 分配heap(C语言中用于动态分配内存,malloc分配,free释放,heap大小根据请求动态增长)

  4. I/O相关操作(eg. 在unix系统中,每个进程默认关联三个打开的文件描述符:标准输入、标准输出和标准出错,更多I/O相关内存位于持久化部分)

进程状态

  • 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. A common example: when a process initiates an I/O request to a disk, it becomes blocked and thus some other process can use the processor.

操作系统数据结构

Information an OS needs to track about each process in the xv6 kernel:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
// the registers xv6 will save and restore
// to stop and subsequently restart a process
struct context {
int eip;
int esp;
int ebx;
int ecx;
int edx;
int esi;
int edi;
int ebp;
};
// the different states a process can be in
enum proc_state { UNUSED, EMBRYO, SLEEPING,
RUNNABLE, RUNNING, ZOMBIE };
// the information xv6 tracks about each process
// including its register context and state
struct proc {
char *mem; // Start of process memory
uint sz; // Size of process memory
char *kstack; // Bottom of kernel stack
// for this process
enum proc_state state; // Process state
int pid; // Process ID
struct proc *parent; // Parent process
void *chan; // If !zero, sleeping on chan
int killed; // If !zero, has been killed
struct file *ofile[NOFILE]; // Open files
struct inode *cwd; // Current directory
struct context context; // Switch here to run process
struct trapframe *tf; // Trap frame for the
// current interrupt
};

一些术语:

  • 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.

进程API (实用内容)

//todo

底层机制: Limited Direct Execution

虚拟化CPU->同时运行多个进程(的假象)
idea: 分时复用CPU
challenges:

  1. performance : 高性能
  2. control : OS保留控制能力

实现: 硬件支持+软件实现

基本技术: 限制直接执行(Limited Direct Execution)

the problem without “Limited”:

  1. 限制操作
  2. 进程切换

problem 1 : Restricted Operations
通过系统调用限制特权指令的执行:
即中断机制

problem 2 : Switching Between Processes

  1. A Cooperative Approach: Wait For System Calls
    通过系统调用陷入内核
  2. A Non-Cooperative Approach: The OS Takes Control
    时钟中断:使内核获得CPU控制

由时钟中断构造的进程切换有两次寄存器的保存和恢复,第一次发生在时钟中断发生时,由硬件将user registers保存到进程内核栈中,第二次发生在操作系统进行进程切换时,软件将kernel registers保存到进程控制块中。

处理中断/陷入时发生中断/陷入
并行部分将做详细讨论: 主要是 关中断和锁机制

上下文切换(context switch) -> 进程切换

-------------本文结束啦感谢您阅读-------------
0%