时间片轮转进程调度算法java
时间: 2023-12-18 10:30:20 浏览: 117
```java
// Java实现时间片轮转进程调度算法
public class RoundRobinScheduling {
public static void main(String[] args) {
int[] process = {1, 2, 3, 4}; // 进程号
int[] arrivalTime = {0, 1, 2, 3}; // 到达时间
int[] burstTime = {5, 3, 8, 6}; // 服务时间
int timeQuantum = 2; // 时间片大小
int[] remainingTime = new int[process.length];
for (int i = 0; i < process.length; i++) {
remainingTime[i] = burstTime[i];
}
int currentTime = 0;
while (true) {
boolean done = true;
for (int i = 0; i < process.length; i++) {
if (remainingTime[i] > 0) {
done = false;
if (remainingTime[i] > timeQuantum) {
currentTime += timeQuantum;
remainingTime[i] -= timeQuantum;
} else {
currentTime += remainingTime[i];
remainingTime[i] = 0;
int completionTime = currentTime;
int turnAroundTime = completionTime - arrivalTime[i];
int waitingTime = turnAroundTime - burstTime[i];
System.out.println("进程 " + process[i] + " 周转时间: " + turnAroundTime + " 等待时间: " + waitingTime);
}
}
}
if (done) {
break;
}
}
}
}
```
阅读全文