【正文】
操作系統(tǒng) 設(shè)計(jì)與實(shí)現(xiàn) Chapter 2 Processes *The importance of process in an operating system Introduction to processes *mon Parallelism==Pseudoparallelism *It is the cpu rapid switching back and forth *multiprocessor is the real parallelism *people design a model , sequential processes 順序進(jìn)程 The process model Characters: * All the runnable software are anized into a number of sequential processes。 (in this chapter we called it processes) * The process is an executing program。 ?The process include the values of the all the program counter, registers, and variables。(進(jìn)程包括程序計(jì)數(shù)器、寄存器和變量的當(dāng)前值。) To these characters , it is easy to analysis the collection of the processes than keep track the rapid switch of CPU A B C D One prog count A B C D Four prog count A B C D time processes ProcessProgram Process is an executing program, it has program, input, output, and state. An example: scientist prepare the cake for his daughter. Recipe: /resipi/ 食譜 **** A Process is an activity of some kind, It has a program ,input, output, and a state, A single processor may be shared among several processes, with some scheduling algorithm being used to determine when to stop work on one process and service a different one. 理解進(jìn)程和程序的區(qū)別: CPU: 計(jì)算機(jī)科學(xué)家 程序 1: 烘制生日蛋糕的食譜 數(shù)據(jù): 面粉、雞蛋、糖和香草汁等 對(duì)應(yīng)的進(jìn)程 1: 閱讀食譜、取來各種原料以及烘制蛋糕的一系列動(dòng)作的總和。 事件: 女兒被蜜蜂螫傷 保存進(jìn)程 1的當(dāng)前狀態(tài): 計(jì)算機(jī)科學(xué)家就記錄下自己照著食譜做到哪兒了。 程序 2: 急救手冊(cè) 數(shù)據(jù): 藥物等 對(duì)應(yīng)的進(jìn)程 2: 實(shí)施醫(yī)療救治(高優(yōu)先級(jí)進(jìn)程) 當(dāng)蜜蜂螫傷處理完之后,計(jì)算機(jī)科學(xué)家又回來做蛋糕,從他離開時(shí)的那一步繼續(xù)做下去。 進(jìn)程的創(chuàng)建 1. 系統(tǒng)初始化 (1) 前臺(tái)進(jìn)程:同用戶交互并替它們完成工作的哪些進(jìn)程。 (2) 后臺(tái)進(jìn)程:守護(hù)進(jìn)程,處理網(wǎng)頁、打印之類活動(dòng)的進(jìn)程。 。 。 在交互式系統(tǒng)中,用戶可以通過鍵入命令啟動(dòng)程序。 4. 批處理作業(yè)的初始化 在操作系統(tǒng)認(rèn)為有資源運(yùn)行另一個(gè)作業(yè)時(shí),它創(chuàng)建一個(gè)新的進(jìn)程,并運(yùn)行其輸入隊(duì)列中的一個(gè)作業(yè)。 進(jìn)程的終止 1. 正常退出: 多數(shù)進(jìn)程由于完成了它們的工作而終止。 2. 出錯(cuò)退出(自愿) :進(jìn)程發(fā)現(xiàn)了嚴(yán)重錯(cuò)誤。 3. 嚴(yán)重錯(cuò)誤(非自愿): 通常是由于程序中的錯(cuò)誤所致。例如,執(zhí)行了一條非法指令,引用了不存在的內(nèi)存,或除數(shù)是零。 4. 被其它進(jìn)程殺死: 當(dāng)一個(gè)進(jìn)程終止時(shí),由該進(jìn)程所創(chuàng)建的所有進(jìn)程也都立即被殺死。 Process hierarchies Any OS, to support process, it must provide some way to create all the processes needed. Use fork( ) to create a new process, the father process can create his child processes,0~~~more, and later ,the process tree may appeared. In Minix, the root is init, and it works in this way: read info, create terminal, start program, NFS, SMTP , WWW,FTP…… Process States 進(jìn)程之間經(jīng)常需要交互、通信以及和其他進(jìn)程同步。 For Communication between processes exchange info among them, but, once more than one processes ? lead to block **logically, it have to stop , it will. (wait our input) **logically, it should continue, but it stopped, now, the CPU may be occupied by another process. 1. 運(yùn)行態(tài)( Running,在該時(shí)刻實(shí)際占用處理機(jī))。 2. 就緒態(tài)( Ready,可運(yùn)行,因?yàn)槠渌M(jìn)程正在運(yùn)行而暫時(shí)被掛起)。 3. 阻塞態(tài)( Blocked,除非某種外部事件發(fā)生,否則不能運(yùn)行)。 Running Blocked Ready 1 2 3 4 1. Process blocks for input 2. Scheduler picks another process 3. Scheduler picks this process 4. Input bees available Three States of process, and four transition are there 1 2 3 … … n3 n2 n1 … Scheduler User process Disk process Terminal process In this model, we don’t care about the interrupt Implementation of Processes **Process Table (an array of structures)(進(jìn)程表) each process is a record of this table, and each record include the state of the process, program counter, stack pointer, and allocation of memory…..。 thus, when the process is in ready state, all the info won’t lose. In MINIX, the process management, memory management, and file management are each handled by separate modules within the system ,so the process table is partitioned. Look at the pic: Interrupt Vector(中斷向量) An interruption ,it relate to each kinds of I/O device (hardware), it contain the address of the interrupt service procedure. The interrupt procedure store the current variables and info to stacks, and did some works related to it’s recover . Now ,the pre process is stored, and the new one could continue. When it is finished, the old one could be recalled ,and runs smoothly, just nothing had happened. Of course the interrupt must related to their priority. The real procedure is: Threads(線程) 1. Concept to the traditional process, one process just has one control clue(控制流) and one program counter. In modern OS, more and more OS support the multicontrol clue in a process, and we call these control clue threads. The real application of threads model from the application to show it’s importance to OS 多線程的應(yīng)用( 1) explorer scape(網(wǎng)絡(luò)瀏覽器) 許多 Web頁面都包含有多幅很小的圖像。 site: the images folder lies D:\site\images process: build up one connection threads: build up more connection 在瀏覽器內(nèi)設(shè)立多個(gè)進(jìn)程,同時(shí)請(qǐng)求傳輸多幅圖像,可節(jié)省建立和釋放鏈接的時(shí)間。 To small size files, the connectionbuilding needs more time than transporting. transmitting use .rar .zip than folder. 多線程的應(yīng)用( 2) 網(wǎng)絡(luò)螞蟻 (NetAnts)是從因特網(wǎng)下載文件的工具軟件,設(shè)計(jì)特點(diǎn)如下: (A)支持 HTTP和 FTP協(xié)議,可同時(shí)下載 15個(gè)文件; (B)可隨時(shí)中止正在下載的任務(wù),任務(wù)將自動(dòng)保存當(dāng)前狀態(tài); (C)支持拖放,可從瀏覽器中將鏈接拖入任務(wù)列表; (D)裁剪板自動(dòng)監(jiān)視,并可指定將捕獲的文件類型; (E)捕獲瀏覽器的動(dòng)作,當(dāng)用戶在瀏覽器中單擊鏈接時(shí),網(wǎng)絡(luò)螞蟻將自動(dòng)激活。 A B To A: when sever is busy , it will stopped, the writing will stopped. save the destination to …. To B: when server is busy, other threads will still try.. the writing won’t stopped. use flashget ant 進(jìn)程和線程的區(qū)別 每個(gè)進(jìn)程項(xiàng) 每個(gè)線程項(xiàng) 地址空間 全局變量 打開文件 子進(jìn)程 定時(shí)器 信號(hào)和信號(hào)處理程序 統(tǒng)計(jì)信息 程序計(jì)數(shù)器 寄存器 堆棧 狀態(tài) 允許在同一個(gè)進(jìn)程環(huán)境中有多個(gè)執(zhí)行流(線程),這些流在很大程度上相對(duì)獨(dú)立,但共享相同的地址空間。 進(jìn)程用來集合資源,而線程是 CPU中調(diào)度的實(shí)體。 To B: these threads share the same address space And these threads can write the information to the same place ,the same file. So the table will can be used to threads,then the threads can be restore、 block 、 ready …. Meaning : all the program can use the resou