(转载)虚拟化(1):进程概述(The Process)

转自:https://zhuanlan.zhihu.com/p/37917981

这一章主要是对如下问题的解释。

1.Process(进程)是什么?

简单说process就是一个运行中的程序。

2.怎么去虚拟出很多CPU?

操作系统可以让一个进程运行,然后停止这个进程让另外一个进程运行,只要交替的时间足够快,使用者就觉得多个进程是在同时运行的。能够让这个过程实现的基本技术就是time sharing,这可以让cpu可以去实现上述进程的切换。

3.Process的主要组成部分有哪些?

1.Process对应的内存

2.寄存器

3.I/O

4.Process提供的API

1.create

2.destroy

3.wait

4.Miscellaneous control。比如暂停和恢复。

5.status

5.进程创建的更多内容

1.首先将磁盘上的程序加载到程序中,现代os不会一次性加载所有的代码,而是需要哪些就加载哪些。

2.分配stack内存,并且初始化

3.分配heap内存。

4.剩余的一些初始化操作,尤其是I/O相关的。UNUX系统会有默认的三个file description,分别是standard input,output,error。

5.将PC置为main代码处,开始执行。

6.进程状态

Running Ready Blocked

Running表示这个程序正在cpu上运行。

Ready表示程序可以随时被os调度运行。

Blocked表示进程在一些事件没有发生前不能运行。比如进程在等待I/O操作完成的时候。

7.Process 相关的数据结构

os也是程序,和其他的程序一样,也需要一些数据结构来保存各种各样的信息。对于进程,为了能够跟踪他们的状态并且能进行调度等动作,os也会创建相应的数据结构来保存进程的相应的信息。下面这个表格就是xv6内核的进程信息。在其他的操作系统,比如linux,mac os或者是wndows内,也有类似的数据结构,可以通过这个简单的例子来看下到底os要保存哪些进程的信息。

// 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
//xv6内核给每个进程保存的信息,包括寄存器信息和一些状态信息
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//进程ID
struct proc *parent; // Parent 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
struct context context; // Switch here to run process
struct trapframe *tf; // Trap frame for the
                     // current interrupt
};
原文地址:https://www.cnblogs.com/kongweisi/p/15162467.html