Write a program that implements the following disk-scheduling algorithms: a. FCFS b. SSTF c. SCAN d. C-SCAN e. LOOK f. C-LOOK Your program will service a disk with 5,000 cylinders numbered 0 to 4,999. The program will generate a random series of 1,000 cylinder requests and service them according to each of the algorithms listed above. The program will be passed the initial position of the disk head (as a parameter on the command line) and report the total amount of head movement required by each algorithm.
时间: 2024-01-31 07:02:21 浏览: 154
以下是实现上述磁盘调度算法的示例代码,使用了C++11的随机数生成器和命令行参数:
```c++
#include <iostream>
#include <cstdlib>
#include <vector>
#include <algorithm>
#include <random>
#include <cmath>
using namespace std;
// 磁盘请求结构体
struct Request
{
int cylinder; // 磁道号
int arrival_time; // 到达时间
};
// FCFS算法
int fcfs(vector<Request>& requests, int start)
{
int head = start, sum = 0;
for (auto r : requests)
{
sum += abs(head - r.cylinder);
head = r.cylinder;
}
return sum;
}
// SSTF算法
int sstf(vector<Request>& requests, int start)
{
int head = start, sum = 0;
while (!requests.empty())
{
int index = -1, min_dist = INT_MAX;
for (int i = 0; i < requests.size(); i++)
{
int dist = abs(requests[i].cylinder - head);
if (dist < min_dist)
{
min_dist = dist;
index = i;
}
}
sum += min_dist;
head = requests[index].cylinder;
requests.erase(requests.begin() + index);
}
return sum;
}
// SCAN算法
int scan(vector<Request>& requests, int start)
{
int head = start, sum = 0;
vector<Request> left, right;
for (auto r : requests)
{
if (r.cylinder < head)
left.push_back(r);
else
right.push_back(r);
}
sort(left.begin(), left.end(), [](const Request& r1, const Request& r2) { return r1.cylinder > r2.cylinder; });
sort(right.begin(), right.end(), [](const Request& r1, const Request& r2) { return r1.cylinder < r2.cylinder; });
for (auto r : right)
{
sum += abs(head - r.cylinder);
head = r.cylinder;
}
if (!left.empty())
{
sum += abs(head - left.front().cylinder);
head = left.front().cylinder;
for (auto r : left)
{
sum += abs(head - r.cylinder);
head = r.cylinder;
}
}
return sum;
}
// C-SCAN算法
int cscan(vector<Request>& requests, int start)
{
int head = start, sum = 0;
vector<Request> left, right;
for (auto r : requests)
{
if (r.cylinder < head)
left.push_back(r);
else
right.push_back(r);
}
sort(left.begin(), left.end(), [](const Request& r1, const Request& r2) { return r1.cylinder > r2.cylinder; });
sort(right.begin(), right.end(), [](const Request& r1, const Request& r2) { return r1.cylinder < r2.cylinder; });
for (auto r : right)
{
sum += abs(head - r.cylinder);
head = r.cylinder;
}
if (!left.empty())
{
sum += abs(head - 4999);
head = 4999;
for (auto r : left)
{
sum += abs(head - r.cylinder);
head = r.cylinder;
}
}
return sum;
}
// LOOK算法
int look(vector<Request>& requests, int start)
{
int head = start, sum = 0;
vector<Request> left, right;
for (auto r : requests)
{
if (r.cylinder < head)
left.push_back(r);
else
right.push_back(r);
}
sort(left.begin(), left.end(), [](const Request& r1, const Request& r2) { return r1.cylinder > r2.cylinder; });
sort(right.begin(), right.end(), [](const Request& r1, const Request& r2) { return r1.cylinder < r2.cylinder; });
for (auto r : right)
{
sum += abs(head - r.cylinder);
head = r.cylinder;
}
if (!left.empty())
{
sum += abs(head - left.back().cylinder);
head = left.back().cylinder;
for (auto it = left.rbegin(); it != left.rend(); it++)
{
sum += abs(head - it->cylinder);
head = it->cylinder;
}
}
return sum;
}
// C-LOOK算法
int clook(vector<Request>& requests, int start)
{
int head = start, sum = 0;
vector<Request> left, right;
for (auto r : requests)
{
if (r.cylinder < head)
left.push_back(r);
else
right.push_back(r);
}
sort(left.begin(), left.end(), [](const Request& r1, const Request& r2) { return r1.cylinder > r2.cylinder; });
sort(right.begin(), right.end(), [](const Request& r1, const Request& r2) { return r1.cylinder < r2.cylinder; });
for (auto r : right)
{
sum += abs(head - r.cylinder);
head = r.cylinder;
}
if (!left.empty())
{
sum += abs(head - left.back().cylinder);
head = left.back().cylinder;
for (auto it = left.rbegin(); it != left.rend(); it++)
{
sum += abs(head - it->cylinder);
head = it->cylinder;
}
sum += abs(head - right.front().cylinder);
head = right.front().cylinder;
for (auto r : right)
{
sum += abs(head - r.cylinder);
head = r.cylinder;
}
}
return sum;
}
int main(int argc, char* argv[])
{
if (argc < 2)
{
cerr << "用法:disk_sched <start>\n";
return -1;
}
int start = atoi(argv[1]);
vector<Request> requests(1000);
random_device rd;
mt19937 gen(rd());
uniform_int_distribution<> dis(0, 4999);
for (auto& r : requests)
r.cylinder = dis(gen);
sort(requests.begin(), requests.end(), [](const Request& r1, const Request& r2) { return r1.arrival_time < r2.arrival_time; });
cout << "FCFS算法扫描长度为:" << fcfs(requests, start) << endl;
cout << "SSTF算法扫描长度为:" << sstf(requests, start) << endl;
cout << "SCAN算法扫描长度为:" << scan(requests, start) << endl;
cout << "C-SCAN算法扫描长度为:" << cscan(requests, start) << endl;
cout << "LOOK算法扫描长度为:" << look(requests, start) << endl;
cout << "C-LOOK算法扫描长度为:" << clook(requests, start) << endl;
return 0;
}
```
该程序首先从命令行参数中读入磁头的初始位置,然后使用C++11的随机数生成器生成1000个磁盘请求,磁道号随机分布在0到4999之间。接着按照到达时间对请求进行排序,然后依次使用FCFS、SSTF、SCAN、C-SCAN、LOOK和C-LOOK算法对请求进行服务,并输出每种算法的扫描长度。
阅读全文