SSTF算法或SCAN算法.docx
SSTF算法或SCAN算法SSTF算法或SCAN算法.txt心是自己的,干嘛总被别人伤.没有伞的孩子必须努力奔跑敷衍旳青春 总昰想太多 怨,只怨现实太现实为什么在一起要两个人的同意丶而分手只需要一个人 #include<stdio.h> #include<math.h> #define MAXSIZE 100 #include<stdlib.h> #include<time.h> typedef struct node int num;/磁盘号 int flag;/用于标记是否已经服务过 Disk; /Disk seqMAXSIZE 55,0,58,0,39,0,18,0,90,0,160,0,150,0,38,0,184,0; Disk seqMAXSIZE; int size = 9,currentNum = 100; int sum1 = 0; int sum2 = 0; void Init(int size,Disk seqMAXSIZE)/磁道号的个数 int i; srand(time(NULL); for(i = 0; i<size; i+) seqi.num = rand%199;/产生0到199之间的数字 / scanf("%d",&seqi.num); /seqi.num = i*4 ; seqi.flag = 0; void FCFS(int size,Disk seqMAXSIZE)/此参数为请求的入口 int i; for(i = 0; i<size; i+) printf("访问的序列号%dn",seqi.num); int FindShortest(int size,int currentNum,Disk seqMAXSIZE) int i,pos1,pos2,min,flag = 0; for(i = 0; i<size; i+) = if(seqi.flag = 0 && seqi.num != currentNum) min = abs(seqi.num - currentNum); pos1 = i; break; for(i = 0; i<size; i+) if(!seqi.flag && min > abs(seqi.num - currentNum) flag = 1; min = abs(seqi.num - currentNum); pos2 = i; sum1 += min; if(flag) seqpos2.flag = 1; return pos2; else seqpos1.flag = 1; return pos1; int SSTF(int size,int start,Disk seqMAXSIZE)/start为开始序列号 int i,pos;/pos为最短那个序列号的位置 /pos = FindShortest(size); printf("访问的序列号:n"); for(i = 0; i<size; i+) pos = FindShortest(size,currentNum,seq); /printf("%d: ",currentNum); /sum += currentNum; currentNum = seqpos.num; printf("%d ",seqpos.num); return 1; int flagDirection(int size,int currentNum,Disk seqMAXSIZE)/此函数用于判断磁头的移动方向 int pos,i,flag = 0; for(i = 0; i<size; i+) if(seqi.num > currentNum && seqi.flag = 0) flag = 1; pos = i; break; return flag; int FindShortest2(int size,int currentNum,int status,Disk seqMAXSIZE)/status 1(由内像外)0 int i,pos1,pos2,min,flag = 0; for(i = 0; i<size; i+) if(status = 1) if(seqi.flag = 0 && seqi.num != currentNum && seqi.num > currentNum) min = abs(seqi.num - currentNum); pos1 = i; break; else if(seqi.flag = 0 && seqi.num != currentNum && seqi.num < currentNum) min = abs(seqi.num - currentNum); pos1 = i; break; for(i = 0; i<size; i+) if(status = 1) if(!seqi.flag && min > abs(seqi.num - currentNum) && seqi.num > currentNum) flag = 1; min = abs(seqi.num - currentNum); pos2 = i; else if(!seqi.flag && min > abs(seqi.num - currentNum) && seqi.num < currentNum) flag = 1; min = abs(seqi.num - currentNum); pos2 = i; /printf("%d ",min); sum2 += min; /printf("n"); /printf("%d ",sum2); if(flag) seqpos2.flag = 1; return pos2; else seqpos1.flag = 1; return pos1; void SCAN(int size,int currentNum,Disk seqMAXSIZE) int status,pos; int i; for(i = 0; i<size; i+) status = flagDirection(size,currentNum,seq); pos = FindShortest2(size,currentNum,status,seq); currentNum = seqpos.num; printf("%d ",currentNum); int main Disk seqMAXSIZE; int i; Init(size,seq); for(i = 0; i<size; i+) printf("%d ",seqi.num); printf("n"); /FCFS(10); SSTF(size,currentNum,seq); printf("平均寻道长度%.1fn",(float)(sum1/9.0); printf("n"); /printf("%dn",sum1); for(i = 0; i<size;i+) seqi.flag = 0; SCAN(size,currentNum,seq); /printf("%d n",sum2); printf("平均寻道长度%.1fn",(float)(sum2/9.0); return 0;