作業系統程序排程演算法(Java 實現)
阿新 • • 發佈:2018-12-26
FCFS(First Come First Server,先來先服務)
這是最簡單,最基本的演算法,它的思想非常簡單,就是按照程序到來的時間順序,逐個分配 CPU 資源
優點:簡單,方便
缺點:效率低,資源利用率低
/**
* CPU 佔用情況
* 1: 空閒
* 0: 正被佔用
*/
static int CPU = 1;
/**
* 等待佇列長度
*/
static final int MAXLEN = 10;
/**
* 先來先服務演算法
* @param processes
*/
public static void FCFS(List<Process> processes){
int count = processes.size();
int time = 0;
int[] waitQueue = new int[MAXLEN];
int front = 0;int tail = 0;
int running = 0;
System.out.println("-------------FCFS演算法-------------");
if (count <= 0){
System.out.println("無可用程序");
return;
}
while (count > 0 ){
System.out.print("第 " + time + " 秒: ");
for (int i = 0; i < processes.size(); i++){
if (processes.get(i).isAlive() && processes.get(i).getInTime() == time){
System.out.print("程序 " + i + " 到來 ");
waitQueue[tail] = i;
tail = (tail+1 ) % MAXLEN;
}
if (processes.get(running).isAlive() && processes.get(running).getCount() == 0){
System.out.print("程序 " + running + " 結束執行 ");
processes.get(running).setAlive(false);
processes.get(running).setEndTime(time);
count--;
CPU = 1;
}
}
if (CPU == 1 && front != tail){
running = waitQueue[front];
front = (front+1) % MAXLEN;
System.out.print("程序 " + running + " 開始執行");
int temp = processes.get(running).getCount();
temp--;
processes.get(running).setCount(temp);
CPU = 0;
} else if (CPU == 0){
int temp = processes.get(running).getCount();
temp--;
processes.get(running).setCount(temp);
}
time++;
System.out.println();
}
System.out.println("---------------------------------");
ShowResult(processes);
}
SJF(Short Job First,短作業優先)
按照程序預計需要的執行時間,按照從小到大分配資源
優點:簡單程序執行速度快
缺點:無法準確預估執行時間,容易造成長程序飢餓
/**
* 短作業優先演算法
* 就是在 FCFS 演算法中加入對 waitQueue 等待佇列按照執行時間排序
*/
//改變部分
...
for (int i = 0; i < processes.size(); i++){
if (processes.get(i).isAlive() && processes.get(i).getInTime() == time){
System.out.print("程序 " + i + " 到來 ");
waitQueue[tail] = i;
tail = (tail+1) % MAXLEN;
length++;
/**
* 對等待佇列按程序執行時長按從小到大排序
*/
for (int x=front, z=0; z < length; x=(x+1)%MAXLEN, z++){
for (int y=x+1, q=0; q < length-x; y=(y+1)%MAXLEN, q++){
if (processes.get(waitQueue[x]).getCount() > processes.get(waitQueue[y]).getCount()){
int t = waitQueue[x];
waitQueue[x] = waitQueue[y];
waitQueue[y] = t;
}
}
}
}
...
PSA(優先順序排程)
按照程序的優先順序選擇排程順序
/**
* 優先順序排程演算法
* 就是將 SJF 演算法中的排序,改為按照優先順序排序
*/
/**
* 改變部分
* 對等待佇列按程序優先順序按從小到大排序
*/
for (int x=front, z=0; z < length; x=(x+1)%MAXLEN, z++){
for (int y=x+1, q=0; q < length-x; y=(y+1)%MAXLEN, q++){
if (processes.get(waitQueue[x]).getPriority() > processes.get(waitQueue[y]).getPriority()){
int t = waitQueue[x];
waitQueue[x] = waitQueue[y];
waitQueue[y] = t;
}
}
}
RR (時間片輪轉演算法)
為 CPU 的執行設定一個時間片大小,每個程序輪詢分配時間片,時間片結束後暫停執行加入等待佇列
/**
* 時間片輪轉演算法
* @param processes
* @param round
*/
public static void RR(List<Process> processes, int round){
int count = processes.size();
int time = 0;
int[] waitQueue = new int[MAXLEN];
int front = 0;int tail = 0;
int running = 0;
System.out.println("------------- RR 演算法-------------");
if (count <= 0){
System.out.println("無可用程序");
return;
}
while (count > 0){
System.out.print("第 " + time + " 秒: ");
for (int i = 0; i < processes.size(); i++){
if (processes.get(i).isAlive() && processes.get(i).getInTime() == time){
System.out.print("程序 " + i + " 到來 ");
waitQueue[tail] = i;
tail = (tail+1) % MAXLEN;
}
if (processes.get(running).isAlive() && processes.get(running).getCount() == 0){
System.out.print("程序 " + running + " 結束執行 ");
processes.get(running).setAlive(false);
processes.get(running).setEndTime(time);
count--;
CPU = 1;
}
}
if (CPU == 1 && front != tail){
running = waitQueue[front];
front = (front+1) % MAXLEN;
System.out.print("程序 " + running + " 開始執行");
int temp = processes.get(running).getCount();
temp--;
processes.get(running).setCount(temp);
CPU = 0;
} else if (CPU == 0){
int temp = processes.get(running).getCount();
temp--;
processes.get(running).setCount(temp);
if (time % round == 0){
System.out.print("程序 " + running + " 暫停執行");
waitQueue[tail] = running;
tail = (tail+1) % MAXLEN;
CPU = 1;
}
}
time++;
System.out.println();
}
System.out.println("---------------------------------");
ShowResult(processes);
}
演算法執行結果顯示
/**
* 輸出時間統計結果
* @param processes
*/
public static void ShowResult(List<Process> processes){
int averageTime = 0;
for (int i = 0; i < processes.size(); i++){
int inTime = processes.get(i).getInTime();
int endTime = processes.get(i).getEndTime();
averageTime += endTime-inTime;
System.out.println("程序 " + i + " : 到來時間: " +
inTime + " 結束時間: " +
endTime + " 週轉時間: " +
(endTime-inTime));
}
System.out.println("平均週轉時間: " + averageTime / processes.size());
System.out.println("---------------END--------------");
}