请用Java编写,将先来先服务、短作业优先、时间片轮转、优先级算法合并到一个程序中,并有一个菜单可以选择需要的算法,以及还有算法的输出界面,包括进程名、到达时间、运行时间、开始时间、结束时间、周转时间、带权周转时间、平均带权周转时间、平均周转时间。
时间: 2023-10-20 20:00:38 浏览: 84
操作系统实验调度算法
好的,以下是Java实现的代码:
```
import java.util.*;
class Process {
String name;
int arrivalTime;
int burstTime;
int priority;
int startTime;
int finishTime;
int turnaroundTime;
double weightedTurnaroundTime;
boolean completed;
public Process(String name, int arrivalTime, int burstTime, int priority) {
this.name = name;
this.arrivalTime = arrivalTime;
this.burstTime = burstTime;
this.priority = priority;
this.completed = false;
}
}
public class Scheduler {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter the number of processes: ");
int numProcesses = scanner.nextInt();
List<Process> processes = new ArrayList<>();
for (int i = 1; i <= numProcesses; i++) {
System.out.println("Enter details for process " + i + ":");
System.out.print("Name: ");
String name = scanner.next();
System.out.print("Arrival time: ");
int arrivalTime = scanner.nextInt();
System.out.print("Burst time: ");
int burstTime = scanner.nextInt();
System.out.print("Priority: ");
int priority = scanner.nextInt();
processes.add(new Process(name, arrivalTime, burstTime, priority));
}
while (true) {
System.out.println("\nChoose a scheduling algorithm:");
System.out.println("1. First Come First Serve (FCFS)");
System.out.println("2. Shortest Job First (SJF)");
System.out.println("3. Round Robin (RR)");
System.out.println("4. Priority");
System.out.println("5. Exit");
int choice = scanner.nextInt();
switch (choice) {
case 1:
fcfs(processes);
break;
case 2:
sjf(processes);
break;
case 3:
System.out.print("Enter time quantum: ");
int timeQuantum = scanner.nextInt();
rr(processes, timeQuantum);
break;
case 4:
priority(processes);
break;
case 5:
System.exit(0);
break;
default:
System.out.println("Invalid choice.");
}
}
}
public static void fcfs(List<Process> processes) {
System.out.println("\nFCFS scheduling:");
int currentTime = 0;
for (Process p : processes) {
if (p.arrivalTime > currentTime) {
currentTime = p.arrivalTime;
}
p.startTime = currentTime;
p.finishTime = currentTime + p.burstTime;
p.turnaroundTime = p.finishTime - p.arrivalTime;
p.weightedTurnaroundTime = p.turnaroundTime / (double) p.burstTime;
currentTime = p.finishTime;
System.out.println("Process " + p.name + ":");
System.out.println("Arrival time: " + p.arrivalTime);
System.out.println("Burst time: " + p.burstTime);
System.out.println("Start time: " + p.startTime);
System.out.println("Finish time: " + p.finishTime);
System.out.println("Turnaround time: " + p.turnaroundTime);
System.out.println("Weighted turnaround time: " + p.weightedTurnaroundTime);
}
double totalTurnaroundTime = 0;
double totalWeightedTurnaroundTime = 0;
for (Process p : processes) {
totalTurnaroundTime += p.turnaroundTime;
totalWeightedTurnaroundTime += p.weightedTurnaroundTime;
}
double avgTurnaroundTime = totalTurnaroundTime / processes.size();
double avgWeightedTurnaroundTime = totalWeightedTurnaroundTime / processes.size();
System.out.println("\nAverage turnaround time: " + avgTurnaroundTime);
System.out.println("Average weighted turnaround time: " + avgWeightedTurnaroundTime);
}
public static void sjf(List<Process> processes) {
System.out.println("\nSJF scheduling:");
int currentTime = 0;
List<Process> queue = new ArrayList<>();
while (!processes.isEmpty() || !queue.isEmpty()) {
for (Process p : processes) {
if (p.arrivalTime <= currentTime) {
queue.add(p);
}
}
if (queue.isEmpty()) {
currentTime++;
continue;
}
Process shortestJob = queue.get(0);
for (int i = 1; i < queue.size(); i++) {
Process p = queue.get(i);
if (p.burstTime < shortestJob.burstTime) {
shortestJob = p;
}
}
queue.remove(shortestJob);
shortestJob.startTime = currentTime;
shortestJob.finishTime = currentTime + shortestJob.burstTime;
shortestJob.turnaroundTime = shortestJob.finishTime - shortestJob.arrivalTime;
shortestJob.weightedTurnaroundTime = shortestJob.turnaroundTime / (double) shortestJob.burstTime;
shortestJob.completed = true;
currentTime = shortestJob.finishTime;
System.out.println("Process " + shortestJob.name + ":");
System.out.println("Arrival time: " + shortestJob.arrivalTime);
System.out.println("Burst time: " + shortestJob.burstTime);
System.out.println("Start time: " + shortestJob.startTime);
System.out.println("Finish time: " + shortestJob.finishTime);
System.out.println("Turnaround time: " + shortestJob.turnaroundTime);
System.out.println("Weighted turnaround time: " + shortestJob.weightedTurnaroundTime);
}
double totalTurnaroundTime = 0;
double totalWeightedTurnaroundTime = 0;
for (Process p : processes) {
if (p.completed) {
totalTurnaroundTime += p.turnaroundTime;
totalWeightedTurnaroundTime += p.weightedTurnaroundTime;
}
}
double avgTurnaroundTime = totalTurnaroundTime / processes.size();
double avgWeightedTurnaroundTime = totalWeightedTurnaroundTime / processes.size();
System.out.println("\nAverage turnaround time: " + avgTurnaroundTime);
System.out.println("Average weighted turnaround time: " + avgWeightedTurnaroundTime);
}
public static void rr(List<Process> processes, int timeQuantum) {
System.out.println("\nRound Robin scheduling with time quantum " + timeQuantum + ":");
int currentTime = 0;
List<Process> queue = new ArrayList<>();
while (!processes.isEmpty() || !queue.isEmpty()) {
for (Process p : processes) {
if (p.arrivalTime <= currentTime) {
queue.add(p);
}
}
if (queue.isEmpty()) {
currentTime++;
continue;
}
Process p = queue.get(0);
if (p.burstTime <= timeQuantum) {
queue.remove(p);
p.startTime = currentTime;
p.finishTime = currentTime + p.burstTime;
p.turnaroundTime = p.finishTime - p.arrivalTime;
p.weightedTurnaroundTime = p.turnaroundTime / (double) p.burstTime;
p.completed = true;
currentTime = p.finishTime;
System.out.println("Process " + p.name + ":");
System.out.println("Arrival time: " + p.arrivalTime);
System.out.println("Burst time: " + p.burstTime);
System.out.println("Start time: " + p.startTime);
System.out.println("Finish time: " + p.finishTime);
System.out.println("Turnaround time: " + p.turnaroundTime);
System.out.println("Weighted turnaround time: " + p.weightedTurnaroundTime);
} else {
queue.remove(p);
p.startTime = currentTime;
p.finishTime = currentTime + timeQuantum;
p.burstTime -= timeQuantum;
currentTime = p.finishTime;
queue.add(p);
}
}
double totalTurnaroundTime = 0;
double totalWeightedTurnaroundTime = 0;
for (Process p : processes) {
if (p.completed) {
totalTurnaroundTime += p.turnaroundTime;
totalWeightedTurnaroundTime += p.weightedTurnaroundTime;
}
}
double avgTurnaroundTime = totalTurnaroundTime / processes.size();
double avgWeightedTurnaroundTime = totalWeightedTurnaroundTime / processes.size();
System.out.println("\nAverage turnaround time: " + avgTurnaroundTime);
System.out.println("Average weighted turnaround time: " + avgWeightedTurnaroundTime);
}
public static void priority(List<Process> processes) {
System.out.println("\nPriority scheduling:");
int currentTime = 0;
List<Process> queue = new ArrayList<>();
while (!processes.isEmpty() || !queue.isEmpty()) {
for (Process p : processes) {
if (p.arrivalTime <= currentTime) {
queue.add(p);
}
}
if (queue.isEmpty()) {
currentTime++;
continue;
}
Process highestPriority = queue.get(0);
for (int i = 1; i < queue.size(); i++) {
Process p = queue.get(i);
if (p.priority < highestPriority.priority) {
highestPriority = p;
}
}
queue.remove(highestPriority);
highestPriority.startTime = currentTime;
highestPriority.finishTime = currentTime + highestPriority.burstTime;
highestPriority.turnaroundTime = highestPriority.finishTime - highestPriority.arrivalTime;
highestPriority.weightedTurnaroundTime = highestPriority.turnaroundTime / (double) highestPriority.burstTime;
highestPriority.completed = true;
currentTime = highestPriority.finishTime;
System.out.println("Process " + highestPriority.name + ":");
System.out.println("Arrival time: " + highestPriority.arrivalTime);
System.out.println("Burst time: " + highestPriority.burstTime);
System.out.println("Start time: " + highestPriority.startTime);
System.out.println("Finish time: " + highestPriority.finishTime);
System.out.println("Turnaround time: " + highestPriority.turnaroundTime);
System.out.println("Weighted turnaround time: " + highestPriority.weightedTurnaroundTime);
}
double totalTurnaroundTime = 0;
double totalWeightedTurnaroundTime = 0;
for (Process p : processes) {
if (p.completed) {
totalTurnaroundTime += p.turnaroundTime;
totalWeightedTurnaroundTime += p.weightedTurnaroundTime;
}
}
double avgTurnaroundTime = totalTurnaroundTime / processes.size();
double avgWeightedTurnaroundTime = totalWeightedTurnaroundTime / processes.size();
System.out.println("\nAverage turnaround time: " + avgTurnaroundTime);
System.out.println("Average weighted turnaround time: " + avgWeightedTurnaroundTime);
}
}
```
程序中定义了一个进程类 `Process`,包含了进程的名称、到达时间、运行时间、优先级、开始时间、结束时间、周转时间、带权周转时间和完成状态。然后实现了四种调度算法:先来先服务(FCFS)、短作业优先(SJF)、时间片轮转(RR)和优先级算法。
在主函数中,首先输入进程数量和每个进程的详细信息。然后进入一个循环,用户可以选择调度算法。每个调度算法都会按照不同的方式对进程进行调度,并输出进程的详细信息和平均周转时间、带权周转时间。
值得注意的是,在时间片轮转算法中,需要输入每个时间片的长度。
阅读全文