程序排程程式設計之優先數法的java實現
阿新 • • 發佈:2018-12-17
設計一個有幾個程序共行的程序排程程式,每個程序由一個程序控制塊(PCB)表示,程序控制塊通常應包括下述資訊:程序名,程序優先數,程序需要執行的時間,佔用CPU的時間以及程序的狀態等,且可按照排程演算法的不同而增刪。
系統應能顯示或列印各程序狀態和引數的變化情況,便於觀察。
本程式使用優先數法對程序進行排程。每個程序處於執行R(run)、就緒W(wait)和完成F(finish)三種狀態之一, 並假定起始狀態都是就緒狀態W。
為了便於處理,程式中程序的執行時間以時間片為單位計算。各程序的優先數或輪轉時間片數、以及程序需要執行的時間片數,均由偽隨機數發生器產生。
程序就緒鏈按優先數大小從大到小排列,鏈首程序首先投入執行。每過一個時間片,執行程序所需執行的時間片數減1,說明它已運行了一個時間片,優先數也減3。理由是該程序如果在一個時間片中完成不了,優先順序應降低一級。接著比較現行程序和就緒鏈鏈首程序的優先數,如果仍是現行程序高或者相同,就讓現行程序繼續執行,否則,排程就緒鏈鏈首程序投入執行。原執行程序再按其優先數大小插入就緒鏈,且改變它們對應的程序狀態,直至所有程序都執行完各自的時間片數。
程序控制塊結構如下:
程序控制塊鏈結構如下:
程式框圖如下圖所示(左邊):
主類
package report; import java.util.ArrayList; import java.util.Collections; import java.util.List; public class N2 { // cpu總工作時間 private static int allTime; // 一個時間片 private static int pieceTime = 1; // current private static Pcb currentPcb; // wait鏈 private static List<Pcb> waitList = new ArrayList<Pcb>(); // finish鏈 private static List<Pcb> finishList = new ArrayList<Pcb>(); public static void main(String[] args) { // 初始化wait鏈 Pcb Pcb1 = new Pcb(1, (int) (Math.random() * 10 + 1), (int) (Math.random() * 10 + 1), 0, "wait"); Pcb Pcb2 = new Pcb(2, (int) (Math.random() * 10 + 1), (int) (Math.random() * 10 + 1), 0, "wait"); Pcb Pcb3 = new Pcb(3, (int) (Math.random() * 10 + 1), (int) (Math.random() * 10 + 1), 0, "wait"); Pcb Pcb4 = new Pcb(4, (int) (Math.random() * 10 + 1), (int) (Math.random() * 10 + 1), 0, "wait"); Pcb Pcb5 = new Pcb(5, (int) (Math.random() * 10 + 1), (int) (Math.random() * 10 + 1), 0, "wait"); waitList.add(Pcb1); waitList.add(Pcb2); waitList.add(Pcb3); waitList.add(Pcb4); waitList.add(Pcb5); priority(); } private static void priority() { // 生成並按優先數大小排列程序控制塊鏈 Collections.sort(waitList); while (!waitList.isEmpty()) { // 鏈首程序投入執行,將鏈首程序移出鏈 currentPcb = waitList.get(0); waitList.remove(0); currentPcb.setStatus("【run】"); while (currentPcb.getNeedTime() > 0) { // 開始時間片 costPieceTimeInPriority(); // 時間片結束 if (currentPcb.getNeedTime() == 0) { // 撤銷該程序,設定status為finish,放到finish鏈 currentPcb.setStatus("finish"); finishList.add(currentPcb); break; } else { if (!waitList.isEmpty()) { // 優先數大於鏈首程序 if (currentPcb.getPriority() >= waitList.get(0).getPriority()) { continue; } else { // 撤銷該程序,設定status為wait,按priority放到wait鏈中 currentPcb.setStatus("wait"); waitList.add(currentPcb); Collections.sort(waitList); System.out.println("\n備註:程序" + currentPcb.getId() + "的priority小於wait鏈首,故重新插入wait鏈中\n"); break; } } else { continue; } } } } printFinishList(); } private static void printAllLists() { System.out.println("id\t needTime\t priority\t costTime\t status\n"); // finish鏈 for (Pcb pcb : finishList) System.out.println(pcb.getId() + "\t\t" + pcb.getNeedTime() + "\t\t" + pcb.getPriority() + "\t\t" + pcb.getCostTime() + "\t\t" + pcb.getStatus()); // current System.out.println(currentPcb.getId() + "\t\t" + currentPcb.getNeedTime() + "\t\t" + currentPcb.getPriority() + "\t\t" + currentPcb.getCostTime() + "\t\t" + currentPcb.getStatus()); // wait鏈 for (Pcb pcb : waitList) System.out.println(pcb.getId() + "\t\t" + pcb.getNeedTime() + "\t\t" + pcb.getPriority() + "\t\t" + pcb.getCostTime() + "\t\t" + pcb.getStatus()); System.out.println( "==========================================================================allTime:" + allTime); } private static void printFinishList() { System.out.println("id\t needTime\t priority\t costTime\t status\n"); // finish鏈 for (Pcb pcb : finishList) System.out.println(pcb.getId() + "\t\t" + pcb.getNeedTime() + "\t\t" + pcb.getPriority() + "\t\t" + pcb.getCostTime() + "\t\t" + pcb.getStatus()); System.out.println( "==========================================================================allTime:" + allTime); } private static void costPieceTimeInPriority() { printAllLists(); // 開始一個時間片 try { Thread.sleep(pieceTime); } catch (InterruptedException e) { } // 時間片到,當前程序需要時間片數-1 int currentPcbNeedTime = currentPcb.getNeedTime() - 1; currentPcb.setNeedTime(currentPcbNeedTime); // 時間片到,當前程序優先數-3 int currentPcbPriority = currentPcb.getPriority() - 3; currentPcb.setPriority(currentPcbPriority); // 時間片到,當前程序佔用片數+1 int currentPcbCostTime = currentPcb.getCostTime() + 1; currentPcb.setCostTime(currentPcbCostTime); // 時間片到,cpu總工作時間+1 allTime++; } private static void costPieceTimeInRoundRobin() { printAllLists(); // 開始一個時間片 try { Thread.sleep(pieceTime); } catch (InterruptedException e) { } // 時間片到,當前程序需要時間片數-1 int currentPcbNeedTime = currentPcb.getNeedTime() - 1; currentPcb.setNeedTime(currentPcbNeedTime); // 時間片到,當前程序佔用片數+1 int currentPcbCostTime = currentPcb.getCostTime() + 1; currentPcb.setCostTime(currentPcbCostTime); // 時間片到,cpu總工作時間+1 allTime++; } }
pcb類
package report;
public class Pcb implements Comparable<Object> {
private int id;
private int priority;
private int needTime;
private int costTime;
private String status;
Pcb(int id, int priority, int needTime, int costTime, String status) {
this.id = id;
this.priority = priority;
this.costTime = costTime;
this.needTime = needTime;
this.status = status;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public int getPriority() {
return priority;
}
public void setPriority(int priority) {
this.priority = priority;
}
public int getCostTime() {
return costTime;
}
public void setCostTime(int costTime) {
this.costTime = costTime;
}
public int getNeedTime() {
return needTime;
}
public void setNeedTime(int needTime) {
this.needTime = needTime;
}
public String getStatus() {
return status;
}
public void setStatus(String status) {
this.status = status;
}
@Override
public int compareTo(Object o) {
Pcb s = (Pcb) o;
if (this.getPriority() <= s.getPriority())
return 1;
else
return -1;
}
}
執行過程部分截圖