本文内容:武汉理工大学操作系统的第二次课内实验
开发环境:IntelliJ IDEA2021.3
二、实验要求 1.实验内容模拟实现磁盘调度
2.实验要求(1)选择1~3种磁盘调度算法(先来先服务算法、最短寻道时间优先算法、电梯算法)模拟 实现磁盘调度;
(2)能够输入当前磁头的位置磁头移动方向、磁头访问请求序列等;
(3)计算磁头移动的总磁道数;
(4)能够显示磁盘调度结果(磁头依次访问的磁道顺序)
三、实验原理与设计 1.算法原理(1)先来先服务算法:按先来后到次序服务。这个算法实际上不考虑访问者要求访问的物理 位置,只考虑访问者提出访问的先后顺序。
(2)最短寻道时间优先算法:该算法从等待访问者中挑选距离最近的访问请求进行访问,而 不考虑访问请求的先后顺序。
(3)电梯算法:该算法是从磁头位置开始,先选择大于或小于磁头作为移动方向,优先完成 该方向上的申请序列,然后再完成另一个方向上的申请序列。
2.主要仪器设备及耗材装有windos10系统的PC机一台;
Java JDK17
IntelliJ IDEA 2021.3
3.算法设计
public void firstComeFirstServe(){
Scanner scan = new Scanner(System.in);
System.out.println("磁头位置为:");
int head = scan.nextInt();
visitList[0] = head;
System.arraycopy(requestList, 0, visitList, 1, requestList.length);
System.out.println("先来先服务:");
showPath();
showSum();
}
public void minSeekTime(){
Scanner scan = new Scanner(System.in);
System.out.println("磁头位置为:");
int head = scan.nextInt();
visitList[0] = head;
linkedList list = new linkedList<>();
for (int j : requestList) {
list.add(j);
}
for(int i = 1 ;i < visitList.length;i++){
visitList[i] = findNear(list,head);
}
System.out.println("最短寻道时间优先:");
showPath();
showSum();
}
public int findNear(linkedListlist ,int head){
int index = 0;
int min = 1000;
for(int i =0;i< list.size();i++){
if(Math.abs(head- list.get(i))
public void theElevatorAlgorithm() {
Scanner scan = new Scanner(System.in);
System.out.println("磁头位置为:");
int head = scan.nextInt();
visitList[0] = head;
PriorityQueue compareSmall = new PriorityQueue<>(10, new Comparator<>() {
@Override
public int compare(Integer o1, Integer o2) {
return o2 - o1;
}
});
PriorityQueue compareBig = new PriorityQueue<>(10);
for (int j : requestList) {
if (j > head) {
compareBig.add(j);
} else if (j < head) {
compareSmall.add(j);
}
}
int index = 1;
int command = scan.nextInt();
switch (command){
//选择‘1’即从磁头位置向右扫描
case 1 :{
while (!compareBig.isEmpty()){
visitList[index++] = compareBig.poll();
}
while(!compareSmall.isEmpty()){
visitList[index++] = compareSmall.poll();
}
break;
}
//选择‘2’即从磁头位置向左扫描
case 2 :{
while(!compareSmall.isEmpty()) {
visitList[index++] = compareSmall.poll();
}
while (!compareBig.isEmpty()){
visitList[index++] = compareBig.poll();
}
break;
}
default:{
System.out.println("输入错误!!!");
}
}
System.out.println("电梯算法:");
showPath();
showSum();
}
四、源码
链接:https://pan.baidu.com/s/1k95pNoSx3k6NoHoO5nRntw
提取码:jiei



