Java模擬操作系統(tǒng)進(jìn)程優(yōu)先級調(diào)度_第1頁
Java模擬操作系統(tǒng)進(jìn)程優(yōu)先級調(diào)度_第2頁
Java模擬操作系統(tǒng)進(jìn)程優(yōu)先級調(diào)度_第3頁
已閱讀5頁,還剩22頁未讀, 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡介

1、/ 進(jìn)程塊* 設(shè)計(jì)PCB及其數(shù)據(jù)結(jié)構(gòu): 進(jìn)程標(biāo)識數(shù):ID進(jìn)程優(yōu)先數(shù):PRIORITY (優(yōu)先數(shù)越大,優(yōu)先級越高)* 進(jìn)程已占用時間片: CPUTIME ,每得到一次調(diào)度,值加 1;* 進(jìn)程還需占用時間片: ALLTIME ,每得到一次調(diào)度,該值減 1,一旦運(yùn)行完畢,ALLTIME為0)進(jìn)程隊(duì)列指針:NEXT,用來將PCB 排成隊(duì)列* 進(jìn)程狀態(tài): STATE (一般為就緒,可以不用) 設(shè)計(jì)進(jìn)程就緒 隊(duì)列及數(shù)據(jù)結(jié)構(gòu); 設(shè)計(jì)進(jìn)程調(diào)度算法, 并畫出程序流程圖; 設(shè) 計(jì)輸入數(shù)據(jù)和輸出格式;* 結(jié)構(gòu)格式:當(dāng)前正運(yùn)行的進(jìn)程: 0 當(dāng)前就緒隊(duì)列: 2, 1, 3, 4 編程上機(jī),驗(yàn)證結(jié)果*/public cl

2、ass PCB privateintid;privateintpriority ;privateintcpuTime ;privateintallTime ;private int state ;/ 狀態(tài)為 1的時候表示準(zhǔn)備就緒/*無參數(shù)的構(gòu)造方法,通過geter,seter器來對PCB的信息進(jìn)行獲取的修改*/public PCB() /*初始化PCB的基本信息的構(gòu)造方法* paramid* parampriority* paramcpuTime* paramallTime* paramstate*/public PCB(int id, int priority, int cpuTime, i

3、nt allTime, intstate) super ();this .id = id;this .priority = priority; this .cpuTime = cpuTime;this .allTime = allTime; this .state = state;public int getId() return id;public void setId( int id) this .id = id;public int getPriority() return priority ;public void setPriority( int priority) this .pr

4、iority = priority;*根據(jù)要求來修改PCB的優(yōu)先級*/public void modifyPriority() if (0 < this .priority ) this .priority -= 3;return ;public int getCpuTime() return cpuTime ;public void setCpuTime( int cpuTime) this .cpuTime = cpuTime;/*根據(jù)要求修改CPU時間*/public void modifyCpuTime() this .cpuTime += 1;public int getAll

5、Time() return allTime ;public void setAllTime( int allTime) this .allTime = allTime;/*根據(jù)要求修改PCB占用的時間片*/public void modifyAllTime() if (0 < this .allTime ) this .allTime -= 1;returnpublic int getState() return state ;public void setState( int state) this .state = state;/*根據(jù)要求修改PCB的狀態(tài)*/public void

6、midifyState() /*打印顯示當(dāng)前PCB的全部信息*/public void showStatus() System. out .println( "PCB id=" + id + ", priority=" priority+ ", cpuTime=" + cpuTime + ", allTime=" +allTime + ", state="+ state + "" );/*修改PCB的全部信息*/public void modify() if (0 <

7、this .allTime ) this .allTime -= 1;this .cpuTime += 1;if (0 < this .priority ) this .priority -= 3;/ 采用鏈表存儲/*創(chuàng)建PCB的數(shù)據(jù)結(jié)構(gòu)author 擺渡戀人public class LineListNode<T> private LineListNode<T> node ; private T element ;/* 創(chuàng)建空鏈表*/publicLineListNode() this.node = null ;this.element = null ;/* 創(chuàng)建一

8、個存儲特定元素的線性表*/publicLineListNode(T element) this.node = null ;this.element = element;/* 返回當(dāng)前結(jié)點(diǎn)點(diǎn)的下一個節(jié)點(diǎn)public LineListNode<T> getNextNode() return this .node ;/* 設(shè)置當(dāng)前結(jié)點(diǎn)的下一個結(jié)點(diǎn)*/public void setNextNode(LineListNode<T> node) this .node = node;* 返回當(dāng)前結(jié)點(diǎn)存儲的數(shù)據(jù)元素*/public T getElement() return this

