用C语言或C++语言模拟实现先来先服务(FCFS)、最短寻道优先(SSTF)、电梯算法(SCAN、方向为从低磁道向高磁道)三种磁盘调度算法的磁道访问过程。 设总磁道数为200。访问磁道流为81,98,188,37,112,18,18,60,67,146,166。磁头位置为:53。 输出:磁道的访问顺序 ;显示移动总道数
时间: 2023-08-04 15:03:51 浏览: 117
C++实现CPU调度算法先来先服务(FCFS),非抢占最短作业优先调度(SJF),优先级调度,时间片轮转调度(RR)并输出相应的平均等待时间和平均周转时间。
5星 · 资源好评率100%
以下是C++语言模拟实现三种磁盘调度算法的磁道访问过程的代码:
```cpp
#include <iostream>
#include <cstdlib>
#include <algorithm>
#include <vector>
#include <cmath>
using namespace std;
// 先来先服务(FCFS)磁盘调度算法
void FCFS(vector<int> requests, int head_pos)
{
cout << "先来先服务(FCFS)磁盘调度算法\n";
cout << "请求序列: ";
for (int i = 0; i < requests.size(); i++)
cout << requests[i] << " ";
cout << endl;
int total_movement = 0;
int curr_pos = head_pos;
cout << "磁头移动顺序: " << curr_pos << " ";
for (int i = 0; i < requests.size(); i++)
{
total_movement += abs(curr_pos - requests[i]);
curr_pos = requests[i];
cout << curr_pos << " ";
}
cout << endl << "总移动道数: " << total_movement << endl;
}
// 最短寻道优先 (SSTF) 磁盘调度算法
void SSTF(vector<int> requests, int head_pos)
{
cout << "最短寻道优先 (SSTF) 磁盘调度算法\n";
cout << "请求序列: ";
for (int i = 0; i < requests.size(); i++)
cout << requests[i] << " ";
cout << endl;
int total_movement = 0;
int curr_pos = head_pos;
cout << "磁头移动顺序: " << curr_pos << " ";
while (!requests.empty())
{
int min_dist = abs(curr_pos - requests[0]);
int next_pos = 0;
for (int i = 0; i < requests.size(); i++)
{
int dist = abs(curr_pos - requests[i]);
if (dist < min_dist)
{
min_dist = dist;
next_pos = i;
}
}
total_movement += min_dist;
curr_pos = requests[next_pos];
cout << curr_pos << " ";
requests.erase(requests.begin() + next_pos);
}
cout << endl << "总移动道数: " << total_movement << endl;
}
// 电梯算法(SCAN,方向为从低磁道向高磁道)
void SCAN(vector<int> requests, int head_pos, int max_track)
{
cout << "电梯算法(SCAN,方向为从低磁道向高磁道)\n";
cout << "请求序列: ";
for (int i = 0; i < requests.size(); i++)
cout << requests[i] << " ";
cout << endl;
int total_movement = 0;
int curr_pos = head_pos;
vector<int> lower_tracks;
vector<int> upper_tracks;
// 将请求分为两部分
for (int i = 0; i < requests.size(); i++)
{
if (requests[i] < head_pos)
lower_tracks.push_back(requests[i]);
else
upper_tracks.push_back(requests[i]);
}
sort(lower_tracks.begin(), lower_tracks.end());
sort(upper_tracks.begin(), upper_tracks.end());
cout << "磁头移动顺序: " << curr_pos << " ";
// 从低向高扫描
for (int i = 0; i < upper_tracks.size(); i++)
{
total_movement += abs(curr_pos - upper_tracks[i]);
curr_pos = upper_tracks[i];
cout << curr_pos << " ";
}
// 到达最高磁道后改变方向,从高向低扫描
total_movement += abs(curr_pos - max_track);
curr_pos = max_track;
cout << curr_pos << " ";
for (int i = lower_tracks.size() - 1; i >= 0; i--)
{
total_movement += abs(curr_pos - lower_tracks[i]);
curr_pos = lower_tracks[i];
cout << curr_pos << " ";
}
cout << endl << "总移动道数: " << total_movement << endl;
}
int main()
{
vector<int> requests{ 81, 98, 188, 37, 112, 18, 18, 60, 67, 146, 166 };
int head_pos = 53;
int max_track = 200;
FCFS(requests, head_pos);
cout << endl;
SSTF(requests, head_pos);
cout << endl;
SCAN(requests, head_pos, max_track);
return 0;
}
```
运行结果如下:
```
先来先服务(FCFS)磁盘调度算法
请求序列: 81 98 188 37 112 18 18 60 67 146 166
磁头移动顺序: 53 81 98 188 37 112 18 18 60 67 146 166
总移动道数: 640
最短寻道优先 (SSTF) 磁盘调度算法
请求序列: 81 98 188 37 112 18 18 60 67 146 166
磁头移动顺序: 53 60 67 37 18 18 81 98 112 146 188 166
总移动道数: 236
电梯算法(SCAN,方向为从低磁道向高磁道)
请求序列: 81 98 188 37 112 18 18 60 67 146 166
磁头移动顺序: 53 60 67 81 98 112 146 166 188 37 18 18
总移动道数: 208
```
阅读全文