设计程序模拟先来先服务FCFS、最短寻道时间优先SSTF、SCAN和循环SCAN算法的工作过程。假设有n个磁道号所组成的磁道访问序列,给定开始磁道号m和磁头移动的方向(正向或者反向),分别利用不同的磁盘调度算法访问磁道序列,给出每一次访问的磁头移动距离,计算每种算法的平均寻道长度。
程序要求:
1)利用先来先服务FCFS、最短寻道时间优先SSTF、SCAN和循环SCAN算法模拟磁道访问过程。
2)模拟四种算法的磁道访问过程,给出每个磁道访问的磁头移动距离。
3)输入:磁道个数n和磁道访问序列,开始磁道号m和磁头移动方向(对SCAN和循环SCAN算法有效),算法选择1-FCFS,2-SSTF,3-SCAN,4-循环SCAN。
4)输出:每种算法的平均寻道长度。
完整代码
#include <iostream>
#include <fstream>
#include <iomanip>
#include <cmath>
#include <stdlib.h>
using namespace std;
#define MaxNumber 100
int TrackOrder[MaxNumber];//初始磁道序列
int MoveDistance[MaxNumber];//磁头移动距离(磁道数)
double AverageDistance;//磁头平均移动距离
int direction;//选择磁头方向
int TrackNum;//磁道数
int StartTrack;//开始磁道号m
int VisitOrder[MaxNumber];//访问磁道序列
bool isVisit[MaxNumber];//标记是否被访问过
int SumDistance;//磁头移动的总距离
int choose;
void input();//输入起始磁道号、磁道顺序
void initial();
void output();
void FCFS();//先来先服务,先进先出
void SSTF();//最短寻道时间优先
void SCAN();//扫描,从开始磁道沿选择方向扫描,直到没有要访问的磁道在沿反方向扫描
void CSCAN();//循环扫描,自开始磁道始终沿一个方向扫描,直到没有要访问的磁道再从最里圈或最外圈扫描
void chooseAlgorithm();
int main(){
input();
chooseAlgorithm();
return 0;
}
void input(){
cout<<"请输入磁道个数:";
cin>>TrackNum;
cout<<"请输入磁道序列:";
for (int i=0;i<TrackNum;i++)
{
cin>>TrackOrder[i];
}
cout<<"请输入当前磁道号(开始磁道):";
cin>>StartTrack;
}
void initial(){
for (int i=0;i<TrackNum;i++){
MoveDistance[i] = 0;
VisitOrder[i] = TrackOrder[i];
isVisit[i] = false;
}
SumDistance = 0;
AverageDistance = 0;
}
void output(){
cout<<"从"<<StartTrack<<"号磁道开始"<<endl;
cout<<"访问的下一个磁道号"<<"\t"<<"移动磁道数"<<"\t"<<endl;
for (int i=0;i<TrackNum;i++){
cout<<VisitOrder[i]<<"\t\t\t"<<MoveDistance[i]<<"\t"<<endl;
}
cout<<"移动的总磁道数:"<< SumDistance<<endl;
cout<<"平均寻道长度:"<<setprecision(4)<<AverageDistance<<endl;
cout<<endl;
}
//先来先服务,先进先出
void FCFS(){
cout<<endl;
cout<<"先来先服务(FCFS)"<<endl;
initial();
//按照输入顺序依次访问磁道
MoveDistance[0] = abs(TrackOrder[0]-StartTrack);
SumDistance = MoveDistance[0];
VisitOrder[0] = TrackOrder[0];
for (int i=1;i<TrackNum;i++){
MoveDistance[i] = abs(TrackOrder[i]-TrackOrder[i-1]);
SumDistance += MoveDistance[i];
VisitOrder[i] = TrackOrder[i];
}
AverageDistance = SumDistance*1.0/TrackNum;
output();
}
//最短寻道时间优先
void SSTF(){
cout<<endl;
cout<<"最短寻道时间优先(SSTF)"<<endl;
initial();
int CurrentTrack = StartTrack;
int i,j,pointMin;
int distance[MaxNumber];
for (i = 0;i<TrackNum;i++){
for (j = 0;j<TrackNum;j++){
if (!isVisit[j])
distance[j] = abs(TrackOrder[j]-CurrentTrack);
else
distance[j] = 10000; //表示无穷远,即访问过的磁道就不再访问
}
pointMin = 0;
for (j = 0;j<TrackNum;j++){
if (distance[pointMin] > distance[j])
pointMin = j; //指向最小的位置
}
VisitOrder[i] = TrackOrder[pointMin]; //给访问序列赋值
MoveDistance[i] = abs(TrackOrder[pointMin]-CurrentTrack); //计算每次的移动距离
SumDistance += MoveDistance[i]; //累计移动距离
CurrentTrack = TrackOrder[pointMin]; //改变当前的磁道号
isVisit[pointMin] = true; //将当前的磁道号设置为已访问
}
AverageDistance = SumDistance*1.0/(TrackNum);
output();
}
//扫描,从开始磁道沿选择方向扫描,直到没有要访问的磁道在沿反方向扫描
void SCAN(){
cout<<endl;
cout<<"扫描算法(SCAN)"<<endl;
initial();
int i,j,temp;
int SortTrackOrder[MaxNumber];
for (i = 0;i<TrackNum;i++){//初始化
SortTrackOrder[i] = TrackOrder[i];
}
//冒泡排序
for (i = 0;i<TrackNum;i++){
for (j = i;j<TrackNum;j++){
if (SortTrackOrder[i]>=SortTrackOrder[j]){
temp = SortTrackOrder[i];
SortTrackOrder[i] = SortTrackOrder[j];
SortTrackOrder[j] = temp;
}
}
}
//找到既在当前磁道之外,又是距离最近的磁道号
int point = 0;
while(StartTrack>=SortTrackOrder[point]){
point++;
}
int count = 0;
int currentTrack = StartTrack;
cout<<"向磁道增加的方向访问"<<endl;
for (i = point;i<TrackNum;i++){
VisitOrder[count] = SortTrackOrder[i];
MoveDistance[count] = abs(VisitOrder[count]-currentTrack);
currentTrack = VisitOrder[count];
count++;
}
for (i = point - 1;i>=0;i--){
VisitOrder[count] = SortTrackOrder[i];
MoveDistance[count] = abs(VisitOrder[count]-currentTrack);
currentTrack = VisitOrder[count];
count++;
}
for (i = 0;i<TrackNum;i++){
SumDistance += MoveDistance[i];
}
AverageDistance = (SumDistance*1.0)/TrackNum;
output();
}
//循环扫描,自开始磁道始终沿一个方向扫描,直到没有要访问的磁道再从最里圈或最外圈扫描
void CSCAN(){
initial();
cout<<"循环扫描算法(CSCAN)"<<endl;
int SortTrackOrder[MaxNumber];
int i,j,temp;
for (i = 0;i<TrackNum;i++){
SortTrackOrder[i] = TrackOrder[i];
}
//冒泡排序
for (i = TrackNum - 1;i>0;i--){
for (j = 0;j<i;j++){
if (SortTrackOrder[j]>=SortTrackOrder[j+1]){
temp = SortTrackOrder[j];
SortTrackOrder[j] = SortTrackOrder[j+1];
SortTrackOrder[j+1] = temp;
}
}
}
//找到既在当前磁道之外,又是距离最近的磁道号
int point = 0;
while(StartTrack>=SortTrackOrder[point]){
point++;
}
int count = 0;
int currentTrack = StartTrack;
cout<<"向磁道增加的方向访问"<<endl;
for (i = point;i<TrackNum;i++){
VisitOrder[count] = SortTrackOrder[i];
MoveDistance[count] = abs(VisitOrder[count]-currentTrack);
currentTrack = VisitOrder[count];
count++;
}
for (i =0;i<point;i++){
VisitOrder[count] = SortTrackOrder[i];
MoveDistance[count] = abs(VisitOrder[count]-currentTrack);
currentTrack = VisitOrder[count];
count++;
}
for (i = 0;i<TrackNum;i++){
SumDistance += MoveDistance[i];
}
AverageDistance = (SumDistance*1.0)/TrackNum;
output();
}
void chooseAlgorithm()
{
cout<<"---------------------------------"<<endl;
cout<<"请选择算法:"<<endl;
cout<<"1-先来先服务(FCFS)"<<endl;
cout<<"2-最短寻道时间优先(SSTF)"<<endl;
cout<<"3-扫描算法(SCAN)"<<endl;
cout<<"4-循环扫描算法(CSCAN)"<<endl;
cout<<"0-结束"<<endl;
cout<<"---------------------------------"<<endl;
cin>>choose;
if (choose==1)
{
FCFS();
chooseAlgorithm();
}
else if(choose==2)
{
SSTF();
chooseAlgorithm();
}
else if(choose==3){
SCAN();
chooseAlgorithm();
}
else if(choose==4){
CSCAN();
chooseAlgorithm();
}
else if(choose==0){
exit(0);
}
else
{
cout<<"请输入正确的选择:1-FCFS,2-SSTF,3-SCAN,4-CSCAN,0-退出"<<endl;
chooseAlgorithm();
}
}
实例运行结果截图:
磁道个数:9
磁道序列:55 58 39 18 90 160 150 38 184
当前磁道号(开始磁道):100