Java模擬操作系統進程優先級調度_第1頁
Java模擬操作系統進程優先級調度_第2頁
Java模擬操作系統進程優先級調度_第3頁
已閱讀5頁,還剩22頁未讀 繼續免費閱讀

下載本文檔

版權說明:本文檔由用戶提供并上傳,收益歸屬內容提供方,若內容存在侵權,請進行舉報或認領

文檔簡介

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

2、ass PCB privateintid;privateintpriority ;privateintcpuTime ;privateintallTime ;private int state ;/ 狀態為 1的時候表示準備就緒/*無參數的構造方法,通過geter,seter器來對PCB的信息進行獲取的修改*/public PCB() /*初始化PCB的基本信息的構造方法* 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;*根據要求來修改PCB的優先級*/public void modifyPriority() if (0 < this .priority ) this .priority -= 3;return ;public int getCpuTime() return cpuTime ;public void setCpuTime( int cpuTime) this .cpuTime = cpuTime;/*根據要求修改CPU時間*/public void modifyCpuTime() this .cpuTime += 1;public int getAll

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

6、midifyState() /*打印顯示當前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;/ 采用鏈表存儲/*創建PCB的數據結構author 擺渡戀人public class LineListNode<T> private LineListNode<T> node ; private T element ;/* 創建空鏈表*/publicLineListNode() this.node = null ;this.element = null ;/* 創建一

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

9、.element ;/*設置當前結點存儲的數據元素*/public void setElement(T element) this .element = element;package xiao.zhang.backup;import xiao.zhang.osa.LineListNode;/*創建一個存儲PCB用來線性鏈表,通過線性鏈表的相關操作來實現相應的功能* author XiaoZhang*/ public class LineListPcb<PCB> /*表示PCB線性表的長度*/private int size;/*表示PCB線性表的頭元素結點private Line

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

11、 0;this .headPcb = null ;this .lastPcb = this .headPcb ;/*從PCB線性表中的頭部移除數據元素* param element*/public void removeElement() if (0 = this .size ) return ;/* 這一段主要是為了處理移除數據元素后只剩下一個數據元 素的時候的線性鏈表處理*/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 ) /* 頭結點為空, 表示鏈表中當前沒有數據元素,點和尾結點指向同一內存空間*/說明頭結向PCB線性表中的添加數據元素this .headPcb = newElement;/從PCB線性表中的頭部移除數據元素,并將其添加到 PCB線性 表中的尾部 */public void removeFromHeadAddToLast() 新添加一個數據元素則仍然指向同一內存空間*/this .lastPcb = this .headPcb ; else /* 鏈表不為空, 在這里要保證最后一個結點與新加入的結點連接在一起*/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線性表中中的尾部移除數據元素,并將其添加到線性表中的頭部*/PCB的public void r

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

16、de = lastSecondNode.getNextNode();lastSecondNode.setNextNode( null );*設置PCB線性表尾結點的下一個結點為頭結點的下一個結點,頭結點為為尾結點*/tempNode.setNextNode( this .headPcb .getNextNode();this .headPcb = tempNode;/* 并根據線性表中的數據元素的優先級從高到低的順序將頭數 據元素放置在合適的位置*/* 基本思想是:* 1. 通過獲得頭數據元素,并且得到其優先級* 2. 獲得鏈表的下一個數據元素 tempNode ,并得到其優先級* 3. 通過

17、比較頭數據元素的優先級 priority 和通過線性表獲得的 數據元素的優先級 tempPrority 的比較* 3.1 如果大于等于則將頭數據元素插入在當前( tempNode ) 的數據元素的后面* 3.2 如果小于則繼續 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>* 實現進程進如就緒隊列的時后進行由優先級由高到低的順序進入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 線性鏈表添加數據元 素*/minIndex = j;break;if (minIndex != i) PCB temp = pminIndex; pminIndex = pi;pi = temp;/*向PCB線性表中添加數據元素*/llp.addElement(pi);public static v

溫馨提示

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

評論

0/150

提交評論