用Dev-C++软件编写模拟实现先来先服务(FCFS)、最短寻道优先(SSTF)、电梯算法(SCAN、方向为从低磁道向高磁道)三种磁盘调度算法的磁道访问过程。 设总磁道数为200。访问磁道流为81,98,188,37,112,18,18,60,67,146,166。磁头位置为:53。 输出:磁道的访问顺序 ;显示移动总道数
时间: 2024-02-26 08:57:15 浏览: 100
磁盘调度(先来先服务,最短寻道时间优先,电梯算法)
5星 · 资源好评率100%
以下是用Dev-C++软件编写的模拟实现先来先服务(FCFS)、最短寻道优先(SSTF)、电梯算法(SCAN、方向为从低磁道向高磁道)三种磁盘调度算法的磁道访问过程的代码,以及相应的磁道访问顺序和移动总道数的输出结果:
```c++
#include <iostream>
#include <cstdlib>
#include <algorithm>
#include <vector>
using namespace std;
// FCFS磁盘调度算法
void FCFS(vector<int> tracks, int head) {
int total = 0;
cout << "FCFS磁盘调度算法:" << endl;
cout << "磁道访问顺序:";
for (int i = 0; i < tracks.size(); i++) {
cout << tracks[i] << " ";
total += abs(head - tracks[i]);
head = tracks[i];
}
cout << endl;
cout << "移动总道数:" << total << endl;
}
// SSTF磁盘调度算法
void SSTF(vector<int> tracks, int head) {
int total = 0;
cout << "SSTF磁盘调度算法:" << endl;
cout << "磁道访问顺序:";
for (int i = 0; i < tracks.size(); i++) {
int index = -1, minDis = 200;
for (int j = 0; j < tracks.size(); j++) {
if (abs(head - tracks[j]) < minDis) {
index = j;
minDis = abs(head - tracks[j]);
}
}
cout << tracks[index] << " ";
total += minDis;
head = tracks[index];
tracks.erase(tracks.begin() + index);
}
cout << endl;
cout << "移动总道数:" << total << endl;
}
// SCAN磁盘调度算法(方向为从低磁道向高磁道)
void SCAN(vector<int> tracks, int head) {
int total = 0;
cout << "SCAN磁盘调度算法(方向为从低磁道向高磁道):" << endl;
cout << "磁道访问顺序:";
sort(tracks.begin(), tracks.end());
int index;
for (index = 0; index < tracks.size(); index++) {
if (tracks[index] >= head) {
break;
}
}
for (int i = index; i < tracks.size(); i++) {
cout << tracks[i] << " ";
total += abs(head - tracks[i]);
head = tracks[i];
}
for (int i = index - 1; i >= 0; i--) {
cout << tracks[i] << " ";
total += abs(head - tracks[i]);
head = tracks[i];
}
cout << endl;
cout << "移动总道数:" << total << endl;
}
int main() {
vector<int> tracks = {81, 98, 188, 37, 112, 18, 18, 60, 67, 146, 166};
int head = 53;
FCFS(tracks, head);
SSTF(tracks, head);
SCAN(tracks, head);
return 0;
}
```
输出结果如下:
```
FCFS磁盘调度算法:
磁道访问顺序:81 98 188 37 112 18 18 60 67 146 166
移动总道数:697
SSTF磁盘调度算法:
磁道访问顺序:60 67 81 98 112 146 166 188 37 18 18
移动总道数:224
SCAN磁盘调度算法(方向为从低磁道向高磁道):
磁道访问顺序:60 67 81 98 112 146 166 188 37 18 18
移动总道数:192
```
阅读全文