What is process?
Process can be seen as a dynamic instance of program, or say it's an instance of a computer program that is being executed./in execution.
Program其實(shí)就是指我們通常寫的程序野揪,這些都是靜態(tài)的代碼,當(dāng)這些程序被執(zhí)行的時(shí)候瞧栗,它們就叫做process斯稳,進(jìn)程。所以我們說迹恐,進(jìn)程就是執(zhí)行中的程序挣惰。
當(dāng)程序運(yùn)行的時(shí)候,操作系統(tǒng)會(huì)為這個(gè)程序提供它需要的資源殴边。例如memory for storing data, system resource like file system access. 一個(gè)進(jìn)程其實(shí)就是一個(gè)container, 其中存放了運(yùn)行需要的資源憎茂,和這段運(yùn)行的代碼。
A process is defined as an entity which represents the basic unit of work to be implemented in the system.
When a program is loaded into the memory and it becomes a process, it can be divided into four sections ─ stack, heap, text and data. The following image shows a simplified layout of a process inside main memory. ?Program指的就是我們平時(shí)寫的代碼锤岸。
what does a process include
Stack:The process Stack contains the temporary data such as method/function parameters, return address and local variables.
Heap:This is dynamically allocated memory to a process during its run time.
Text:This includes the current activity represented by the value of Program Counter and the contents of the processor's registers. 儲(chǔ)存處理器執(zhí)行的代碼竖幔。
Data: This section contains the global and static variables. 存儲(chǔ)全局和靜態(tài)變量。
States of Process
操作系統(tǒng)通常都支持多任務(wù)處理是偷,即多個(gè)進(jìn)程同時(shí)執(zhí)行拳氢,這樣的系統(tǒng)也叫做multi-processing, multi-tasking or time-sharing system。對(duì)于這種多任務(wù)同時(shí)處理蛋铆,并不需要很多CPU馋评,當(dāng)CPU的個(gè)數(shù)小于運(yùn)行的進(jìn)程的數(shù)量的時(shí)候,就會(huì)通過改變進(jìn)程的狀態(tài)刺啦,來安排這些進(jìn)程‘同時(shí)’運(yùn)行留特。
這里‘同時(shí)’并不是真正的多個(gè)進(jìn)程同時(shí)運(yùn)行,而是通過快速切換多個(gè)進(jìn)程玛瘸,讓用戶覺得好像這些進(jìn)程是同時(shí)在運(yùn)行的蜕青,實(shí)際上并不是。每個(gè)進(jìn)程占用CPU很短的時(shí)間捧韵,然后就放棄資源市咆,讓給下一個(gè)進(jìn)程汉操≡倮矗或者當(dāng)這個(gè)進(jìn)程在等待I/O的時(shí)候或其他很慢的操作的時(shí)候,也會(huì)放棄CPU資源磷瘤,進(jìn)入wait/block/sleep狀態(tài)芒篷,讓給其他進(jìn)程。
進(jìn)程的三種狀態(tài)
進(jìn)程執(zhí)行時(shí)的間斷性采缚,決定了進(jìn)程可能具有多種狀態(tài)针炉。事實(shí)上,運(yùn)行中的進(jìn)程可能具有以下三種基本狀態(tài)扳抽。
1)就緒狀態(tài)(Ready):
進(jìn)程已獲得除處理器外的所需資源篡帕,等待分配處理器資源殖侵;只要分配了處理器進(jìn)程就可執(zhí)行。就緒進(jìn)程可以按多個(gè)優(yōu)先級(jí)來劃分隊(duì)列镰烧。例如拢军,當(dāng)一個(gè)進(jìn)程由于時(shí)間片用完而進(jìn)入就緒狀態(tài)時(shí),排入低優(yōu)先級(jí)隊(duì)列怔鳖;當(dāng)進(jìn)程由I/O操作完成而進(jìn)入就緒狀態(tài)時(shí)茉唉,排入高優(yōu)先級(jí)隊(duì)列。
The process is waiting to be assigned to a processor. Ready processes are waiting to have the processor allocated to them by the operating system so that they can run. Process may come into this state afterStartstate or while running it by but interrupted by the scheduler to assign CPU to some other process.
2)運(yùn)行狀態(tài)(Running):
進(jìn)程占用處理器資源结执;處于此狀態(tài)的進(jìn)程的數(shù)目小于等于處理器的數(shù)目度陆。在沒有其他進(jìn)程可以執(zhí)行時(shí)(如所有進(jìn)程都在阻塞狀態(tài)),通常會(huì)自動(dòng)執(zhí)行系統(tǒng)的空閑進(jìn)程献幔。
Once the process has been assigned to a processor by the OS scheduler, the process state is set to running and the processor executes its instructions.
3)阻塞狀態(tài)(Blocked):
由于進(jìn)程等待某種條件(如I/O操作或進(jìn)程同步)懂傀,在條件滿足之前無(wú)法繼續(xù)執(zhí)行。該事件發(fā)生前即使把處理器資源分配給該進(jìn)程斜姥,也無(wú)法運(yùn)行鸿竖。[3]
Process moves into the waiting state if it needs to wait for a resource, such as waiting for user input, or waiting for a file to become available.
*如果計(jì)算機(jī)有多個(gè)cpu,或者一個(gè)cpu中有多核(multi cores)铸敏,那么我們就可以讓多個(gè)進(jìn)程真正的同時(shí)運(yùn)行(simultaneously)缚忧,每個(gè)進(jìn)程的狀態(tài)轉(zhuǎn)變也遵循上面的規(guī)律。?
Process Scheduling
什么時(shí)候進(jìn)程被從cpu上移開杈笔,接下來哪個(gè)進(jìn)程會(huì)被從queue里拿出來闪水,放在cpu上執(zhí)行是有一定規(guī)律的,這個(gè)規(guī)律取決于該操作系統(tǒng)用的什么scheduling algorithm蒙具。對(duì)于multiprogramming operating system來說球榆,process scheduling是必不可少的一部分。
Process Scheduling Queues
The Operating System maintains the following important process scheduling queues ?
Job queue? This queue keeps all the processes in the system.
Ready queue? This queue keeps a set of all processes residing in main memory, ready and waiting to execute. A new process is always put in this queue.
Device queues? The processes which are blocked due to unavailability of an I/O device constitute this queue.
程序用于描述進(jìn)程要完成的功能禁筏,是控制進(jìn)程執(zhí)行的指令集持钉;數(shù)據(jù)集合是程序在執(zhí)行時(shí)所需要的數(shù)據(jù)和工作區(qū);程序控制塊(Program Control Block篱昔,簡(jiǎn)稱PCB)每强,包含進(jìn)程的描述信息和控制信息,是進(jìn)程存在的唯一標(biāo)志州刽。
進(jìn)程具有的特征:
動(dòng)態(tài)性:進(jìn)程是程序的一次執(zhí)行過程空执,是臨時(shí)的,有生命期的穗椅,是動(dòng)態(tài)產(chǎn)生辨绊,動(dòng)態(tài)消亡的;
并發(fā)性:任何進(jìn)程都可以同其他進(jìn)程一起并發(fā)執(zhí)行匹表;
獨(dú)立性:進(jìn)程是系統(tǒng)進(jìn)行資源分配和調(diào)度的一個(gè)獨(dú)立單位门坷;
結(jié)構(gòu)性:進(jìn)程由程序宣鄙、數(shù)據(jù)和進(jìn)程控制塊三部分組成。
Most modern operating systems support multiple concurrent users(并行用戶默蚌,即同一個(gè)時(shí)間有多個(gè)user), and by implication multiple concurrent processes(同一時(shí)間有多個(gè)進(jìn)程). In such systems, the same program may be executed more than once at the same time, leading to several different processes all associated with the same program file. In the example, two of the users are executing the chrome command, resulting in two chrome processes.
現(xiàn)在很多操作系統(tǒng)通過實(shí)現(xiàn)同個(gè)時(shí)間多個(gè)進(jìn)程同時(shí)運(yùn)行框冀,來支持多并行用戶。在這樣的系統(tǒng)里敏簿,可能有些程序會(huì)被同個(gè)時(shí)間多次執(zhí)行明也,例如,兩個(gè)用戶同時(shí)都在執(zhí)行chrome command惯裕,就會(huì)開啟兩個(gè)chrome 進(jìn)程温数。
Most of the code that is executed in a process is that of the actual program. This is known as "user code". The user code of each process is well insulated from that of other processes. However the process may need the operating system to do some work for it. The process makes a call into the operating system, causing operating system code to run on its behalf. When this happens the process is said to be running in kernel mode.
一般來說, 應(yīng)用程序是在User mode中執(zhí)行程序蜻势,普通的數(shù)值計(jì)算或變量指派都可以在此模式完成撑刺,但是若要執(zhí)行一些危及系統(tǒng)安全的指令(例如對(duì)磁盤寫入資料),而這些指令是不準(zhǔn)在User mode中執(zhí)行的握玛,強(qiáng)要執(zhí)行那些特殊指令只會(huì)讓系統(tǒng)給你一個(gè)錯(cuò)誤信息而已够傍,應(yīng)用程序必須呼叫一些OS定義好的函數(shù)才能達(dá)成那些功能,例如printf()挠铲,這些OS事先定義好的函數(shù)我們稱為system call(系統(tǒng)調(diào)用)冕屯。
當(dāng)應(yīng)用程序執(zhí)行了system call,并不是傻傻地讓應(yīng)用程序想做什么就做什么拂苹,他們首先會(huì)嚴(yán)密地檢查這個(gè)調(diào)用的應(yīng)用程序的權(quán)限以及操作的內(nèi)容(是否讀取不屬于自己的存儲(chǔ)器范圍安聘,是否讀寫沒有權(quán)限讀寫的文件,是否想把資料往錯(cuò)誤的裝置送過去......)瓢棒,若是有任何錯(cuò)誤浴韭,system call將會(huì)停止執(zhí)行并回傳一個(gè)錯(cuò)誤代號(hào),讓應(yīng)用程序知道自己錯(cuò)在何處脯宿。相反地??一切檢查都沒問題念颈,system call將會(huì)通知CPU進(jìn)入Kernel mode,并依照應(yīng)用程序送過來的參數(shù)執(zhí)行特權(quán)指令连霉。當(dāng)特權(quán)指令執(zhí)行完畢榴芳,system call將會(huì)通知CPU返回User mode,并回到應(yīng)用程序中窘面。
為了不讓程序任意存取資源翠语,大部分的CPU架構(gòu)都支持Kernel mode與User mode兩種執(zhí)行模式叽躯。當(dāng)CPU運(yùn)行于Kernel mode時(shí)财边,任務(wù)可以執(zhí)行特權(quán)級(jí)指令,對(duì)任何I/O設(shè)備有全部的訪問權(quán)点骑,還能夠訪問任何虛擬地址和控制虛擬內(nèi)存硬件酣难;這種模式對(duì)應(yīng)x86的ring0層谍夭,操作系統(tǒng)的核心部分,包括設(shè)備驅(qū)動(dòng)程序都運(yùn)行在該模式憨募。當(dāng)CPU運(yùn)行于User Mode時(shí)紧索,硬件防止特權(quán)指令的執(zhí)行,并對(duì)內(nèi)存和I/O空間的訪問操作進(jìn)行檢查菜谣,如果運(yùn)行的代碼不能通過操作系統(tǒng)的某種門機(jī)制珠漂,就不能進(jìn)入內(nèi)核模式;這種模式對(duì)應(yīng)于x86的ring3層尾膊,操作系統(tǒng)的用戶接口部分以及所有的用戶應(yīng)用程序都運(yùn)行在該級(jí)別媳危。
*kernel:內(nèi)核。kernel可以看作一臺(tái)車子的發(fā)動(dòng)機(jī)冈敛,如果說操作系統(tǒng)是一臺(tái)車子的話待笑。