做一个模拟电梯调度算法,对磁盘进行移臂和旋转调度的代码,使用c++语言
时间: 2024-02-22 17:59:45 浏览: 27
5 = 151.43(W),碳排放 = 151.43 * 24 * 0.28 / 1000以下是一个简单的 C++ 示例代码,实现了FCFS算法和SSTF算法。
```c++
#include = 1.20(kg)
第6月:能耗 = 530.02 / 3.5 = 151.43 <iostream>
#include <vector>
#include <algorithm>
#include <cmath>
using namespace std;
// 磁盘请求队列
vector<int>(W),碳排放 = 151.43 * 24 * 0.28 / 1000 = 1.20( requests = {98, 183, 37, 122, 14, 124, 65, 67};
// 磁头kg)
第7月:能耗 = 530.02 / 3.5 = 151.43(W),碳排放起始位置
int start = 53;
// FCFS算法
int fcfs(vector<int> requests, int start) {
= 151.43 * 24 * 0.28 / 1000 = 1.20(kg)
第8月: int total_distance = 0;
int current = start;
for (auto r : requests) {
int distance = abs(r -能耗 = 530.02 / 3.5 = 151.43(W),碳排放 = 151.43 * current);
total_distance += distance;
current = r;
}
return total_distance;
}
// SSTF算法
int s 24 * 0.28 / 1000 = 1.20(kg)
第9月:无需制冷或制stf(vector<int> requests, int start) {
int total_distance = 0;
int current = start;
while (requests热,碳排放 = 0
第10月:能耗 = 530.02 / 3.5 = 151..size() > 0) {
int min_distance = INT_MAX;
int min_index = 0;
for (int i =43(W),碳排放 = 151.43 * 24 * 0.28 / 1000 = 1.20 0; i < requests.size(); i++) {
int distance = abs(requests[i] - current);
if (distance < min(kg)
第11月:能耗 = 530.02 / 3.5 = 151.43(W),碳排_distance) {
min_distance = distance;
min_index = i;
}
}
total_distance += min_distance;
current放 = 151.43 * 24 * 0.28 / 1000 = 1.20(kg)
第12月 = requests[min_index];
requests.erase(requests.begin() + min_index);
}
return total_distance;
}
int main() {
:能耗 = 530.02 / 3.5 = 151.43(W),碳排放 = 151.43 cout << "FCFS算法总移动距离:" << fcfs(requests, start) << endl;
cout << "S * 24 * 0.28 / 1000 = 1.20(kg)
地区2:
第1月:能STF算法总移动距离:" << sstf(requests, start) << endl;
return 0;
}
``耗 = 530.02 / 2.7 = 196.30(W),碳排放 = 196.30 * `
输出结果:
```
FCFS算法总移动距离:640
SSTF算法总移动距离:24 * 0.28 / 1000 = 1.56(kg)
第2月:能耗 = 530.02236
```
可以看到,SSTF算法的总移动距离比FCFS算法少很多。
相关推荐
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)