先来先服务的调度算法:最简单的调度算法,既可以用于作业调度 ,也可以用于程序调度,当作业调度中采用该算法时,系统将按照作业到达的先后次序来进行调度,优先从后备队列中,选择一个或多个位于队列头部的作业,把他们调入内存,分配所需资源、创建进程,然后放入“就绪队列”,直到该进程运行到完成或发生某事件堵塞后,进程调度程序才将处理机分配给其他进程。
算法的运用:
算法程序编写大致思路:对到达系统时间进行排序,依次设置其值。具体还是看备注吧~
JAVA实现如下:
工程目录:
FCFS类
package FCFS;
public class FCFS {
//TAG是FCFS类的名字
private final String TAG = FCFS.class.getSimpleName();
/**FCFS先来先服务算法*/
public int execute(ProcessFCFSModel[] processList) {
//如果进程对象为空或其长度为零则出错
if (processList == null || processList.length == 0) {
System.out.println(TAG + ">数据为空");
return -1;
}
ProcessFCFSModel[] fcfsModels = processList;
int runTimeSum = 0;
//按照到达时间进行排序
mysort(fcfsModels);
//将进程对象进行遍历
for (ProcessFCFSModel model : fcfsModels) {
//如果总时间小于进程到达的时间,总时间变为该进程到来的时间:
if (runTimeSum < model.getComingTime()) {
runTimeSum = (int)model.getComingTime();
}
//将总时间设置为进程开始执行的时间
model.setStartRunTime(runTimeSum);
//该进程运行后的总时间
runTimeSum += model.getRunTime();
//设置该进程结束的时间
model.setFinishTime(runTimeSum);
//设置该进程的周转时间
model.setTurnaroundTime(runTimeSum - model.getComingTime());
}
return runTimeSum;
}
/**选择排序*/
public ProcessFCFSModel[] mysort(ProcessFCFSModel[] processList){
ProcessFCFSModel temp;
for(int i=0;i<processList.length;i++){
for(int j=i+1;j<processList.length;j++){
if(processList[i].getComingTime()>processList[j].getComingTime()){
temp = processList[i];
processList[i] = processList[j];
processList[j] = temp;
}
}
}
return processList;
}
}
ProcessFCFSModel类
package FCFS;
/**
* FCFS的进程对象拥有的属性
*/
public class ProcessFCFSModel{
private String processId; // 进程标识
private long runTime; // 进程完整执行预计的时间
private long comingTime; // 到达时间
private long startRunTime; // 开始执行时间
private long finishTime; // 完成时间
private long turnaroundTime; // 周转时间
public ProcessFCFSModel(String processId, long runTime, long comingTime) {
this.processId = processId;
this.runTime = runTime;
this.comingTime = comingTime;
}
/**getter and setter*/
public long getComingTime() {
return comingTime;
}
public long getStartRunTime() {
return startRunTime;
}
public void setStartRunTime(long startRunTime) {
this.startRunTime = startRunTime;
}
public long getFinishTime() {
return finishTime;
}
public void setFinishTime(long finishTime) {
this.finishTime = finishTime;
}
public long getTurnaroundTime() {
return turnaroundTime;
}
public void setTurnaroundTime(long turnaroundTime) {
this.turnaroundTime = turnaroundTime;
}
public String getProcessId() {
return processId;
}
public long getRunTime() {
return runTime;
}
}
ProcessNumber类
package FCFS;
/**
*
*/
public class ProcessNumber {
public static ProcessFCFSModel[] getFCFSModelArray() {
/**自定义进程数量*/
int processnumber=4;
ProcessFCFSModel[] models = new ProcessFCFSModel[processnumber];
models[0] = new ProcessFCFSModel("A", 4, 0);
models[1] = new ProcessFCFSModel("B", 3, 1);
models[2] = new ProcessFCFSModel("C", 5, 100);
models[3] = new ProcessFCFSModel("D", 2, 3);
return models;
}
}
TestFCFS类
package FCFS;
/**
* 测试类
*/
public class TestFCFS {
public static void main(String[] args) {
TestFCFS schedule = new TestFCFS();
schedule.testFCFS();
System.out.println("---------------------------------------------------------------------------------");
}
private void testFCFS() {
/**进行FCFS算法进程调度*/
FCFS fcfs = new FCFS();
ProcessFCFSModel[] processArray = ProcessNumber.getFCFSModelArray();//创建进程
int timeSum = fcfs.execute(processArray);//总运行时间
int turnaroundSum = 0;//周转时间
/**打印结果*/
System.out.println("进程调度过程:
进程名 到达时间 服务时间 开始时间 完成时间 周转时间");
for (ProcessFCFSModel process : processArray) {
turnaroundSum += process.getTurnaroundTime();
System.out.println(process.getProcessId() + " " +
process.getComingTime() + " " + process.getRunTime() + " " +
process.getStartRunTime() + " " + process.getFinishTime() + " " +
process.getTurnaroundTime() + " ");
}
System.out.println("FCFS总运行时间:" + timeSum + "(ms)");
System.out.println("平均周转时间:" + String.format("%.2f", 1.0*(turnaroundSum / processArray.length)));
}
}
运行结果:
emmm这里没有带权周转。。不过加进去也很简单,自行加就好啦。