磁盘调度算法(先来先服务,最短寻道时间优先,基于扫描的磁盘调度算法,最少完成两种算法的设计,其中扫描算法必须选择一种,其他算法任选一种,并计算平均寻道长度。)用c语言编译
时间: 2023-07-16 20:17:07 浏览: 119
以下是使用 C 语言编写的磁盘调度算法示例代码:
```c
#include <stdio.h>
#include <stdlib.h>
#define MAX_REQUESTS 100
/* 磁盘请求结构体 */
struct request {
int track;
int visited;
};
/* 磁盘请求队列结构体 */
struct request_queue {
int count;
struct request requests[MAX_REQUESTS];
};
/* 先来先服务调度算法 */
void fcfs(struct request_queue *queue, int start, int *total_seek_time) {
int i;
int current_track = start;
for (i = 0; i < queue->count; i++) {
int seek_time = abs(current_track - queue->requests[i].track);
*total_seek_time += seek_time;
current_track = queue->requests[i].track;
}
}
/* 最短寻道时间优先调度算法 */
void sstf(struct request_queue *queue, int start, int *total_seek_time) {
int i, j;
int current_track = start;
for (i = 0; i < queue->count; i++) {
int min_seek_time = 9999;
int min_index = -1;
for (j = 0; j < queue->count; j++) {
if (!queue->requests[j].visited) {
int seek_time = abs(current_track - queue->requests[j].track);
if (seek_time < min_seek_time) {
min_seek_time = seek_time;
min_index = j;
}
}
}
*total_seek_time += min_seek_time;
current_track = queue->requests[min_index].track;
queue->requests[min_index].visited = 1;
}
}
/* 基于扫描的调度算法 */
void scan(struct request_queue *queue, int start, int *total_seek_time) {
int i, j;
int current_track = start;
int direction = 1;
int max_track = 199;
for (i = 0; i < queue->count; i++) {
if (current_track == max_track) {
direction = -1;
} else if (current_track == 0) {
direction = 1;
}
int min_seek_time = 9999;
int min_index = -1;
for (j = 0; j < queue->count; j++) {
if (!queue->requests[j].visited) {
int seek_time = abs(current_track - queue->requests[j].track);
if (seek_time < min_seek_time && (direction == 1 ? queue->requests[j].track >= current_track : queue->requests[j].track <= current_track)) {
min_seek_time = seek_time;
min_index = j;
}
}
}
if (min_index == -1) {
direction = -direction;
continue;
}
*total_seek_time += min_seek_time;
current_track = queue->requests[min_index].track;
queue->requests[min_index].visited = 1;
}
}
int main() {
int i;
int total_seek_time = 0;
struct request_queue queue = {0};
int start_track;
printf("请输入起始磁道号:");
scanf("%d", &start_track);
printf("请输入磁盘请求数量(不超过 %d):", MAX_REQUESTS);
scanf("%d", &queue.count);
printf("请输入磁盘请求序列(磁道号以空格分隔):\n");
for (i = 0; i < queue.count; i++) {
scanf("%d", &queue.requests[i].track);
queue.requests[i].visited = 0;
}
printf("\n先来先服务调度算法:\n");
fcfs(&queue, start_track, &total_seek_time);
printf("平均寻道长度:%f\n", (double)total_seek_time / queue.count);
total_seek_time = 0;
for (i = 0; i < queue.count; i++) {
queue.requests[i].visited = 0;
}
printf("\n最短寻道时间优先调度算法:\n");
sstf(&queue, start_track, &total_seek_time);
printf("平均寻道长度:%f\n", (double)total_seek_time / queue.count);
total_seek_time = 0;
for (i = 0; i < queue.count; i++) {
queue.requests[i].visited = 0;
}
printf("\n基于扫描的调度算法:\n");
scan(&queue, start_track, &total_seek_time);
printf("平均寻道长度:%f\n", (double)total_seek_time / queue.count);
return 0;
}
```
在该示例代码中,我们定义了三种磁盘调度算法:先来先服务、最短寻道时间优先和基于扫描的调度算法。其中,先来先服务和最短寻道时间优先算法比较简单,直接按照请求的顺序或者距离当前磁道最近的顺序进行处理即可。而基于扫描的调度算法需要考虑磁头移动的方向和限制,需要在当前磁头位置的前后扫描请求,选择距离最近的请求进行处理。
在主函数中,我们通过读取用户输入的磁道请求序列和起始位置,调用三种算法进行处理,并输出平均寻道长度。
阅读全文