程序排程程式設計之簡單輪轉法的java實現
阿新 • • 發佈:2018-12-17
設計一個有幾個程序共行的程序排程程式,每個程序由一個程序控制塊(PCB)表示,程序控制塊通常應包括下述資訊:程序名,程序優先數,程序需要執行的時間,佔用CPU的時間以及程序的狀態等,且可按照排程演算法的不同而增刪。
系統應能顯示或列印各程序狀態和引數的變化情況,便於觀察。
本程式使用簡單輪轉法對程序進行排程。每個程序處於執行R(run)、就緒W(wait)和完成F(finish)三種狀態之一, 並假定起始狀態都是就緒狀態W。
為了便於處理,程式中程序的執行時間以時間片為單位計算。各程序的優先數或輪轉時間片數、以及程序需要執行的時間片數,均由偽隨機數發生器產生。
程序就緒鏈按各程序進入的先後次序排列,程序每次佔用處理機的輪轉時間按其重要程度登入程序控制塊中的輪轉時間片數記錄項(相應於優先數法的優先數記錄項位置)。每過一個時間片,執行程序佔用處理機的時間片數加1,然後比較佔用處理機的時間片數是否與該程序的輪轉時間片數相等,若相等說明已到達輪轉時間,應將現執行程序排到就緒鏈末尾,排程鏈首程序佔用處理機,且改變它們的程序狀態,直至所有程序完成各自的時間片。
程序控制塊結構如下:
程序控制塊鏈結構如下:
程式框圖如下圖所示(右邊):
主類
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); roundRobin(); } private static void roundRobin() { while (!waitList.isEmpty()) { // 鏈首程序投入執行,將鏈首程序移出鏈 currentPcb = waitList.get(0); waitList.remove(0); currentPcb.setStatus("【run】"); while (currentPcb.getNeedTime() > 0) { // 開始時間片 costPieceTimeInRoundRobin(); // 時間片結束 if (currentPcb.getNeedTime() == 0) { // 撤銷該程序,設定status為finish,放到finish鏈 currentPcb.setStatus("finish"); finishList.add(currentPcb); break; } else { if (!waitList.isEmpty()) { // 佔用處理機時間片沒到 if (currentPcb.getPriority() > currentPcb.getCostTime()) { continue; } else { // 撤銷該程序,設定status為wait,放到wait鏈尾 currentPcb.setStatus("wait"); waitList.add(currentPcb); System.out.println("\n備註:程序" + currentPcb.getId() + "佔用處理機時間片到,故重新插到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;
}
}
執行過程部分截圖