我正在尝试在Linux命令行中实现Shortest Seek Time First(SSTF)算法。命令行是从以下行读取轨道值:
./disksed 53 98 183 37 122 14 124 65 67
并打印以下报告:
SSTF: Start:53 65:12 67:2 37:30 14:23 98:84 122:24 124:2 183:59 Total:236
我是否在正确的轨道上?我没有得到正确的值,我不知道如何继续。以下是我的代码。任何帮助将不胜感激。
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char **argv)
{
const int ARRAY_SIZE = 9;
int sstd[ARRAY_SIZE];
int total2 = 0;
int temp;
printf("\nSSTF: Start:");
printf("%s", argv[1]);
printf(" ");
for (int i = 1; i < 9; i++)
{
if (strtol(argv[i + 1], NULL, 10) - strtol(argv[i], NULL, 10) < 0)
total2 += (abs(strtol(argv[i + 1], NULL, 10) - strtol(argv[i], NULL, 10)));
else
total2 += (strtol(argv[i + 1], NULL, 10) - strtol(argv[i], NULL, 10));
temp = strtol(argv[i + 1], NULL, 10);
argv[i + 1] = argv[i + 2];
argv[i + 2] = (char)temp;
sstd[i] = strtol(argv[i + 1], NULL, 10);
printf("%d", sstd[i]);
printf(":");
//Print difference?
printf(" ");
}
printf("Total:");
printf("%d", total2);
printf("\n");
return 0;
}