最短寻道时间优先(SSTF)算法。要求访问的磁道,与当前磁头所在的磁道距离最近,以使每次的寻道时间最短。
扫描调度(SCAN)算法。该算法不仅考虑到欲访问的磁道与当前磁道间的距离,更优先考虑的是磁头当前的移动方向。例如,当磁头正在自里向外移动时,SCAN算法所考虑的下一个访问对象,应是其欲访问的磁道,既在当前磁道之外,又是距离最近的。这样自里向外的访问,直至再无更外的磁道需要访问时,才将磁道换向自外向里移动。
#include<iostream>
#include<ctime>
using namespace std;
void SSTF(int a[],int n);
void CSCAN(int a[],int n);
int main()
{
int n; //磁道的个数
int s; //功能号
cout<<"请输入当前磁道的个数,按Enter键显示生成的随机磁道号:"<<endl;
cin>>n;
int *a=new int[n];//申请一个整型变量空间,赋初值为n,并定义一个整型指针a指向该地址空间
cout<<"生成的随机磁道号为:";
srand((unsigned)time(NULL));
for(int i=0;i<n;i++)
{
a[i]=(rand()%100)+1;
cout<<a[i]<<" ";
}
cout<<endl;
while(1)
{
cout<<endl;
cout<<" ┏━━━━━━━━━━━━━━━━━━━━━━━┓"<<endl;
cout<<" ┃ 磁盘调度算法功能列表 ┃"<<endl;
cout<<" ┠───────────────────────┨"<<endl;
cout<<" ┃ 1、最短寻道时间算法(SSTF) ┃"<<endl;
cout<<" ┠───────────────────────┨"<<endl;
cout<<" ┃ 2、循环扫描算法(CSCAN) ┃"<<endl;
cout<<" ┠───────────────────────┨"<<endl;
cout<<" ┃ 0、退出 ┃"<<endl;
cout<<" ┗━━━━━━━━━━━━━━━━━━━━━━━┛"<<endl;
cout<<endl;
cout<<"请选择所需功能的前置编号:";
cin>>s;
if(s>3)
{
cout<<"数据输入有误!请重新输入:"<<endl;
}
else
{
switch(s){
case 0: exit(0);break ;
case 1:SSTF(a, n);break;
case 2:CSCAN(a,n);break;}
}
}
return 0;
}
//最短寻道时间算法(SSTF)
void SSTF(int a[],int n)
{
int temp;
int k=1;
int now,l,r;
int i,j;
double sum=0;
//将磁道号按递增排序
for(i=0;i<n;i++)
for(j=i+1;j<n;j++){
if(a[i]>a[j]){
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
cout<<"按递增顺序排好的磁道显示为:"<<endl;
for(i=0;i<n;i++)
{
cout<<a[i]<<" ";//输出排好的磁道顺序
}
cout<<endl;
cout<<"请输入当前的磁道号:";
cin>>now;//确定当前磁头所在位置
cout<<"磁盘调度顺序为:"<<endl;
if(a[n-1]<=now){//当前磁头位置大于最外围欲访问磁道(比如21,90,当前的磁道为91,顺序为91,1)
for(i=n-1;i>=0;i--)
cout<<a[i]<<" ";
sum=now-a[0];
}
else
if(a[0]>=now)//当前磁头位置小于最里欲访问磁道(比如21,90,当前的磁道为91,顺序为21,91)
{
for(i=0;i<n;i++)
cout<<a[i]<<" ";
sum=a[n-1]-now;
}
else//不大也不小,在范围之内
{
while(a[k]<now)//确定当前磁道在已排的序列中的位置
{
k++;
}
l=k-1;//在磁头位置的前一个欲访问磁道
r=k;//磁头欲访问磁道
while((l>=0)&&(r<n))
{
if((now-a[l])<=(a[r]-now))//选择离磁头近的磁道
{
cout<<a[l]<<" ";//(比如37,44,49,64,84,93,当前的磁道为65,则调度后的循序为:64(为a[l]),49,44,37,84 93)
sum+=now-a[l];
now=a[l];
l=l-1;
}
else
{
cout<<a[r]<<" ";//(比如22 26 62 67 88 而当前的磁道为87,则磁盘调度的顺序为88 67 62 23 22 )
sum+=a[r]-now;
now=a[r];
r=r+1;
}
}
if(l=-1)//磁头位置里侧的磁道已访问完,这是针对(now-a[l]<a[r]-now的情况)
{
for(j=r;j<n;j++)//访问磁头位置外侧的磁道
{
cout<<a[j]<<" ";
}
sum+=a[n-1]-a[0];
}
if(r==n)//磁头位置外侧的磁道已访问完,这是针对(else的情况)
{
for(j=k-1;j>-1;j--) //访问磁头位置里侧的磁道
{
cout<<a[j]<<" ";
}
sum+=a[n-1]-a[0];
}
}
cout<<endl;
cout<<"移动的总道数为:"<<sum<<endl;
cout<<"移动的平均道数为:"<<(sum/n)<<endl;
}
//循环扫描算法(CSCAN)
void CSCAN(int a[],int n)
{
int temp;
int now,l,r;
int i,j;
double sum=0;
int k=1;
for(i=0;i<n;i++)//对访问磁道按由小到大顺序排列输出
for(j=i+1;j<n;j++){
if(a[i]>a[j])
{
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
cout<<"按递增顺序排好的磁道为:"<<endl;
for( i=0;i<n;i++)
{
cout<<a[i]<<" ";
}
cout<<endl;
cout<<"请输入当前的磁道号:";
cin>>now;//确定当前磁道号
if(a[n-1]<=now)//磁头位置大于最外围欲访问磁道
{
for(i=0;i<n;i++)
cout<<a[i]<<" ";
sum=now-2*a[0]+a[n-1];
}
else
if(a[0]>=now)//磁头位置小于最里欲访问磁道
{
for(i=0;i<n;i++)
cout<<a[i]<<" ";
sum=a[n-1]-now;
}
else //磁头位置在最里侧磁道与最外侧磁道之间
{ int d;
while(a[k]<now)
{
k++;
}
l=k-1;//在磁头位置的前一个欲访问磁道
r=k; //磁头欲访问磁道
cout<<"请输入当前磁头移动的方向 (0 表示向内 ,1表示向外) : ";
cin>>d; //确定磁头访问的方向
cout<<"磁盘调度顺序为:";
if(d==0||d==1)
{
if(d==1) //磁头向外侧访问
{
for(j=r;j<n;j++)//先访问外侧磁道再转向最里欲访问磁道
{
cout<<a[j]<<" ";
}
for(j=0;j<r;j++)
{
cout<<a[j]<<" ";
}
sum=2*a[n-1]-now-2*a[0]+a[l];
}
if(d==0) //磁头向内侧访问
{
for(j=r-1;j>=0;j--)
{
cout<<a[j]<<" ";
}
for(j=n-1;j>=r;j--)
{
cout<<a[j]<<" ";
}
sum=2*a[n-1]-2*a[0]+now-a[r];
}
}
else
cout<<"请输入0或1!";
}
cout<<endl;
cout<<"移动的总道数为:"<<sum<<endl;
cout<<"移动的平均道数为:"<<(sum/n)<<endl;
}