while (finishedCount < processes.size()) { // 找到到达时间最早的进程 Process earliestProcess = null; for (Process process : processes) { if (process.status == 0 && process.arriveTime <= currentTime) { if (earliestProcess == null || process.arriveTime < earliestProcess.arriveTime) { earliestProcess = process; } } } if (earliestProcess != null) { // 运行进程 earliestProcess.status = 2; runningProcess = earliestProcess; System.out.println("时间 " + currentTime + " 运行进程 " + runningProcess.name + ",剩余时间 " + runningProcess.runTime); } else if (runningProcess != null) { // 继续运行进程 runningProcess.runTime--; System.out.println("时间 " + currentTime + " 继续运行进程 " + runningProcess.name + ",剩余时间 " + runningProcess.runTime); if (runningProcess.runTime == 0) { // 完成进程 runningProcess.status = 3; finishedCount++; System.out.println("时间 " + (currentTime + 1) + " 完成进程 " + runningProcess.name); runningProcess = null; } } currentTime++; } System.out.println("所有进程完成,总耗时 " + currentTime); } }是怎么实现先来先服务的
时间: 2024-03-14 19:48:49 浏览: 105
解析C#中用Process类杀死进程,执行命令的深入分析
这段代码实现的是先来先服务(FCFS)调度算法。在循环中,首先找到到达时间最早的进程 earliestProcess,并将其设置为正在运行的进程 runningProcess。如果没有到达时间小于等于当前时间的进程,则继续运行正在运行的进程 runningProcess,直到其运行时间为 0。这样,进程按照它们到达的顺序依次执行,因此实现了先来先服务的调度方式。
阅读全文