掌握文件的存取方法;掌握文件的逻辑结构和物理结构;掌握存储空间的分配和回收;掌握磁盘管理与调度。
用程序模拟磁盘的调度过程,并计算各磁盘调度算法包括先来先服务算法、最短寻道时间优先算法、扫描算法和循环扫描算法的平均寻道长度。
本实验是模拟操作系统的磁盘寻道方式,运用磁盘访问顺序的不同来设计磁盘的调度算法。实现的磁盘调度算法有FCFS,SSTF,SCAN,CSCAN和 NStepSCAN算法。设定开始磁道号寻道范围,依据起始扫描磁道号和最大磁道号数,随机产生要进行寻道的磁道号序列。选择磁盘调度算法,显示该算法的磁道访问顺序,计算出移动的磁道总数和平均寻道总数。按算法的寻道效率进行排序,并对各算法的性能进行分析比较。
函数名称 | 功能描述 |
---|---|
void FCFS(int b[],int n,int init) | 先来先服务算法(FCFS) |
void SSTF(int b[],int n,int k) | 最短寻道时间优先算法(SSTF) |
void SCAN1(int b[],int n,int k) | 扫描算法(SCAN)由外向里 |
void SCAN2(int b[],int n,int k) | 扫描算法(SCAN)由里向外 |
void FCFS(int b[],int n,int init) | 循环算法(C—SCAN) |
int main() | main函数 |
#include
#include
#define SYSINFO "文件管理" // 系统信息
#define AUTHOR "孤灯" //作者
void FCFS(int b[],int n,int init)
{
int i = 0;
int s = 0;
int sum = 0;
int a[20];
for(i = 0; i < n; i++)
{
a[i] = b[i];
}
s = init;
sum = 0;
for(i = 0; i < n; i++)
{
printf("第%d次访问的磁道:%d\n", i+1, a[i]);
sum += abs(s - a[i]);
printf("sum=%d\n", sum);
s = a[i];
}
printf("平均寻道长度:%f\n", sum * 1.0 / n);
}
void SSTF(int b[],int n,int k)
{
int i = 0;
int j = 0;
int s = 0;
int sum = 0;
int p = 0;
int a[20];
for(i = 0; i < n; i++)
{
a[i] = b[i];
}
for(i = n-1; i >= 0; i--)
{
s = a[0];
p = 0;
for(j = 0; j <= i; j++)
if(abs(a[j] - k) < abs(s - k))
{
s = a[j];
p = j;
}
a[p] = a[i];
printf("第%d次访问的磁道:%d\n", n-i, s);
sum += abs(s - k);
printf("sum=%d\n", sum);
k = s;
}
printf("平均寻道长度:%f\n",sum * 1.0 / n);
}
void SCAN1(int b[],int n,int k)
{
int i = 0;
int j = 0;
int s = 0;
int sum=0;
int p = 0;
int biaoji = 0;
int a[20];
for(i=0; i<n; i++)
{
a[i] = b[i];
}
for(i = n-1; i >= 0; i--)
{
biaoji = 0;
for(j = 0; j <= i; j++)
{
if(a[j] - k <= 0)
{
biaoji=1;
p=j;
break;
}
}
if(biaoji==1)
{
s=a[p];
for(j=0; j<=i; j++)
if(a[j]<k&&k-a[j]<k-s)
{
s=a[j];
p=j;
}
a[p]=a[i];
printf("第%d次访问的磁道:%d\n",n-i,s);
sum+=k-s;
printf("sum=%d\n",sum);
k=s;
}
else
{
s=a[0];
for(j=0; j<=i; j++)
if(a[j]-k<=s-k)
{
s=a[j];
p=j;
}
a[p] = a[i];
printf("第%d次访问的磁道:%d\n",n-i,s);
sum += abs(k-s);
printf("sum=%d\n",sum);
k = s;
}
}
printf("平均寻道长度:%f\n",sum * 1.0 / n);
}
void SCAN2(int b[],int n,int k)
{
int i = 0;
int j = 0;
int s = 0;
int sum=0;
int p = 0;
int biaoji = 0;
int a[20];
for(i = 0; i < n; i++)
{
a[i] = b[i];
}
for(i = n - 1; i >= 0; i--)
{
biaoji = 0;
for(j = 0; j <= i; j++)
{
if(a[j] - k >= 0)
{
biaoji = 1;
p = j;
break;
}
}
if(biaoji == 1)
{
s = a[p];
for(j = 0; j <= i; j++)
if(a[j]>k&&a[j]-k<s-k)
{
s=a[j];
p=j;
}
a[p]=a[i];
printf("第%d次访问的磁道:%d\n",n-i,s);
sum += s-k;
printf("sum=%d\n", sum);
k = s;
}
else
{
s = a[0];
for(j = 0; j <= i; j ++)
{
if(k - a[j] <= k - s)
{
s = a[j];
p = j;
}
}
a[p] = a[i];
printf("第%d次访问的磁道:%d\n", n - i, s);
sum += abs(k - s);
printf("sum=%d\n",sum);
k = s;
}
}
printf("平均寻道长度:%f\n",sum * 1.0 / n);
}
void C_SCAN(int b[],int n,int k)
{
int i = 0;
int j = 0;
int s = 0;
int sum=0;
int p = 0;
int biaoji = 0;
int a[20];
for(i = 0; i < n; i++)
{
a[i] = b[i];
}
for(i = n-1; i >= 0; i--)
{
biaoji=0;
for(j = 0; j <= i; j++)
{
if(a[j]-k <= 0)
{
biaoji=1;
p=j;
break;
}
}
if(biaoji == 1)
{
s = a[p];
for(j = 0; j <= i; j++)
{
if(a[j] < k && a[j] - k > s - k)
{
s = a[j];
p = j;
}
}
a[p] = a[i];
printf("第%d次访问的磁道:%d\n", n-i, s);
sum += s - k;
printf("sum=%d\n", sum);
k = s;
}
if(biaoji==0)
break;
}
s = a[0];
for(j = 0; j <= i; j++)
{
if(a[j] >= s)
{
s = a[j];
p = j;
}
}
a[p] = a[i];
printf("第%d次访问的磁道:%d\n", n - i, s);
sum += k - s;
printf("sum=%d\n", sum);
k = s;
i --;
for(i; i >= 0; i --)
{
s = a[0];
for(j = 0; j <= i; j ++)
{
if(a[j] - k >= s - k)
{
s = a[j];
p = j;
}
}
a[p] = a[i];
printf("第%d次访问的磁道:%d\n", n-i, s);
sum += s - k;
printf("sum=%d\n", sum);
k=s;
}
printf("平均寻道长度:%f\n", sum * 1.0 / n);
}
int main()
{
int a[20] = {0};
int i = 0;
int n = 0;
int k = 0;
int k1 = 0;
int init = 0;
printf("请输入需要访问的磁道总数:");
scanf("%d", &n);
printf("要输入的访问序列要有重复值和距离相等的值,例如:初始值为5,序列中必须含有5,4,6,这样的序列。\n");
for(i = 0; i < n; i++)
{
printf("需要访问的磁道%d:",i+1);
scanf("%d",&a[i]);
}
printf("请输入指针所在磁道:");
scanf("%d",&init);
k = 1;
while(k)
{
printf("**********************************\n");
printf("%s\nDesigned By:%s\n", SYSINFO, AUTHOR);
printf("**1.先来先服务(FCFS)**\n");
printf("**2.最短寻道时间优先(SSTF)**\n");
printf("**3.扫描算法(SCAN)**\n");
printf("**4.循环算法(C-SCAN)**\n");
printf("**0.退出**\n");
printf("**********************************\n");
printf("请在下面输入您的选择:");
scanf("%d", &k);
switch(k)
{
case 1:
FCFS(a, n, init);
break;
case 2:
SSTF(a, n, init);
break;
case 3:
k1=1;
while(k1)
{
printf("**1.移动臂由里向外**\n");
printf("**2.移动臂由外向里**\n");
printf("**0.返回上一层**\n");
printf("**********************************\n");
printf("请在下面输入您的选择:");
scanf("%d", &k1);
switch(k1)
{
case 1:
SCAN1(a, n, init);
break;
case 2:
SCAN2(a, n, init);
break;
}
}
case 4:
C_SCAN(a, n, init);
break;
}
}
return 0;
}
体会与总结: