操作系统调度算法在Java中的高效实现与应用实践

前言
操作系统中的进程调度是确保系统资源高效利用的关键环节。不同的调度算法对系统的性能有着显著的影响。本文将深入探讨三种经典的进程调度算法:先来先服务(FCFS)、短作业优先(SJF)和高响应比优先(HRRN),并通过Java语言实现这些算法,展示其在实际应用中的效果。
一、先来先服务(FCFS)算法
1. 算法思想
先来先服务(FCFS)算法是最简单的调度算法。其核心思想是按照进程到达的先后顺序进行调度,即先到达的进程先获得CPU时间。这种算法实现简单,但可能导致“饥饿”现象,即短进程长时间等待。
2. 算法分析
假设有五个进程,其到达时间和服务时间如下:
进程号
进程名
到达时间
服务时间
1001
p1
9:40
20
1004
p4
10:10
10
1005
p5
10:05
30
1002
p2
9:55
15
1003
p3
9:45
25
按照FCFS算法,调度顺序为:p1, p3, p2, p5, p4。
3. Java实现
class Process {
int id;
int arrivalTime;
int burstTime;
int completionTime;
int turnaroundTime;
int waitingTime;

public Process(int id, int arrivalTime, int burstTime) {
this.id = id;
this.arrivalTime = arrivalTime;
this.burstTime = burstTime;
}
}

public class FCFS {
public static void main(String[] args) {
Process[] processes = {
new Process(1001, 940, 20),
new Process(1004, 1010, 10),
new Process(1005, 1005, 30),
new Process(1002, 955, 15),
new Process(1003, 945, 25)
};

Arrays.sort(processes, Comparator.comparingInt(p -> p.arrivalTime));

int currentTime = 0;
for (Process p : processes) {
p.completionTime = currentTime + p.burstTime;
p.turnaroundTime = p.completionTime – p.arrivalTime;
p.waitingTime = p.turnaroundTime – p.burstTime;
currentTime = p.completionTime;
System.out.println(“Process ” + p.id + “: Completion Time = ” + p.completionTime + “, Turnaround Time = ” + p.turnaroundTime + “, Waiting Time = ” + p.waitingTime);
}
}
}
二、短作业优先(SJF)算法
1. 算法思想
短作业优先(SJF)算法的核心思想是优先调度执行时间最短的作业。这种算法可以最小化平均等待时间,但可能导致长作业长时间等待。
2. 算法分析
继续使用上述五个进程的例子,按照SJF算法,调度顺序为:p1, p2, p4, p3, p5。
3. Java实现
public class SJF {
public static void main(String[] args) {
Process[] processes = {
new Process(1001, 940, 20),
new Process(1004, 1010, 10),
new Process(1005, 1005, 30),
new Process(1002, 955, 15),
new Process(1003, 945, 25)
};

int currentTime = 0;
int completed = 0;
while (completed < processes.length) { Process shortestJob = null; for (Process p : processes) { if (p.arrivalTime <= currentTime && (shortestJob == null || p.burstTime < shortestJob.burstTime)) { shortestJob = p; } } if (shortestJob != null) { shortestJob.completionTime = currentTime + shortestJob.burstTime; shortestJob.turnaroundTime = shortestJob.completionTime - shortestJob.arrivalTime; shortestJob.waitingTime = shortestJob.turnaroundTime - shortestJob.burstTime; currentTime = shortestJob.completionTime; completed++; System.out.println("Process " + shortestJob.id + ": Completion Time = " + shortestJob.completionTime + ", Turnaround Time = " + shortestJob.turnaroundTime + ", Waiting Time = " + shortestJob.waitingTime); } else { currentTime++; } } } } 三、高响应比优先(HRRN)算法 1. 算法思想 高响应比优先(HRRN)算法综合考虑了进程的等待时间和执行时间,计算每个进程的响应比,优先调度响应比最高的进程。响应比公式为: [ \text{响应比} = \frac{\text{等待时间} + \text{服务时间}}{\text{服务时间}} ] 2. 算法分析 继续使用上述五个进程的例子,按照HRRN算法,调度顺序会动态变化,具体计算过程较为复杂,但可以确保长作业不会长时间等待。 3. Java实现 public class HRRN { public static void main(String[] args) { Process[] processes = { new Process(1001, 940, 20), new Process(1004, 1010, 10), new Process(1005, 1005, 30), new Process(1002, 955, 15), new Process(1003, 945, 25) }; int currentTime = 0; int completed = 0; while (completed < processes.length) { Process highestResponseRatioJob = null; for (Process p : processes) { if (p.arrivalTime <= currentTime) { double responseRatio = (double) (currentTime - p.arrivalTime + p.burstTime) / p.burstTime; if (highestResponseRatioJob == null || responseRatio > highestResponseRatioJob.responseRatio) {
highestResponseRatioJob = p;
highestResponseRatioJob.responseRatio = responseRatio;
}
}
}
if (highestResponseRatioJob != null) {
highestResponseRatioJob.completionTime = currentTime + highestResponseRatioJob.burstTime;
highestResponseRatioJob.turnaroundTime = highestResponseRatioJob.completionTime – highestResponseRatioJob.arrivalTime;
highestResponseRatioJob.waitingTime = highestResponseRatioJob.turnaroundTime – highestResponseRatioJob.burstTime;
currentTime = highestResponseRatioJob.completionTime;
completed++;
System.out.println(“Process ” + highestResponseRatioJob.id + “: Completion Time = ” + highestResponseRatioJob.completionTime + “, Turnaround Time = ” + highestResponseRatioJob.turnaroundTime + “, Waiting Time = ” + highestResponseRatioJob.waitingTime);
} else {
currentTime++;
}
}
}
}
四、总结
本文通过Java语言实现了三种经典的操作系统进程调度算法:FCFS、SJF和HRRN。每种算法都有其优缺点,适用于不同的场景。FCFS算法简单易行,但可能导致长作业长时间等待;SJF算法能最小化平均等待时间,但可能导致长作业“饥饿”;HRRN算法则通过动态调整响应比,较好地平衡了长作业和短作业的调度。
通过实际代码实现,我们可以更直观地理解这些算法的工作原理和性能表现,为实际操作系统设计和优化提供参考。希望本文能为读者在操作系统学习和实践中提供帮助。