c语言实现磁盘调度算法
时间: 2023-08-17 17:25:26 浏览: 54
磁盘调度算法是用于优化磁盘访问的顺序,常见的算法包括先来先服务(FCFS)、最短寻道时间优先(SSTF)、扫描算法(SCAN)、循环扫描算法(C-SCAN)等。
以下是使用C语言实现SSTF算法的示例代码:
```c
#include <stdio.h>
#include <stdlib.h>
#include <limits.h> //定义了数据类型的极限值
int main() {
int queue[1000], head, n, i, j, seek = 0, diff;
float avg_seek_time;
printf("Enter the number of requests: ");
scanf("%d", &n);
printf("Enter the queue of requests: ");
for (i = 1; i <= n; i++) {
scanf("%d", &queue[i]);
}
printf("Enter the initial head position: ");
scanf("%d", &head);
queue[0] = head;
printf("\n");
for (j = 0; j <= n - 1; j++) {
diff = abs(queue[j + 1] - queue[j]);
seek += diff;
printf("Disk head moves from %d to %d with Seek %d\n", queue[j], queue[j + 1], diff);
}
printf("\nTotal Seek Time: %d\n", seek);
avg_seek_time = seek / (float) n;
printf("Average Seek Time: %f\n", avg_seek_time);
return 0;
}
```
运行程序后,先输入请求队列的长度,再输入请求队列,最后输入磁头的初始位置。程序会输出磁头移动的顺序以及总寻道时间和平均寻道时间。