程序排程(一)——FIFO演算法
阿新 • • 發佈:2019-01-01
一 定義
這是最早出現的置換演算法。該演算法總是淘汰最先進入記憶體的頁面,即選擇在記憶體中駐留時間最久的頁面予以淘汰。該演算法實現簡單,只需把一個程序已調入記憶體的頁面,按先後次序連結成一個佇列,並設定一個指標,稱為替換指標,使它總是指向最老的頁面。但該演算法與程序實際執行的規律不相適應,因為在程序中,有些頁面經常被訪問,比如,含有全域性變數、常用函式、例程等的頁面,FIFO 演算法並不能保證這些頁面不被淘汰。這裡,我們只需要設定一個先進先出佇列就可以。最先進入記憶體的頁面最早被轉換出去。
二 程式碼
程序類如下:
import java.util.Iterator;
import java.util.Scanner;
import java.util.TreeSet;
public class Process implements Comparable<Process>{
private String ProcessName; //程序名
private int ReachTime; //到達時間
private int ProcessTime; //處理時間
private int FinishTime; //完成時間
private int PeriodTime; //週轉時間
private int StartTime; //開始時間
private double WeightedPeriodTime; //帶權週轉時間
private int Priority; //優先順序
public Process(String processname,int reachTime, int processTime) {
super();
ProcessName = processname;
ReachTime = reachTime;
ProcessTime = processTime;
}
public Process(String processName, int reachTime, int processTime, int priority) {
super();
ProcessName = processName;
ReachTime = reachTime;
ProcessTime = processTime;
Priority = priority;
}
public int getPriority() {
return Priority;
}
public String getProcessName() {
return ProcessName;
}
public int getReachTime() {
return ReachTime;
}
public int getProcessTime() {
return ProcessTime;
}
public int getFinishTime() {
return FinishTime;
}
public int getPeriodTime() {
return PeriodTime;
}
public void setProcessTime(int processTime) {
ProcessTime = processTime;
}
public void setFinishTime(int finishTime) {
FinishTime = finishTime;
}
public void setPeriodTime(int periodTime) {
PeriodTime = periodTime;
}
public int getStartTime() {
return StartTime;
}
public void setStartTime(int startTime) {
StartTime = startTime;
}
public double getWeightedPeriodTime() {
return WeightedPeriodTime;
}
public void setWeightedPeriodTime(double weightedPeriodTime) {
WeightedPeriodTime = weightedPeriodTime;
}
@Override
public int compareTo(Process o) {
// TODO Auto-generated method stub
if ( this.ReachTime > o.ReachTime)
return 1;
else if ( this.ReachTime < o.ReachTime)
return -1;
return 0;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ReachTime;
return result;
}
public void Print(){
System.out.print(this.ProcessName+" "+this.ReachTime+" "+this.ProcessTime+" "+" "+this.StartTime+" "+this.FinishTime+" "+this.PeriodTime+" ");
System.out.printf("%.4f",this.WeightedPeriodTime);
System.out.println();
}
}
FIFO演算法程式碼如下:
import java.util.Iterator;
import java.util.Scanner;
import java.util.TreeSet;
public class FIFO{
private TreeSet<Process> process = new TreeSet<Process>() ;
public FIFO() {
//新增程序
Scanner in = new Scanner(System.in);
System.out.println("請輸入要新增的程序數:");
int Num = in.nextInt();
System.out.println("開始初始化程序資訊(程序名 到達時間 耗時):");
for ( int i = 0 ; i < Num ; i++){
String processname = in.next();
int reachtime = in.nextInt();
int processtime = in.nextInt();
Process p = new Process(processname,reachtime, processtime);
process.add(p);
}
in.close();
}
void CarryOut_FIFO(){
//執行先來先服務排程演算法
Iterator<Process> it = this.process.iterator();
Process p0 = it.next();
p0.setStartTime(p0.getReachTime());
p0.setFinishTime(p0.getProcessTime()+p0.getStartTime());
p0.setPeriodTime(p0.getFinishTime()-p0.getReachTime());
p0.setWeightedPeriodTime(p0.getPeriodTime() *1.0 /p0.getProcessTime());
int starttime = this.process.first().getFinishTime();
while ( it.hasNext()){
Process p = it.next();
p.setStartTime(starttime);
p.setFinishTime(p.getProcessTime()+p.getStartTime());
p.setPeriodTime(p.getFinishTime()-p.getReachTime());
p.setWeightedPeriodTime(p.getPeriodTime() * 1.0 / p.getProcessTime());
starttime = p.getFinishTime();
}
}
public double Avg_ProcessTime(){
//平均週轉時間
double avg = 0;
Iterator<Process> it = this.process.iterator();
while( it.hasNext()){
Process p = it.next();
avg += p.getPeriodTime();
}
avg /= this.process.size();
return avg;
}
public double Avg_WeightedProcessTime(){
//平均帶權週轉時間
double avg = 0;
Iterator<Process> it = this.process.iterator();
while( it.hasNext()){
Process p = it.next();
avg += p.getWeightedPeriodTime();
}
avg /= this.process.size();
return avg;
}
public void Print(){
//列印
System.out.println(" 排程示意圖");
System.out.println("程序 到達時間 耗時 開始時間 完成時間 週轉時間 帶權週轉時間");
Iterator<Process> it = process.iterator();
while( it.hasNext()){
Process p = it.next();
p.Print();
}
System.out.printf("平均週轉時間:%.4f",this.Avg_ProcessTime());
System.out.println();
System.out.printf("平均帶權週轉時間:%.4f",this.Avg_WeightedProcessTime());
}
public static void main(String[] args) {
// TODO Auto-generated method stub
FIFO fifo = new FIFO();
fifo.CarryOut_FIFO();
fifo.Print();
}
}