磁盘调度算法先来先服务代码
时间: 2023-08-06 20:10:44 浏览: 50
先来先服务的调度算法
以下是磁盘调度算法先来先服务的C++代码实现:
```c++
#include <bits/stdc++.h>
using namespace std;
int main()
{
int n, start;
cout << "Enter the number of disk requests: ";
cin >> n;
cout << "Enter the initial position of the disk head: ";
cin >> start;
vector<int> requests(n);
cout << "Enter the disk requests: ";
for (int i = 0; i < n; ++i) {
cin >> requests[i];
}
int total = 0;
cout << "The order of disk access is: ";
for (int i = 0; i < n; ++i) {
total += abs(requests[i] - start);
start = requests[i];
cout << requests[i] << " ";
}
cout << "\nTotal head movement: " << total << "\n";
return 0;
}
```
这里使用了C++的STL库中的vector容器来存储磁盘请求,abs()函数来计算磁盘头移动的距离,输出顺序即为先来先服务的顺序。
阅读全文