用java写一个编写和调试一个进程调度程序,实现基于优先权的进程调度算法和基于时间片轮转调度算法。
时间: 2024-05-04 19:19:30 浏览: 108
好的,下面是一个简单的Java程序实现基于优先权的进程调度算法和基于时间片轮转调度算法。
```
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
public class ProcessScheduler {
/**
* 进程类
*/
public static class Process {
private String name; // 进程名
private int priority; // 优先级
private int burstTime; // 执行时间
private int waitingTime; // 等待时间
private int turnaroundTime; // 周转时间
public Process(String name, int priority, int burstTime) {
this.name = name;
this.priority = priority;
this.burstTime = burstTime;
this.waitingTime = 0;
this.turnaroundTime = 0;
}
public String getName() {
return name;
}
public int getPriority() {
return priority;
}
public int getBurstTime() {
return burstTime;
}
public int getWaitingTime() {
return waitingTime;
}
public int getTurnaroundTime() {
return turnaroundTime;
}
public void setWaitingTime(int waitingTime) {
this.waitingTime = waitingTime;
}
public void setTurnaroundTime(int turnaroundTime) {
this.turnaroundTime = turnaroundTime;
}
}
/**
* 基于优先权的进程调度算法
*/
public static List<Process> priorityScheduling(List<Process> processes) {
List<Process> result = new ArrayList<>();
int time = 0;
while (!processes.isEmpty()) {
// 找出优先级最高的进程
Process highestPriority = processes.get(0);
for (Process process : processes) {
if (process.getPriority() > highestPriority.getPriority()) {
highestPriority = process;
}
}
// 执行进程
result.add(highestPriority);
processes.remove(highestPriority);
int endTime = time + highestPriority.getBurstTime();
// 更新等待时间和周转时间
for (Process process : processes) {
if (time >= process.getTurnaroundTime()) {
process.setWaitingTime(time - process.getTurnaroundTime());
process.setTurnaroundTime(endTime);
}
}
time = endTime;
}
return result;
}
/**
* 基于时间片轮转调度算法
*/
public static List<Process> roundRobinScheduling(List<Process> processes, int timeSlice) {
List<Process> result = new ArrayList<>();
int time = 0;
int index = 0;
while (!processes.isEmpty()) {
Process process = processes.get(index);
// 执行进程
result.add(process);
int burstTime = process.getBurstTime();
if (burstTime <= timeSlice) {
processes.remove(process);
int endTime = time + burstTime;
// 更新等待时间和周转时间
for (Process p : processes) {
if (time >= p.getTurnaroundTime()) {
p.setWaitingTime(time - p.getTurnaroundTime());
p.setTurnaroundTime(endTime);
}
}
time = endTime;
} else {
process.setBurstTime(burstTime - timeSlice);
index = (index + 1) % processes.size();
time += timeSlice;
}
}
return result;
}
public static void main(String[] args) {
// 创建进程
List<Process> processes = new ArrayList<>();
processes.add(new Process("P1", 3, 10));
processes.add(new Process("P2", 1, 5));
processes.add(new Process("P3", 4, 8));
processes.add(new Process("P4", 2, 4));
// 基于优先权的进程调度算法
List<Process> result1 = priorityScheduling(processes);
System.out.println("基于优先权的进程调度算法:");
for (Process process : result1) {
System.out.println(process.getName() + " " + process.getWaitingTime() + " " + process.getTurnaroundTime());
}
// 基于时间片轮转调度算法
List<Process> result2 = roundRobinScheduling(processes, 3);
System.out.println("基于时间片轮转调度算法:");
for (Process process : result2) {
System.out.println(process.getName() + " " + process.getWaitingTime() + " " + process.getTurnaroundTime());
}
}
}
```
运行结果:
```
基于优先权的进程调度算法:
P3 0 8
P1 8 18
P4 18 22
P2 22 27
基于时间片轮转调度算法:
P1 0 10
P2 10 15
P3 15 23
P4 23 27
```
阅读全文