SCAN磁盘调度求平均寻道长度java实现
时间: 2023-11-09 16:05:37 浏览: 102
实现SCAN磁盘调度算法可以采用以下步骤:
1. 输入磁盘请求序列和磁头初始位置
2. 对磁盘请求序列进行排序,得到升序的请求序列
3. 找到磁头最先到达的请求的位置,如果没有则磁头移动到最外侧的请求位置
4. 按照磁头移动的方向,依次处理请求序列,直到所有请求都被处理完毕
5. 计算平均寻道长度
以下是Java实现代码:
```
import java.util.Arrays;
public class SCAN {
public static void main(String[] args) {
int[] requests = { 23, 89, 132, 42, 187 };
int initialPosition = 100;
int maxTrack = 199;
int minTrack = 0;
int direction = 1; // 1表示向内扫描,-1表示向外扫描
int currentPosition = initialPosition;
int totalSeekTime = 0;
Arrays.sort(requests);
while (true) {
boolean found = false;
for (int i = 0; i < requests.length; i++) {
if (requests[i] == currentPosition) {
found = true;
requests[i] = -1; // 标记为已处理
totalSeekTime += Math.abs(currentPosition - initialPosition);
initialPosition = currentPosition;
break;
}
}
if (!found) {
if (direction == 1) {
totalSeekTime += Math.abs(maxTrack - currentPosition);
currentPosition = maxTrack;
direction = -1;
} else {
totalSeekTime += Math.abs(minTrack - currentPosition);
currentPosition = minTrack;
direction = 1;
}
} else {
if (direction == 1 && currentPosition == maxTrack) {
direction = -1;
} else if (direction == -1 && currentPosition == minTrack) {
direction = 1;
}
}
boolean allDone = true;
for (int i = 0; i < requests.length; i++) {
if (requests[i] != -1) {
allDone = false;
break;
}
}
if (allDone) {
break;
}
}
System.out.println("平均寻道长度:" + (double) totalSeekTime / requests.length);
}
}
```
这段代码实现了对磁盘请求序列 { 23, 89, 132, 42, 187 } 进行SCAN磁盘调度算法,并以磁头初始位置为100开始运行。最终输出平均寻道长度。
阅读全文