请写一段完整的,有FCFS,SSTF,SCAN,CSCAN算法的磁盘调度程序
时间: 2024-03-25 16:41:05 浏览: 61
[操作系统磁盘调度算法]OS实验C语言代码实现FCFS/SSTF/SCAN/CSCAN
5星 · 资源好评率100%
好的,以下是一段完整的磁盘调度程序,包括FCFS、SSTF、SCAN和CSCAN算法:
```
#include<stdio.h>
#include<math.h>
#include<stdlib.h>
void fcfs(int n, int head, int req[])
{
int seek_time = 0;
printf("\n FCFS Disk Scheduling Algorithm\n");
printf("Seek Sequence is:\n");
for(int i=0; i<n; i++)
{
seek_time += abs(head - req[i]);
head = req[i];
printf("%d ", head);
}
printf("\nTotal Seek Time: %d\n", seek_time);
printf("Average Seek Time: %f\n", (float)seek_time/n);
}
void sstf(int n, int head, int req[])
{
int seek_time = 0;
int completed[n];
for(int i=0; i<n; i++)
{
completed[i] = 0;
}
printf("\n SSTF Disk Scheduling Algorithm\n");
printf("Seek Sequence is:\n");
for(int i=0; i<n; i++)
{
int min_dist = INT_MAX;
int min_index = 0;
for(int j=0; j<n; j++)
{
if(completed[j] == 0)
{
int dist = abs(head - req[j]);
if(dist < min_dist)
{
min_dist = dist;
min_index = j;
}
}
}
completed[min_index] = 1;
seek_time += min_dist;
head = req[min_index];
printf("%d ", head);
}
printf("\nTotal Seek Time: %d\n", seek_time);
printf("Average Seek Time: %f\n", (float)seek_time/n);
}
void scan(int n, int head, int req[], int max)
{
int seek_time = 0;
printf("\n SCAN Disk Scheduling Algorithm\n");
printf("Seek Sequence is:\n");
int direction = 1;
int index;
for(int i=0; i<n; i++)
{
if(direction == 1)
{
index = -1;
int min_dist = INT_MAX;
for(int j=0; j<n; j++)
{
if(req[j] >= head && abs(head - req[j]) < min_dist)
{
min_dist = abs(head - req[j]);
index = j;
}
}
if(index == -1)
{
direction = -1;
printf("%d ", max-1);
continue;
}
}
else if(direction == -1)
{
index = -1;
int min_dist = INT_MAX;
for(int j=0; j<n; j++)
{
if(req[j] <= head && abs(head - req[j]) < min_dist)
{
min_dist = abs(head - req[j]);
index = j;
}
}
if(index == -1)
{
direction = 1;
printf("%d ", 0);
continue;
}
}
seek_time += abs(head - req[index]);
head = req[index];
printf("%d ", head);
}
printf("\nTotal Seek Time: %d\n", seek_time);
printf("Average Seek Time: %f\n", (float)seek_time/n);
}
void cscan(int n, int head, int req[], int max)
{
int seek_time = 0;
printf("\n CSCAN Disk Scheduling Algorithm\n");
printf("Seek Sequence is:\n");
int direction = 1;
int index;
for(int i=0; i<n; i++)
{
if(direction == 1)
{
index = -1;
int min_dist = INT_MAX;
for(int j=0; j<n; j++)
{
if(req[j] >= head && abs(head - req[j]) < min_dist)
{
min_dist = abs(head - req[j]);
index = j;
}
}
if(index == -1)
{
direction = -1;
printf("%d ", max-1);
seek_time += abs(head - (max-1));
head = 0;
continue;
}
}
else if(direction == -1)
{
index = -1;
int min_dist = INT_MAX;
for(int j=0; j<n; j++)
{
if(req[j] <= head && abs(head - req[j]) < min_dist)
{
min_dist = abs(head - req[j]);
index = j;
}
}
if(index == -1)
{
direction = 1;
printf("%d ", 0);
seek_time += abs(head - 0);
head = max-1;
continue;
}
}
seek_time += abs(head - req[index]);
head = req[index];
printf("%d ", head);
}
printf("\nTotal Seek Time: %d\n", seek_time);
printf("Average Seek Time: %f\n", (float)seek_time/n);
}
int main()
{
int n, head, max;
printf("Enter the number of requests: ");
scanf("%d", &n);
int req[n];
printf("Enter the requests: ");
for(int i=0; i<n; i++)
{
scanf("%d", &req[i]);
}
printf("Enter the initial head position: ");
scanf("%d", &head);
printf("Enter the maximum disk size: ");
scanf("%d", &max);
fcfs(n, head, req);
sstf(n, head, req);
scan(n, head, req, max);
cscan(n, head, req, max);
return 0;
}
```
该程序中,我们首先定义了四个函数,分别是FCFS、SSTF、SCAN和CSCAN算法的实现。然后在主函数中,我们先读取用户输入的请求和初始磁头位置,然后调用四个函数来执行不同的磁盘调度算法,并输出结果。
值得注意的是,该程序中使用了C语言的标准库函数,例如`abs()`和`INT_MAX`,需要包含相应的头文件。此外,SCAN和CSCAN算法需要知道磁盘的最大容量,因此我们在主函数中也要读取该值。
希望这段程序能够帮助你理解磁盘调度算法的实现过程。
阅读全文