写一个Java程式实现FCFS算法、SJF算法|
时间: 2023-11-10 18:21:05 浏览: 130
以下是Java实现FCFS算法和SJF算法的代码:
FCFS算法:
```java
import java.util.Scanner;
public class FCFS {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("输入进程数:");
int n = scanner.nextInt();
int[] arrivalTime = new int[n]; // 到达时间
int[] burstTime = new int[n]; // 服务时间
int[] finishTime = new int[n]; // 完成时间
int[] waitingTime = new int[n]; // 等待时间
int[] turnaroundTime = new int[n]; // 周转时间
System.out.println("输入每个进程的到达时间和服务时间:");
for (int i = 0; i < n; i++) {
System.out.print("进程 " + (i + 1) + " 到达时间:");
arrivalTime[i] = scanner.nextInt();
System.out.print("进程 " + (i + 1) + " 服务时间:");
burstTime[i] = scanner.nextInt();
}
// 计算完成时间、等待时间和周转时间
finishTime[0] = arrivalTime[0] + burstTime[0];
turnaroundTime[0] = finishTime[0] - arrivalTime[0];
waitingTime[0] = 0;
for (int i = 1; i < n; i++) {
finishTime[i] = finishTime[i - 1] + burstTime[i];
turnaroundTime[i] = finishTime[i] - arrivalTime[i];
waitingTime[i] = turnaroundTime[i] - burstTime[i];
}
// 计算平均等待时间和平均周转时间
int totalWaitingTime = 0;
int totalTurnaroundTime = 0;
for (int i = 0; i < n; i++) {
totalWaitingTime += waitingTime[i];
totalTurnaroundTime += turnaroundTime[i];
}
double averageWaitingTime = (double) totalWaitingTime / n;
double averageTurnaroundTime = (double) totalTurnaroundTime / n;
// 输出结果
System.out.println("进程\t到达时间\t服务时间\t完成时间\t等待时间\t周转时间");
for (int i = 0; i < n; i++) {
System.out.println((i + 1) + "\t" + arrivalTime[i] + "\t\t" + burstTime[i] + "\t\t" + finishTime[i] + "\t\t" + waitingTime[i] + "\t\t" + turnaroundTime[i]);
}
System.out.println("平均等待时间:" + averageWaitingTime);
System.out.println("平均周转时间:" + averageTurnaroundTime);
}
}
```
SJF算法:
```java
import java.util.Scanner;
public class SJF {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("输入进程数:");
int n = scanner.nextInt();
int[] arrivalTime = new int[n]; // 到达时间
int[] burstTime = new int[n]; // 服务时间
int[] finishTime = new int[n]; // 完成时间
int[] waitingTime = new int[n]; // 等待时间
int[] turnaroundTime = new int[n]; // 周转时间
boolean[] isFinished = new boolean[n]; // 进程是否完成
System.out.println("输入每个进程的到达时间和服务时间:");
for (int i = 0; i < n; i++) {
System.out.print("进程 " + (i + 1) + " 到达时间:");
arrivalTime[i] = scanner.nextInt();
System.out.print("进程 " + (i + 1) + " 服务时间:");
burstTime[i] = scanner.nextInt();
}
// 计算完成时间、等待时间和周转时间
int currentTime = 0;
int finishedCount = 0;
while (finishedCount < n) {
int shortestIndex = -1;
int shortestBurstTime = Integer.MAX_VALUE;
for (int i = 0; i < n; i++) {
if (!isFinished[i] && arrivalTime[i] <= currentTime && burstTime[i] < shortestBurstTime) {
shortestIndex = i;
shortestBurstTime = burstTime[i];
}
}
if (shortestIndex != -1) {
finishTime[shortestIndex] = currentTime + burstTime[shortestIndex];
turnaroundTime[shortestIndex] = finishTime[shortestIndex] - arrivalTime[shortestIndex];
waitingTime[shortestIndex] = turnaroundTime[shortestIndex] - burstTime[shortestIndex];
isFinished[shortestIndex] = true;
currentTime = finishTime[shortestIndex];
finishedCount++;
} else {
currentTime++;
}
}
// 计算平均等待时间和平均周转时间
int totalWaitingTime = 0;
int totalTurnaroundTime = 0;
for (int i = 0; i < n; i++) {
totalWaitingTime += waitingTime[i];
totalTurnaroundTime += turnaroundTime[i];
}
double averageWaitingTime = (double) totalWaitingTime / n;
double averageTurnaroundTime = (double) totalTurnaroundTime / n;
// 输出结果
System.out.println("进程\t到达时间\t服务时间\t完成时间\t等待时间\t周转时间");
for (int i = 0; i < n; i++) {
System.out.println((i + 1) + "\t" + arrivalTime[i] + "\t\t" + burstTime[i] + "\t\t" + finishTime[i] + "\t\t" + waitingTime[i] + "\t\t" + turnaroundTime[i]);
}
System.out.println("平均等待时间:" + averageWaitingTime);
System.out.println("平均周转时间:" + averageTurnaroundTime);
}
}
```
阅读全文