java实现三种进程调度算法的代码,可以模拟使用FCFS、SJF和RR三种不同的进程调度算法,支持多进程输入,并能够计算每个进程的开始时间、到达时间、服务时间、结束时间、周转时间和带权周转时间
时间: 2024-02-06 14:03:15 浏览: 180
以下是Java实现三种进程调度算法的代码,支持多进程输入,并能够计算每个进程的开始时间、到达时间、服务时间、结束时间、周转时间和带权周转时间。其中FCFS、SJF和RR三种算法分别用不同的函数来实现。
```java
import java.util.*;
class Process {
int pid; // 进程ID
int arrivalTime; // 到达时间
int burstTime; // 服务时间
int startTime; // 开始时间
int endTime; // 结束时间
int turnaroundTime; // 周转时间
double weightTurnaroundTime; // 带权周转时间
public Process(int pid, int arrivalTime, int burstTime) {
this.pid = pid;
this.arrivalTime = arrivalTime;
this.burstTime = burstTime;
this.startTime = 0;
this.endTime = 0;
this.turnaroundTime = 0;
this.weightTurnaroundTime = 0;
}
}
public class ProcessScheduler {
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.print("请输入第" + (i+1) + "个进程的到达时间和服务时间:");
int arrivalTime = input.nextInt();
int burstTime = input.nextInt();
processes[i] = new Process(i+1, arrivalTime, burstTime);
}
System.out.println();
// 按照到达时间排序
Arrays.sort(processes, new Comparator<Process>() {
public int compare(Process p1, Process p2) {
return p1.arrivalTime - p2.arrivalTime;
}
});
// 执行FCFS调度算法
System.out.println("FCFS调度算法:");
fcfs(processes);
System.out.println();
// 执行SJF调度算法
System.out.println("SJF调度算法:");
sjf(processes);
System.out.println();
// 执行RR调度算法
System.out.print("请输入时间片大小:");
int quantum = input.nextInt();
System.out.println("RR调度算法:");
rr(processes, quantum);
}
// FCFS调度算法
public static void fcfs(Process[] processes) {
int currentTime = 0;
for (int i = 0; i < processes.length; i++) {
if (currentTime < processes[i].arrivalTime) {
currentTime = processes[i].arrivalTime;
}
processes[i].startTime = currentTime;
processes[i].endTime = processes[i].startTime + processes[i].burstTime;
currentTime = processes[i].endTime;
processes[i].turnaroundTime = processes[i].endTime - processes[i].arrivalTime;
processes[i].weightTurnaroundTime = (double) processes[i].turnaroundTime / processes[i].burstTime;
printProcess(processes[i]);
}
}
// SJF调度算法
public static void sjf(Process[] processes) {
int currentTime = 0;
PriorityQueue<Process> queue = new PriorityQueue<Process>(processes.length, new Comparator<Process>() {
public int compare(Process p1, Process p2) {
return p1.burstTime - p2.burstTime;
}
});
int i = 0;
while (i < processes.length || !queue.isEmpty()) {
if (!queue.isEmpty()) {
Process process = queue.poll();
process.startTime = currentTime;
process.endTime = process.startTime + process.burstTime;
currentTime = process.endTime;
process.turnaroundTime = process.endTime - process.arrivalTime;
process.weightTurnaroundTime = (double) process.turnaroundTime / process.burstTime;
printProcess(process);
while (i < processes.length && processes[i].arrivalTime <= currentTime) {
queue.offer(processes[i]);
i++;
}
} else {
queue.offer(processes[i]);
currentTime = processes[i].arrivalTime;
i++;
}
}
}
// RR调度算法
public static void rr(Process[] processes, int quantum) {
int currentTime = 0;
int[] remainingTime = new int[processes.length];
for (int i = 0; i < processes.length; i++) {
remainingTime[i] = processes[i].burstTime;
}
Queue<Process> queue = new LinkedList<Process>();
int i = 0;
while (i < processes.length || !queue.isEmpty()) {
if (!queue.isEmpty()) {
Process process = queue.poll();
process.startTime = currentTime;
int executeTime = Math.min(quantum, remainingTime[process.pid-1]);
remainingTime[process.pid-1] -= executeTime;
currentTime += executeTime;
if (remainingTime[process.pid-1] == 0) {
process.endTime = currentTime;
process.turnaroundTime = process.endTime - process.arrivalTime;
process.weightTurnaroundTime = (double) process.turnaroundTime / process.burstTime;
printProcess(process);
} else {
queue.offer(process);
}
while (i < processes.length && processes[i].arrivalTime <= currentTime) {
queue.offer(processes[i]);
i++;
}
} else {
queue.offer(processes[i]);
currentTime = processes[i].arrivalTime;
i++;
}
}
}
// 输出进程信息
public static void printProcess(Process process) {
System.out.println("进程" + process.pid + ":");
System.out.println("到达时间:" + process.arrivalTime);
System.out.println("服务时间:" + process.burstTime);
System.out.println("开始时间:" + process.startTime);
System.out.println("结束时间:" + process.endTime);
System.out.println("周转时间:" + process.turnaroundTime);
System.out.println("带权周转时间:" + process.weightTurnaroundTime);
System.out.println();
}
}
```
这个代码实现了FCFS、SJF和RR三种进程调度算法,并且支持多进程输入,能够计算每个进程的开始时间、到达时间、服务时间、结束时间、周转时间和带权周转时间。你可以根据需要修改代码中的输入方式和输出格式。
阅读全文