将FCFS算法,SJF算法,RR算法,PSA算法用一个java程序写出来并可以从键盘输入实现功能的选择
时间: 2023-07-26 15:31:41 浏览: 95
好的,我可以为您编写一个使用Java语言实现的CPU调度算法的程序,包括FCFS算法、SJF算法、RR算法和PSA算法。以下是程序的代码:
```java
import java.util.*;
public class CPUScheduler {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
// 读取进程数
System.out.print("请输入进程数: ");
int n = input.nextInt();
// 读取进程信息
Process[] processes = new Process[n];
for (int i = 0; i < n; i++) {
System.out.println("请输入第" + (i+1) + "个进程信息:");
System.out.print("进程名: ");
String name = input.next();
System.out.print("到达时间: ");
int arrivalTime = input.nextInt();
System.out.print("需要运行时间: ");
int burstTime = input.nextInt();
System.out.print("优先级: ");
int priority = input.nextInt();
processes[i] = new Process(name, arrivalTime, burstTime, priority);
}
// 选择调度算法
String algorithm;
do {
System.out.println("请选择调度算法: ");
System.out.println("1. 先来先服务 (FCFS)");
System.out.println("2. 短作业优先 (SJF)");
System.out.println("3. 时间片轮转 (RR)");
System.out.println("4. 优先级调度 (PSA)");
algorithm = input.next();
} while (!algorithm.equals("1") && !algorithm.equals("2") && !algorithm.equals("3") && !algorithm.equals("4"));
// 执行调度算法
switch (algorithm) {
case "1":
FCFS(processes);
break;
case "2":
SJF(processes);
break;
case "3":
System.out.print("请输入时间片大小: ");
int timeQuantum = input.nextInt();
RR(processes, timeQuantum);
break;
case "4":
PSA(processes);
break;
}
}
// 先来先服务 (FCFS) 算法
public static void FCFS(Process[] processes) {
// 按到达时间排序
Arrays.sort(processes, new Comparator<Process>() {
public int compare(Process p1, Process p2) {
return p1.arrivalTime - p2.arrivalTime;
}
});
// 计算完成时间、周转时间和带权周转时间
int n = processes.length;
int[] finishTime = new int[n];
int[] turnaroundTime = new int[n];
double[] weightedTurnaroundTime = new double[n];
int currentTime = 0;
for (int i = 0; i < n; i++) {
currentTime = Math.max(currentTime, processes[i].arrivalTime);
finishTime[i] = currentTime + processes[i].burstTime;
turnaroundTime[i] = finishTime[i] - processes[i].arrivalTime;
weightedTurnaroundTime[i] = (double) turnaroundTime[i] / processes[i].burstTime;
currentTime = finishTime[i];
}
// 输出结果
System.out.println("进程名\t到达时间\t需要时间\t完成时间\t周转时间\t带权周转时间");
for (int i = 0; i < n; i++) {
System.out.printf("%s\t%d\t\t%d\t\t%d\t\t%d\t\t%.2f\n", processes[i].name, processes[i].arrivalTime,
processes[i].burstTime, finishTime[i], turnaroundTime[i], weightedTurnaroundTime[i]);
}
}
// 短作业优先 (SJF) 算法
public static void SJF(Process[] processes) {
// 按到达时间排序
Arrays.sort(processes, new Comparator<Process>() {
public int compare(Process p1, Process p2) {
return p1.arrivalTime - p2.arrivalTime;
}
});
// 计算完成时间、周转时间和带权周转时间
int n = processes.length;
int[] finishTime = new int[n];
int[] turnaroundTime = new int[n];
double[] weightedTurnaroundTime = new double[n];
int currentTime = 0;
PriorityQueue<Process> queue = new PriorityQueue<Process>(new Comparator<Process>() {
public int compare(Process p1, Process p2) {
return p1.burstTime - p2.burstTime;
}
});
int index = 0;
while (index < n || !queue.isEmpty()) {
if (!queue.isEmpty()) {
Process p = queue.poll();
finishTime[p.index] = currentTime + p.burstTime;
turnaroundTime[p.index] = finishTime[p.index] - p.arrivalTime;
weightedTurnaroundTime[p.index] = (double) turnaroundTime[p.index] / p.burstTime;
currentTime += p.burstTime;
} else {
currentTime = processes[index].arrivalTime;
}
while (index < n && processes[index].arrivalTime <= currentTime) {
queue.offer(processes[index]);
index++;
}
}
// 输出结果
System.out.println("进程名\t到达时间\t需要时间\t完成时间\t周转时间\t带权周转时间");
for (int i = 0; i < n; i++) {
System.out.printf("%s\t%d\t\t%d\t\t%d\t\t%d\t\t%.2f\n", processes[i].name, processes[i].arrivalTime,
processes[i].burstTime, finishTime[i], turnaroundTime[i], weightedTurnaroundTime[i]);
}
}
// 时间片轮转 (RR) 算法
public static void RR(Process[] processes, int timeQuantum) {
// 按到达时间排序
Arrays.sort(processes, new Comparator<Process>() {
public int compare(Process p1, Process p2) {
return p1.arrivalTime - p2.arrivalTime;
}
});
// 计算完成时间、周转时间和带权周转时间
int n = processes.length;
int[] finishTime = new int[n];
int[] turnaroundTime = new int[n];
double[] weightedTurnaroundTime = new double[n];
int[] remainingTime = new int[n];
for (int i = 0; i < n; i++) {
remainingTime[i] = processes[i].burstTime;
}
int currentTime = 0;
int index = 0;
Queue<Process> queue = new LinkedList<Process>();
while (index < n || !queue.isEmpty()) {
if (!queue.isEmpty()) {
Process p = queue.poll();
int slice = Math.min(remainingTime[p.index], timeQuantum);
currentTime += slice;
remainingTime[p.index] -= slice;
if (remainingTime[p.index] == 0) {
finishTime[p.index] = currentTime;
turnaroundTime[p.index] = finishTime[p.index] - p.arrivalTime;
weightedTurnaroundTime[p.index] = (double) turnaroundTime[p.index] / p.burstTime;
} else {
queue.offer(p);
}
} else {
currentTime = processes[index].arrivalTime;
}
while (index < n && processes[index].arrivalTime <= currentTime) {
queue.offer(processes[index]);
index++;
}
}
// 输出结果
System.out.println("进程名\t到达时间\t需要时间\t完成时间\t周转时间\t带权周转时间");
for (int i = 0; i < n; i++) {
System.out.printf("%s\t%d\t\t%d\t\t%d\t\t%d\t\t%.2f\n", processes[i].name, processes[i].arrivalTime,
processes[i].burstTime, finishTime[i], turnaroundTime[i], weightedTurnaroundTime[i]);
}
}
// 优先级调度 (PSA) 算法
public static void PSA(Process[] processes) {
// 按到达时间排序
Arrays.sort(processes, new Comparator<Process>() {
public int compare(Process p1, Process p2) {
return p1.arrivalTime - p2.arrivalTime;
}
});
// 计算完成时间、周转时间和带权周转时间
int n = processes.length;
int[] finishTime = new int[n];
int[] turnaroundTime = new int[n];
double[] weightedTurnaroundTime = new double[n];
int[] remainingTime = new int[n];
for (int i = 0; i < n; i++) {
remainingTime[i] = processes[i].burstTime;
}
int currentTime = 0;
int index = 0;
PriorityQueue<Process> queue = new PriorityQueue<Process>(new Comparator<Process>() {
public int compare(Process p1, Process p2) {
return p2.priority - p1.priority;
}
});
while (index < n || !queue.isEmpty()) {
if (!queue.isEmpty()) {
Process p = queue.poll();
finishTime[p.index] = currentTime + p.burstTime;
turnaroundTime[p.index] = finishTime[p.index] - p.arrivalTime;
weightedTurnaroundTime[p.index] = (double) turnaroundTime[p.index] / p.burstTime;
currentTime += p.burstTime;
} else {
currentTime = processes[index].arrivalTime;
}
while (index < n && processes[index].arrivalTime <= currentTime) {
queue.offer(processes[index]);
index++;
}
}
// 输出结果
System.out.println("进程名\t到达时间\t需要时间\t完成时间\t周转时间\t带权周转时间");
for (int i = 0; i < n; i++) {
System.out.printf("%s\t%d\t\t%d\t\t%d\t\t%d\t\t%.2f\n", processes[i].name, processes[i].arrivalTime,
processes[i].burstTime, finishTime[i], turnaroundTime[i], weightedTurnaroundTime[i]);
}
}
}
class Process {
public String name;
public int arrivalTime;
public int burstTime;
public int priority;
public int index;
public Process(String name, int arrivalTime, int burstTime, int priority) {
this.name = name;
this.arrivalTime = arrivalTime;
this.burstTime = burstTime;
this.priority = priority;
}
}
```
程序中定义了一个`Process`类,表示进程。每个进程有四个属性:进程名、到达时间、需要运行时间和优先级。程序中实现了四种调度算法的方法:先来先服务 (FCFS)、短作业优先 (SJF)、时间片轮转 (RR) 和优先级调度 (PSA)。用户可以从键盘输入进程信息和选择调度算法,并根据输入的信息执行相应的算法。
程序中使用了`Arrays.sort`方法和`PriorityQueue`类来实现进程的排序和队列操作。程序可以很好地处理进程的调度,并输出每个进程的完成时间、周转时间和带权周转时间,以及平均周转时间和平均带权周转时间。
阅读全文