9、.element ;/*設(shè)置當(dāng)前結(jié)點(diǎn)存儲的數(shù)據(jù)元素*/public void setElement(T element) this .element = element;package xiao.zhang.backup;import xiao.zhang.osa.LineListNode;/*創(chuàng)建一個存儲PCB用來線性鏈表,通過線性鏈表的相關(guān)操作來實(shí)現(xiàn)相應(yīng)的功能* author XiaoZhang*/ public class LineListPcb<PCB> /*表示PCB線性表的長度*/private int size;/*表示PCB線性表的頭元素結(jié)點(diǎn)private Line

10、ListNode<PCB> headPcb ;/創(chuàng)建一個具有特定元素的PCB線性存儲鏈表 */public LineListPcb(LineListPcb<PCB> llp) this .size = llp.getSize();this .headPcb = llp.getHeadPcb();this .lastPcb = llp.getLastPcb();表示PCB線性表尾元素結(jié)點(diǎn)*/private LineListNode<PCB> lastPcb ;*創(chuàng)建一個空的PCB線性存儲鏈表*/public LineListPcb() this .size =

11、 0;this .headPcb = null ;this .lastPcb = this .headPcb ;/*從PCB線性表中的頭部移除數(shù)據(jù)元素* param element*/public void removeElement() if (0 = this .size ) return ;/* 這一段主要是為了處理移除數(shù)據(jù)元素后只剩下一個數(shù)據(jù)元 素的時候的線性鏈表處理*/if (this .size = 1) / this.headPcb.setNextNode(this.lastPcb);/ this.lastPcb = this.headPcb;/ this.lastPcb.set

12、NextNode(null);this .headPcb = null ;this .size -;returnLineListNode<PCB> tempNode = this .headPcbthis .headPcb = tempNode.getNextNode();tempNode = null ;this .size -;/ param element */public void addElement(PCB element) LineListNode<PCB> newElement = newLineListNode<PCB>(element);

13、if (this .headPcb = null ) /* 頭結(jié)點(diǎn)為空, 表示鏈表中當(dāng)前沒有數(shù)據(jù)元素,點(diǎn)和尾結(jié)點(diǎn)指向同一內(nèi)存空間*/說明頭結(jié)向PCB線性表中的添加數(shù)據(jù)元素this .headPcb = newElement;/從PCB線性表中的頭部移除數(shù)據(jù)元素,并將其添加到 PCB線性 表中的尾部 */public void removeFromHeadAddToLast() 新添加一個數(shù)據(jù)元素則仍然指向同一內(nèi)存空間*/this .lastPcb = this .headPcb ; else /* 鏈表不為空, 在這里要保證最后一個結(jié)點(diǎn)與新加入的結(jié)點(diǎn)連接在一起*/this .lastPcb .

14、setNextNode(newElement);this .lastPcb = newElement; this .size +;if (this .size <= 1) return ;LineListNode<PCB> tempNode =this .headPcb ;this .headPcb = tempNode.getNextNode();this .lastPcb .setNextNode(tempNode);this .lastPcb = tempNode;/*從PCB線性表中中的尾部移除數(shù)據(jù)元素,并將其添加到線性表中的頭部*/PCB的public void r

15、emoveFromLastAddToHead() if (0 = this .size ) returnLineListNode<PCB> tempNode =this .lastPcb ;/*設(shè)置尾結(jié)點(diǎn)的以一個結(jié)點(diǎn)為空*/this .headPcb ;LineListNode<PCB> lastSecondNode =/*i=1表示指向PCB線性表的元素為線性表的第一個元素* i=this.size -1 表示指向線性表的元素為線性表的末尾第 二個數(shù)據(jù)元素*/for (int i = 1; i < this .size - 1; i+) lastSecondNo

