模拟单处理机下N个进程的先来先服务调度算法、短进程优先调度算法或高响应比优先进程调度算法用java实现
时间: 2023-10-22 09:18:24 浏览: 95
以下是使用Java实现三种调度算法的简单示例:
先来先服务调度算法:
```java
import java.util.ArrayList;
public class FCFS {
public static void main(String[] args) {
ArrayList<Process> processes = new ArrayList<>();
processes.add(new Process(1, 0, 5));
processes.add(new Process(2, 1, 3));
processes.add(new Process(3, 2, 4));
processes.add(new Process(4, 3, 2));
int currentTime = 0;
for (Process p : processes) {
p.setStartTime(currentTime);
p.setFinishTime(currentTime + p.getBurstTime());
p.setTurnaroundTime(p.getFinishTime() - p.getArrivalTime());
p.setWaitingTime(p.getTurnaroundTime() - p.getBurstTime());
currentTime = p.getFinishTime();
}
System.out.println("Process\tAT\tBT\tST\tFT\tTAT\tWT");
for (Process p : processes) {
System.out.println(p.getProcessId() + "\t\t" + p.getArrivalTime() + "\t\t" + p.getBurstTime() +
"\t\t" + p.getStartTime() + "\t\t" + p.getFinishTime() + "\t\t" + p.getTurnaroundTime() +
"\t\t" + p.getWaitingTime());
}
}
}
class Process {
private int processId;
private int arrivalTime;
private int burstTime;
private int startTime;
private int finishTime;
private int turnaroundTime;
private int waitingTime;
public Process(int processId, int arrivalTime, int burstTime) {
this.processId = processId;
this.arrivalTime = arrivalTime;
this.burstTime = burstTime;
}
public int getProcessId() {
return processId;
}
public int getArrivalTime() {
return arrivalTime;
}
public int getBurstTime() {
return burstTime;
}
public int getStartTime() {
return startTime;
}
public void setStartTime(int startTime) {
this.startTime = startTime;
}
public int getFinishTime() {
return finishTime;
}
public void setFinishTime(int finishTime) {
this.finishTime = finishTime;
}
public int getTurnaroundTime() {
return turnaroundTime;
}
public void setTurnaroundTime(int turnaroundTime) {
this.turnaroundTime = turnaroundTime;
}
public int getWaitingTime() {
return waitingTime;
}
public void setWaitingTime(int waitingTime) {
this.waitingTime = waitingTime;
}
}
```
短进程优先调度算法:
```java
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
public class SJF {
public static void main(String[] args) {
ArrayList<Process> processes = new ArrayList<>();
processes.add(new Process(1, 0, 5));
processes.add(new Process(2, 1, 3));
processes.add(new Process(3, 2, 4));
processes.add(new Process(4, 3, 2));
int currentTime = 0;
while (!processes.isEmpty()) {
// 找到剩余时间最短的进程
Process shortest = Collections.min(processes, Comparator.comparing(p -> p.getBurstTime()));
processes.remove(shortest);
shortest.setStartTime(currentTime);
shortest.setFinishTime(currentTime + shortest.getBurstTime());
shortest.setTurnaroundTime(shortest.getFinishTime() - shortest.getArrivalTime());
shortest.setWaitingTime(shortest.getTurnaroundTime() - shortest.getBurstTime());
currentTime = shortest.getFinishTime();
}
System.out.println("Process\tAT\tBT\tST\tFT\tTAT\tWT");
for (Process p : processes) {
System.out.println(p.getProcessId() + "\t\t" + p.getArrivalTime() + "\t\t" + p.getBurstTime() +
"\t\t" + p.getStartTime() + "\t\t" + p.getFinishTime() + "\t\t" + p.getTurnaroundTime() +
"\t\t" + p.getWaitingTime());
}
}
}
```
高响应比优先进程调度算法:
```java
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
public class HRRN {
public static void main(String[] args) {
ArrayList<Process> processes = new ArrayList<>();
processes.add(new Process(1, 0, 5));
processes.add(new Process(2, 1, 3));
processes.add(new Process(3, 2, 4));
processes.add(new Process(4, 3, 2));
int currentTime = 0;
while (!processes.isEmpty()) {
// 找到响应比最高的进程
Process highest = Collections.max(processes, Comparator.comparing(p -> (p.getBurstTime() + currentTime - p.getArrivalTime()) / p.getBurstTime()));
processes.remove(highest);
highest.setStartTime(currentTime);
highest.setFinishTime(currentTime + highest.getBurstTime());
highest.setTurnaroundTime(highest.getFinishTime() - highest.getArrivalTime());
highest.setWaitingTime(highest.getTurnaroundTime() - highest.getBurstTime());
currentTime = highest.getFinishTime();
}
System.out.println("Process\tAT\tBT\tST\tFT\tTAT\tWT");
for (Process p : processes) {
System.out.println(p.getProcessId() + "\t\t" + p.getArrivalTime() + "\t\t" + p.getBurstTime() +
"\t\t" + p.getStartTime() + "\t\t" + p.getFinishTime() + "\t\t" + p.getTurnaroundTime() +
"\t\t" + p.getWaitingTime());
}
}
}
```
以上示例仅为演示,实际情况下还需要考虑诸如进程的优先级、时间片轮转等因素。
阅读全文