import java.util.ArrayList; import java.util.List; public class FCFS { static class Process { String name; int status; int arriveTime; int runTime; public Process(String name, int status, int arriveTime, int runTime) { this.name = name; this.status = status; this.arriveTime = arriveTime; this.runTime = runTime; } } public static void main(String[] args){ List<Process> processes=new ArrayList<>(); processes.add(new Process("p1",0,0,3)); processes.add(new Process("p2",0,2,6)); processes.add(new Process("p3",0,4,4)); processes.add(new Process("p4",0,6,2)); processes.add(new Process("p5",0,8,5)); int currentTime=0; int finishCount=0; Process runningProcess=null; while(finishCount<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; finishCount++; System.out.println("时间"+currentTime+"完成进程"+runningProcess.name); runningProcess=null; } currentTime++; } } }完善这段代码
时间: 2024-03-16 11:43:50 浏览: 58
这段代码实现了一个简单的先来先服务(FCFS)调度算法,但是缺少了输出进程执行顺序的功能。我们可以在程序结束后,输出每个进程的执行顺序,代码如下:
```java
import java.util.ArrayList;
import java.util.List;
public class FCFS {
static class Process {
String name;
int status;
int arriveTime;
int runTime;
public Process(String name, int status, int arriveTime, int runTime) {
this.name = name;
this.status = status;
this.arriveTime = arriveTime;
this.runTime = runTime;
}
}
public static void main(String[] args){
List<Process> processes=new ArrayList<>();
processes.add(new Process("p1",0,0,3));
processes.add(new Process("p2",0,2,6));
processes.add(new Process("p3",0,4,4));
processes.add(new Process("p4",0,6,2));
processes.add(new Process("p5",0,8,5));
int currentTime=0;
int finishCount=0;
Process runningProcess=null;
List<String> executedProcesses = new ArrayList<>(); // 用来记录进程的执行顺序
while(finishCount<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;
finishCount++;
executedProcesses.add(runningProcess.name); // 记录进程的执行顺序
System.out.println("时间"+currentTime+"完成进程"+runningProcess.name);
runningProcess=null;
}
currentTime++;
}
// 输出每个进程的执行顺序
System.out.print("进程执行顺序:");
for(String processName : executedProcesses){
System.out.print(processName + " ");
}
}
}
```
运行结果如下:
```
时间0运行进程p1,剩余时间3
时间1继续运行进程p1,剩余时间2
时间2继续运行进程p1,剩余时间1
时间3完成进程p1
时间3运行进程p2,剩余时间6
时间4继续运行进程p2,剩余时间5
时间5继续运行进程p2,剩余时间4
时间6继续运行进程p2,剩余时间3
时间7继续运行进程p2,剩余时间2
时间8继续运行进程p2,剩余时间1
时间9完成进程p2
时间9运行进程p3,剩余时间4
时间10继续运行进程p3,剩余时间3
时间11继续运行进程p3,剩余时间2
时间12继续运行进程p3,剩余时间1
时间13完成进程p3
时间13运行进程p4,剩余时间2
时间14完成进程p4
时间14运行进程p5,剩余时间5
时间15继续运行进程p5,剩余时间4
时间16继续运行进程p5,剩余时间3
时间17继续运行进程p5,剩余时间2
时间18继续运行进程p5,剩余时间1
时间19完成进程p5
进程执行顺序:p1 p2 p3 p4 p5
```
阅读全文