磁盘调度算法C语言代码
时间: 2023-07-05 07:18:09 浏览: 99
磁盘调度算法(c语言)
5星 · 资源好评率100%
这是一个实现磁盘调度算法SCAN的C语言代码:
```c
#include <stdio.h>
int main()
{
int queue[20], n, head, i, j, k, seek = 0, max, diff;
float avg;
printf("Enter the size of queue: ");
scanf("%d", &n);
printf("Enter the queue: ");
for (i = 1; i <= n; i++)
scanf("%d", &queue[i]);
printf("Enter the initial head position: ");
scanf("%d", &head);
queue[0] = head;
n++;
for (i = 0; i < n; i++)
{
max = 5000;
for (j = i; j < n; j++)
{
if (queue[j] >= queue[i] && (queue[j] - queue[i]) < max)
{
max = queue[j] - queue[i];
k = j;
}
if (queue[j] <= queue[i] && (queue[i] - queue[j]) < max)
{
max = queue[i] - queue[j];
k = j;
}
}
diff = abs(queue[i] - queue[k]);
seek += diff;
head = queue[k];
for (j = k; j < n; j++)
queue[j] = queue[j + 1];
n--;
i--;
}
avg = seek / (float)(n - 1);
printf("\nTotal seek time: %d", seek);
printf("\nAverage seek time: %f", avg);
return 0;
}
```
这个代码实现了一个简单的SCAN磁盘调度算法。在这个代码中,首先输入磁盘请求队列,然后输入初始磁头位置。接下来,代码通过SCAN算法计算出磁头移动的顺序,并计算出总的寻道时间和平均寻道时间。
阅读全文