sstf磁盘调度算法 C++
时间: 2023-07-09 14:19:45 浏览: 103
下面是一个简单的 C++ 实现 Scheduling SSTF 磁盘调度算法的示例代码:
```c++
#include<iostream>
#include<cstdlib>
using namespace std;
int main()
{
int queue[20], q_size, head, seek = 0, max, diff;
float avg;
cout << "Enter the size of queue: ";
cin >> q_size;
cout << "Enter queue: ";
for(int i = 1; i <= q_size; i++)
{
cin >> queue[i];
}
cout << "Enter initial head position: ";
cin >> head;
queue[0] = head;
for(int j = 0; j <= q_size-1; j++)
{
diff = abs(queue[j+1] - queue[j]);
seek += diff;
cout << "Move from " << queue[j] << " to " << queue[j+1] << " with seek " << diff << endl;
}
cout << "\nTotal seek time = " << seek << endl;
avg = seek/(float)q_size;
cout << "Average seek time = " << avg << endl;
return 0;
}
```
该程序首先要求用户输入磁盘队列的大小和初始磁头位置,然后接受队列中的元素。程序使用循环来计算磁头移动次数,并输出每一步的移动和寻道距离。最后,程序计算总寻道时间和平均寻道时间,并将其打印到标准输出。
注意,这只是一个简单的示例程序,它假设输入是正确的,并且没有进行错误检查。实际上,当实现磁盘调度算法时,必须考虑到更多的因素。
阅读全文