# 磁盘寻道算法FCFS与SSTF

#include <stdio.h>
#include <stdlib.h>
int tracks[200];//track number

void Init(int start,int num);//initialize track sequence
void FCFS(int start,int num);//First come first serve algorithm
void SSTF(int start,int num);//Shortest seek time first algorithm

void Init(int start,int num)
{
int i;
printf("----------Input Track Number---------\n");
for(i=0;i<num;i++)
{
printf("Please enter track number %d : ",i+1);
scanf("%d",&tracks[i]);
}

}
void FCFS(int start,int num)
{
double sum=0;
int temp,i;
printf("==========================\n");
printf("Starting from track number %d\n",start);
printf("Next track number:\t seek length:\n");
for(i=0;i<num;i++)
{
temp=abs(start-tracks[i]);
printf("%d\t\t\t %d\n",tracks[i],temp);
sum+=temp;
start=tracks[i];
}
printf("Average seek length:%.3lf\n",sum/num);
printf("==========================\n");
}
void SSTF(int start,int num)
{
int i,j,temp,min,mint;
bool table[100];
double sum=0;
for(i=0;i<num;i++)
{
table[i]=0;
}
printf("==========================\n");
printf("Starting from track number %d\n",start);
printf("Next track number:\t seek length:\n");
for(i=0;i<num;i++)
{
min=-1;
for(j=0;j<num;j++)
{
if(!table[j])
{
temp=abs(start-tracks[j]);
if(min==-1||min>temp)
{
min=temp;
mint=j;
}
}
}
printf("%d\t\t\t %d\n",tracks[mint],min);
start=tracks[mint];
table[mint]=1;
sum+=min;
}
printf("Average seek length:%.3lf\n",sum/num);
printf("==========================\n");
}

int main()
{
int start;//Initial track number
int num;//visit times
int choose;
printf("\t\t\t Disk Scheduling Algorithms\n");

do
{
printf("Please enter the initial track number(0~200):");
scanf("%d",&start);
}while(start<0||start>200);
do
{
printf("Please enter the number of disk accesses(1~10):");
scanf("%d",&num);
}while(num<1||num>10);

Init(start,num);
do
{
do
{
printf("1.FCFS\n");
printf("2.SSTF\n");
printf("3.exit\n");
scanf("%d",&choose);
}while(choose<1||choose>5);
switch(choose)
{
case 1:
FCFS(start,num);
break;
case 2:
SSTF(start,num);
break;
case 3: exit(0);
}
}while(choose!=5);
return 0;
}


• 0
点赞
• 0
评论
• 0
收藏
• 一键三连
• 扫一扫，分享海报

12-12 6008

08-20 1万+
04-09 4393
12-21 1282
11-20 1221
12-13 2644
11-30
06-21 1万+
09-26 1万+
06-25 381