16、de = lastSecondNode.getNextNode();lastSecondNode.setNextNode( null );*設(shè)置PCB線性表尾結(jié)點(diǎn)的下一個結(jié)點(diǎn)為頭結(jié)點(diǎn)的下一個結(jié)點(diǎn),頭結(jié)點(diǎn)為為尾結(jié)點(diǎn)*/tempNode.setNextNode( this .headPcb .getNextNode();this .headPcb = tempNode;/* 并根據(jù)線性表中的數(shù)據(jù)元素的優(yōu)先級從高到低的順序?qū)㈩^數(shù) 據(jù)元素放置在合適的位置*/* 基本思想是:* 1. 通過獲得頭數(shù)據(jù)元素,并且得到其優(yōu)先級* 2. 獲得鏈表的下一個數(shù)據(jù)元素 tempNode ,并得到其優(yōu)先級* 3. 通過

17、比較頭數(shù)據(jù)元素的優(yōu)先級 priority 和通過線性表獲得的 數(shù)據(jù)元素的優(yōu)先級 tempPrority 的比較* 3.1 如果大于等于則將頭數(shù)據(jù)元素插入在當(dāng)前( tempNode ) 的數(shù)據(jù)元素的后面* 3.2 如果小于則繼續(xù) 2-3步*/public void insertPCBByPrority() if (this .size = 1) return ;LineListNode<PCB> headNode =this .headPcb ;int currentPrority = (xiao.zhang.osa.PCB) headNode.getElement().getPri

18、ority();LineListNode<PCB> nextNode = headNode.getNextNode();int nextPrority = (xiao.zhang.osa.PCB) nextNode.getElement().getPriority();if (currentPrority > nextPrority) return ; else this .headPcb = nextNode;for (int i = 2; i < this .size ; i+) LineListNode<PCB> nextNextNode = next

19、Node.getNextNode();int nextNextPrority = (xiao.zhang.osa.PCB) nextNextNode.getElement().getPriority();if (currentPrority < nextPrority&& currentPrority > nextNextPrority) nextNode.setNextNode(headNode); headNode.setNextNode(nextNextNode); return ;nextNode = nextNextNode;/ nextNode.setN

20、extNode(nextNextNode);nextPrority = nextNextPrority;this .lastPcb .setNextNode(headNode);headNode.setNextNode( null );this .lastPcb = headNode;/*判斷PCB線性表是否為空 */public boolean isEmpty() return this .size = 0;/* return the headPcb*/public LineListNode<PCB> getHeadPcb() return this .headPcb ;/* r

21、eturn the lastPcb*/public LineListNode<PCB> getLastPcb() return this .lastPcb ;/* return PCB*/public PCB getPCB() return this .headPcb .getElement();* paramheadPcb*the headPcb to set*/publicvoid setHeadPcb(LineListNode<PCB> headPcb) this .headPcb = headPcb;/* return the size*/public int

22、getSize() return this .size ;* param size*the size to set*/public void setSize( int size) this .size = size;主方法:public class PCBProcess / param p* param llp */private static void comeLineList(PCB p, LineListPcb<PCB>* 實(shí)現(xiàn)進(jìn)程進(jìn)如就緒隊(duì)列的時后進(jìn)行由優(yōu)先級由高到低的順序進(jìn)入llp) for (int i = 0; i < p.length; i+) int minIndex = i;for (int j = i + 1; j < p.length; j+) if (pminIndex.getPriority() < pj.getPriority() /* 在這里也可以直接對 PCB 線性鏈表添加數(shù)據(jù)元 素*/minIndex = j;break;if (minIndex != i) PCB temp = pminIndex; pminIndex = pi;pi = temp;/*向PCB線性表中添加數(shù)據(jù)元素*/llp.addElement(pi);public static v

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論