import java.util.Scanner;
public class FCFS_SJF {
public static class Process{
public String name = "";
public int arrivalTime ;//到达时间
public int serviceTime ;//服务时间
public int finishTime;//完成时间
public int wholeTime;//周转时间
public double weightWholeTime;//带权周转时间
public int order;//运行次序
public boolean finish = false ;
}
static int n;
public static void main(String[] args) {
Process[] processArray = new Process[100];
System.out.println("请输入进程个数");
Scanner input = new Scanner(System.in);
n = input.nextInt();
System.out.println("请输入进程名,到达时间,服务时间");
for(int i = 0 ;i < n ;i++){
processArray[i] = new Process();
processArray[i].name = input.next();
processArray[i].arrivalTime = input.nextInt();
processArray[i].serviceTime = input.nextInt();
}
processArray[n] = new Process();
System.out.println("输入F选用FCFS算法,输入其他选用SJF算法");
String algorithm = input.next();
switch(algorithm){
case "F":
FCFS(processArray);
break;
default:
SJF(processArray);
}
// 0 4 1 3 2 5 3 2 4 4
}
public static void SJF(Process[]processArray) {
sortByArrivalTime(processArray);
int nowTime = processArray[0].arrivalTime;
//运行第一个到达的进程
processArray[0].finishTime = nowTime + processArray[0].serviceTime;
processArray[0].wholeTime = processArray[0].finishTime - processArray[0].arrivalTime;
processArray[0].weightWholeTime = 1.0 * processArray[0].wholeTime / processArray[0].serviceTime;
processArray[0].order = 0 + 1;
processArray[0].finish = true;
System.out.println("时刻" + nowTime + ":" + "进程" + processArray[0].name + "开始运行");
int nowJobIndex = 0;
nowTime = processArray[0].finishTime;
int lastJobIndex = 0;
//选出接下来到达的程序中最短serviTime的一个
for(int i = 0;i < n - 1;i++) {
int shortestJobIndex = chooseShortestJob(processArray,nowJobIndex);
if(nowTime < processArray[shortestJobIndex].arrivalTime)
nowTime = processArray[shortestJobIndex].arrivalTime;
nowJobIndex = shortestJobIndex;
processArray[shortestJobIndex].finishTime = nowTime + processArray[shortestJobIndex].serviceTime;
processArray[shortestJobIndex].wholeTime = processArray[shortestJobIndex].finishTime - processArray[shortestJobIndex].arrivalTime;
processArray[shortestJobIndex].weightWholeTime = 1.0 * processArray[shortestJobIndex].wholeTime / processArray[shortestJobIndex].serviceTime;
processArray[shortestJobIndex].order = processArray[lastJobIndex].order + 1;
processArray[shortestJobIndex].finish = true;
System.out.println("时刻" + nowTime + ":" + "进程" + processArray[shortestJobIndex].name + "开始运行");
lastJobIndex = shortestJobIndex;
nowTime = processArray[shortestJobIndex].finishTime;
}
display(processArray);
double avrageWT = calculateAverageWT(processArray);
System.out.println("平均周转时间为:" + avrageWT);
double avrageWWT = calculateAverageWWT(processArray);
System.out.println("平均带权周转时间为:" + avrageWWT);
}
private static int chooseShortestJob(Process[] processArray, int nowJobIndex) {
int shortestJobIndex = 0;
int shortestJobLength = 99999;
for(int i = 0 ;i < n ;i++){
//该任务没有完成,到达时间比现在任务的完成时间要早,并且是最短的服务时间。
if(processArray[i].finish == false && processArray[i].arrivalTime <= processArray[nowJobIndex].finishTime
&& processArray[i].serviceTime < shortestJobLength){
shortestJobIndex = i;
shortestJobLength = processArray[i].serviceTime;
}
}
//若是在前一个任务完成时,没有其他任务到达,则等待第一个到达的任务
if(shortestJobIndex == 0) {
shortestJobIndex = nowJobIndex + 1; //任务数组按照到达时间升序排列,下一个任务肯定没运行,到达时间一定在前任务后面
// shortestJobLength = processArray[shortestJobIndex].serviceTime;
}
return shortestJobIndex;
}
public static void FCFS(Process[]processArray){
// ArrayList list = new ArrayList();
// list.add(2);
// list.remove(list.size());
sortByArrivalTime(processArray);
int nowTime = processArray[0].arrivalTime;
System.out.println("时刻" + nowTime + ":" + "进程" + processArray[0].name + "开始运行");
for(int i =0 ;i < n; i++){
processArray[i].finishTime = nowTime + processArray[i].serviceTime;
processArray[i].wholeTime = processArray[i].finishTime - processArray[i].arrivalTime;
processArray[i].weightWholeTime = 1.0 * processArray[i].wholeTime / processArray[i].serviceTime;
processArray[i].order = i + 1;
if(i != n-1 && (processArray[i+1].arrivalTime < processArray[i].finishTime ))//下一个进程的到达时间小于上一个进程的完成时间
nowTime = processArray[i].finishTime;
else
nowTime = processArray[i+1].arrivalTime;
if(i != n-1)
System.out.println("时刻" + nowTime + ":" + "进程" + processArray[i+1].name + "开始运行");
}
display(processArray);
double avrageWT = calculateAverageWT(processArray);
System.out.println("平均周转时间为:" + avrageWT);
double avrageWWT = calculateAverageWWT(processArray);
System.out.println("平均带权周转时间为:" + avrageWWT);
}
public static double calculateAverageWT(Process[]p) { //计算平均周转时间
double sumWholeTime = 0;
for(int i = 0;i < n;i++){
sumWholeTime += p[i].wholeTime;
}
double averageWT = 1.0 * sumWholeTime / n;
return averageWT;
}
public static double calculateAverageWWT(Process[]p) { //计算平均带权周转时间
double sumWeightWholeTime = 0;
for(int i = 0;i < n;i++){
sumWeightWholeTime += p[i].weightWholeTime;
}
double averageWWT = 1.0 * sumWeightWholeTime / n;
return averageWWT;
}
public static void sortByArrivalTime(Process[]p){
for(int i = 0; i < n - 1 ;i++){
for(int j = 0 ;j < n - 1 - i;j++){
if(p[j].arrivalTime > p[j+1].arrivalTime)
exchange(p,p,j,j+1);
}
}
}
public static void exchange(Process[]p1,Process[]p2,int i,int j){
String tempName = p1[i].name;
int arrivalTime = p1[i].arrivalTime;
int serviceTime = p1[i].serviceTime;
p1[i].name = p2[j].name ;
p1[i].arrivalTime = p2[j].arrivalTime;
p1[i].serviceTime = p2[j].serviceTime;
p2[j].name = tempName;
p2[j].arrivalTime = arrivalTime;
p2[j].serviceTime = serviceTime;
}
public static void display(Process[]processArray) {
System.out.println("进程名 运行次序 到达时间 服务时间 完成时间 周转时间 带权周转时间");
for (int i = 0; i < n; i++) {
String message = processArray[i].name + " "
+ processArray[i].order + " "
+ processArray[i].arrivalTime + " "
+ processArray[i].serviceTime + " "
+ processArray[i].finishTime + " "
+ processArray[i].wholeTime + " "
+ processArray[i].weightWholeTime;
System.out.println(message);
}
}
}
网友评论