将FCFS算法,SJF算法,RR算法,PSA算法用一个java程序写出来
时间: 2023-07-10 08:31:54 浏览: 97
以下是一个使用Java语言实现四种调度算法的示例代码:
1. FCFS算法
```
import java.util.*;
public class FCFS {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("请输入作业数:");
int n = scanner.nextInt();
int[] arriveTime = new int[n]; // 到达时间
int[] serviceTime = new int[n]; // 服务时间
for (int i = 0; i < n; i++) {
System.out.print("请输入第" + (i+1) + "个作业的到达时间和服务时间:");
arriveTime[i] = scanner.nextInt();
serviceTime[i] = scanner.nextInt();
}
int[] finishTime = new int[n]; // 完成时间
int[] waitingTime = new int[n]; // 等待时间
int[] turnAroundTime = new int[n]; // 周转时间
finishTime[0] = serviceTime[0];
turnAroundTime[0] = finishTime[0] - arriveTime[0];
waitingTime[0] = turnAroundTime[0] - serviceTime[0];
for (int i = 1; i < n; i++) {
finishTime[i] = finishTime[i-1] + serviceTime[i];
turnAroundTime[i] = finishTime[i] - arriveTime[i];
waitingTime[i] = turnAroundTime[i] - serviceTime[i];
}
double avgWaitingTime = 0.0;
double avgTurnAroundTime = 0.0;
for (int i = 0; i < n; i++) {
avgWaitingTime += waitingTime[i];
avgTurnAroundTime += turnAroundTime[i];
}
avgWaitingTime /= n;
avgTurnAroundTime /= n;
System.out.println("作业\t到达时间\t服务时间\t完成时间\t等待时间\t周转时间");
for (int i = 0; i < n; i++) {
System.out.println((i+1) + "\t" + arriveTime[i] + "\t\t" + serviceTime[i] + "\t\t" + finishTime[i] + "\t\t" + waitingTime[i] + "\t\t" + turnAroundTime[i]);
}
System.out.println("平均等待时间:" + avgWaitingTime);
System.out.println("平均周转时间:" + avgTurnAroundTime);
}
}
```
2. SJF算法
```
import java.util.*;
public class SJF {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("请输入作业数:");
int n = scanner.nextInt();
int[] arriveTime = new int[n]; // 到达时间
int[] serviceTime = new int[n]; // 服务时间
for (int i = 0; i < n; i++) {
System.out.print("请输入第" + (i+1) + "个作业的到达时间和服务时间:");
arriveTime[i] = scanner.nextInt();
serviceTime[i] = scanner.nextInt();
}
int[] finishTime = new int[n]; // 完成时间
int[] waitingTime = new int[n]; // 等待时间
int[] turnAroundTime = new int[n]; // 周转时间
boolean[] visited = new boolean[n]; // 标记作业是否已经完成
int currentTime = 0;
int count = 0;
while (count < n) {
int minServiceTime = Integer.MAX_VALUE;
int minIndex = -1;
for (int i = 0; i < n; i++) {
if (!visited[i] && arriveTime[i] <= currentTime && serviceTime[i] < minServiceTime) {
minServiceTime = serviceTime[i];
minIndex = i;
}
}
if (minIndex != -1) {
finishTime[minIndex] = currentTime + serviceTime[minIndex];
turnAroundTime[minIndex] = finishTime[minIndex] - arriveTime[minIndex];
waitingTime[minIndex] = turnAroundTime[minIndex] - serviceTime[minIndex];
visited[minIndex] = true;
count++;
currentTime = finishTime[minIndex];
} else {
currentTime++;
}
}
double avgWaitingTime = 0.0;
double avgTurnAroundTime = 0.0;
for (int i = 0; i < n; i++) {
avgWaitingTime += waitingTime[i];
avgTurnAroundTime += turnAroundTime[i];
}
avgWaitingTime /= n;
avgTurnAroundTime /= n;
System.out.println("作业\t到达时间\t服务时间\t完成时间\t等待时间\t周转时间");
for (int i = 0; i < n; i++) {
System.out.println((i+1) + "\t" + arriveTime[i] + "\t\t" + serviceTime[i] + "\t\t" + finishTime[i] + "\t\t" + waitingTime[i] + "\t\t" + turnAroundTime[i]);
}
System.out.println("平均等待时间:" + avgWaitingTime);
System.out.println("平均周转时间:" + avgTurnAroundTime);
}
}
```
3. RR算法
```
import java.util.*;
public class RR {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("请输入作业数:");
int n = scanner.nextInt();
int[] arriveTime = new int[n]; // 到达时间
int[] serviceTime = new int[n]; // 服务时间
for (int i = 0; i < n; i++) {
System.out.print("请输入第" + (i+1) + "个作业的到达时间和服务时间:");
arriveTime[i] = scanner.nextInt();
serviceTime[i] = scanner.nextInt();
}
int quantum = 2; // 时间片大小
int[] finishTime = new int[n]; // 完成时间
int[] waitingTime = new int[n]; // 等待时间
int[] turnAroundTime = new int[n]; // 周转时间
int[] remainTime = new int[n]; // 剩余服务时间
for (int i = 0; i < n; i++) {
remainTime[i] = serviceTime[i];
}
int currentTime = 0;
int count = 0;
while (count < n) {
boolean flag = false;
for (int i = 0; i < n; i++) {
if (arriveTime[i] <= currentTime && remainTime[i] > 0) {
if (remainTime[i] > quantum) {
currentTime += quantum;
remainTime[i] -= quantum;
} else {
currentTime += remainTime[i];
finishTime[i] = currentTime;
turnAroundTime[i] = finishTime[i] - arriveTime[i];
waitingTime[i] = turnAroundTime[i] - serviceTime[i];
remainTime[i] = 0;
count++;
}
flag = true;
}
}
if (!flag) {
currentTime++;
}
}
double avgWaitingTime = 0.0;
double avgTurnAroundTime = 0.0;
for (int i = 0; i < n; i++) {
avgWaitingTime += waitingTime[i];
avgTurnAroundTime += turnAroundTime[i];
}
avgWaitingTime /= n;
avgTurnAroundTime /= n;
System.out.println("作业\t到达时间\t服务时间\t完成时间\t等待时间\t周转时间");
for (int i = 0; i < n; i++) {
System.out.println((i+1) + "\t" + arriveTime[i] + "\t\t" + serviceTime[i] + "\t\t" + finishTime[i] + "\t\t" + waitingTime[i] + "\t\t" + turnAroundTime[i]);
}
System.out.println("平均等待时间:" + avgWaitingTime);
System.out.println("平均周转时间:" + avgTurnAroundTime);
}
}
```
4. PSA算法
```
import java.util.*;
public class PSA {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("请输入作业数:");
int n = scanner.nextInt();
int[] arriveTime = new int[n]; // 到达时间
int[] serviceTime = new int[n]; // 服务时间
int[] priority = new int[n]; // 优先级
for (int i = 0; i < n; i++) {
System.out.print("请输入第" + (i+1) + "个作业的到达时间、服务时间和优先级:");
arriveTime[i] = scanner.nextInt();
serviceTime[i] = scanner.nextInt();
priority[i] = scanner.nextInt();
}
int[] finishTime = new int[n]; // 完成时间
int[] waitingTime = new int[n]; // 等待时间
int[] turnAroundTime = new int[n]; // 周转时间
boolean[] visited = new boolean[n]; // 标记作业是否已经完成
int currentTime = 0;
int count = 0;
while (count < n) {
int maxPriority = Integer.MIN_VALUE;
int maxIndex = -1;
for (int i = 0; i < n; i++) {
if (!visited[i] && arriveTime[i] <= currentTime && priority[i] > maxPriority) {
maxPriority = priority[i];
maxIndex = i;
}
}
if (maxIndex != -1) {
finishTime[maxIndex] = currentTime + serviceTime[maxIndex];
turnAroundTime[maxIndex] = finishTime[maxIndex] - arriveTime[maxIndex];
waitingTime[maxIndex] = turnAroundTime[maxIndex] - serviceTime[maxIndex];
visited[maxIndex] = true;
count++;
currentTime = finishTime[maxIndex];
} else {
currentTime++;
}
}
double avgWaitingTime = 0.0;
double avgTurnAroundTime = 0.0;
for (int i = 0; i < n; i++) {
avgWaitingTime += waitingTime[i];
avgTurnAroundTime += turnAroundTime[i];
}
avgWaitingTime /= n;
avgTurnAroundTime /= n;
System.out.println("作业\t到达时间\t服务时间\t优先级\t完成时间\t等待时间\t周转时间");
for (int i = 0; i < n; i++) {
System.out.println((i+1) + "\t" + arriveTime[i] + "\t\t" + serviceTime[i] + "\t\t" + priority[i] + "\t\t" + finishTime[i] + "\t\t" + waitingTime[i] + "\t\t" + turnAroundTime[i]);
}
System.out.println("平均等待时间:" + avgWaitingTime);
System.out.println("平均周转时间:" + avgTurnAroundTime);
}
}
```
阅读全文