1. 程式人生 > 其它 >(轉載)虛擬化(1):程序概述(The Process)

(轉載)虛擬化(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 };