请写一段完整的,有FCFS,SSTF,SCAN,CSCAN算法的磁盘调度程序
时间: 2024-03-25 12:41:21 浏览: 23
好的,这是一个简单的磁盘调度程序,它包括了FCFS、SSTF、SCAN和CSCAN算法。请注意,这只是一个示例程序,可以根据需要进行修改和优化。
```
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
// 磁盘请求结构体
struct Request {
int id; // 请求ID
int track; // 请求磁道号
};
// FCFS算法
void FCFS(vector<Request>& requests, int start) {
int total = 0; // 总移动距离
int current = start; // 当前磁头所在磁道号
for (int i = 0; i < requests.size(); i++) {
total += abs(requests[i].track - current);
current = requests[i].track;
}
cout << "FCFS算法:" << total << endl;
}
// SSTF算法
void SSTF(vector<Request>& requests, int start) {
int total = 0; // 总移动距离
int current = start; // 当前磁头所在磁道号
while (!requests.empty()) {
// 找到离当前磁头最近的请求
int minDist = abs(requests[0].track - current);
int minIndex = 0;
for (int i = 1; i < requests.size(); i++) {
int dist = abs(requests[i].track - current);
if (dist < minDist) {
minDist = dist;
minIndex = i;
}
}
total += minDist;
current = requests[minIndex].track;
requests.erase(requests.begin() + minIndex);
}
cout << "SSTF算法:" << total << endl;
}
// SCAN算法
void SCAN(vector<Request>& requests, int start) {
int total = 0; // 总移动距离
int current = start; // 当前磁头所在磁道号
bool direction = true; // 扫描方向:true表示向外,false表示向内
// 先将请求按磁道号排序
sort(requests.begin(), requests.end(), [](Request& r1, Request& r2) {
return r1.track < r2.track;
});
// 找到当前磁头所在位置
for (int i = 0; i < requests.size(); i++) {
if (requests[i].track >= start) {
current = i;
break;
}
}
// 先扫描到最外面,再扫描到最里面
while (!requests.empty()) {
if (direction) {
// 向外扫描
for (int i = current; i < requests.size(); i++) {
total += abs(requests[i].track - current);
current = i;
}
direction = false;
} else {
// 向内扫描
for (int i = current; i >= 0; i--) {
total += abs(requests[i].track - current);
current = i;
}
direction = true;
}
// 删除已经处理的请求
requests.erase(requests.begin(), requests.begin() + current + 1);
current = 0;
}
cout << "SCAN算法:" << total << endl;
}
// CSCAN算法
void CSCAN(vector<Request>& requests, int start) {
int total = 0; // 总移动距离
int current = start; // 当前磁头所在磁道号
// 先将请求按磁道号排序
sort(requests.begin(), requests.end(), [](Request& r1, Request& r2) {
return r1.track < r2.track;
});
// 找到当前磁头所在位置
for (int i = 0; i < requests.size(); i++) {
if (requests[i].track >= start) {
current = i;
break;
}
}
// 先扫描到最外面,然后跳到最里面再扫描到最外面
while (!requests.empty()) {
// 向外扫描
for (int i = current; i < requests.size(); i++) {
total += abs(requests[i].track - current);
current = i;
}
// 跳到最里面
total += abs(requests.back().track - current);
current = requests.size() - 1;
// 再向外扫描
for (int i = current; i >= 0; i--) {
total += abs(requests[i].track - current);
current = i;
}
// 删除已经处理的请求
requests.erase(requests.begin(), requests.begin() + current + 1);
current = 0;
}
cout << "CSCAN算法:" << total << endl;
}
int main() {
// 设置起始位置和请求队列
int start = 50;
vector<Request> requests = {
{1, 60},
{2, 20},
{3, 10},
{4, 90},
{5, 30},
{6, 70}
};
// 运行FCFS算法
FCFS(requests, start);
// 运行SSTF算法
requests = {
{1, 60},
{2, 20},
{3, 10},
{4, 90},
{5, 30},
{6, 70}
};
SSTF(requests, start);
// 运行SCAN算法
requests = {
{1, 60},
{2, 20},
{3, 10},
{4, 90},
{5, 30},
{6, 70}
};
SCAN(requests, start);
// 运行CSCAN算法
requests = {
{1, 60},
{2, 20},
{3, 10},
{4, 90},
{5, 30},
{6, 70}
};
CSCAN(requests, start);
return 0;
}
```
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.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)