磁盘调度算法c语言代码
时间: 2023-09-22 19:11:31 浏览: 11
以下是磁盘调度算法的C语言代码,使用了SCAN算法:
```c
#include<stdio.h>
#include<stdlib.h>
int main()
{
int queue[100], q_size, head, seek = 0, max, diff;
float avg;
printf("%s\t", "Input no of disk locations:");
scanf("%d", &q_size);
printf("%s\t", "Enter head position:");
scanf("%d", &head);
printf("%s\n", "Input elements into disk queue:");
for(int i = 0; i < q_size; i++)
{
scanf("%d", &queue[i]);
}
queue[q_size] = head;
q_size++;
int temp;
for(int i = 0; i < q_size; i++)
{
for(int j = i; j < q_size; j++)
{
if(queue[i] > queue[j])
{
temp = queue[i];
queue[i] = queue[j];
queue[j] = temp;
}
}
}
max = queue[q_size - 1];
int dloc = 0;
for(int i = 0; i < q_size; i++)
{
if(head == queue[i])
{
dloc = i;
break;
}
}
for(int i = dloc; i >= 0; i--)
{
printf("%d --> ", queue[i]);
}
printf("%d --> 0 --> ", max);
for(int i = dloc + 1; i < q_size; i++)
{
printf("%d --> ", queue[i]);
}
seek = head + max;
printf("\nTotal seek time is %d\n", seek);
avg = seek/(float)q_size;
printf("Average seek time is %f\n", avg);
return 0;
}
```
注意:此代码中的磁盘队列中的元素必须按升序排列。
阅读全文