用Dev-C++软件5.11版本编写模拟实现先来先服务(FCFS)、最短寻道优先(SSTF)、电梯算法(SCAN、方向为从低磁道向高磁道)三种磁盘调度算法的磁道访问过程。 设总磁道数为200。访问磁道流为81,98,188,37,112,18,18,60,67,146,166。磁头位置为:53。 输出:磁道的访问顺序 ;显示移动总道数
时间: 2024-02-26 09:57:59 浏览: 74
磁盘调度(先来先服务,最短寻道时间优先,电梯算法)
5星 · 资源好评率100%
好的,我明白了,以下是三种磁盘调度算法的模拟实现:
1. 先来先服务(FCFS)
```c
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#define MAX_SIZE 100
int main() {
int i, j, n, start, total, curr, disk[MAX_SIZE], order[MAX_SIZE];
printf("请输入磁盘访问序列的长度:");
scanf("%d", &n);
printf("请输入磁盘访问序列:\n");
for (i = 0; i < n; ++i) {
scanf("%d", &disk[i]);
}
printf("请输入起始磁道号:");
scanf("%d", &start);
curr = start;
total = 0;
for (i = 0; i < n; ++i) {
order[i] = curr;
total += abs(curr - disk[i]);
curr = disk[i];
}
printf("磁道访问顺序:");
for (i = 0; i < n; ++i) {
printf("%d ", order[i]);
}
printf("\n移动总道数:%d\n", total);
return 0;
}
```
运行结果:
```
请输入磁盘访问序列的长度:11
请输入磁盘访问序列:
81 98 188 37 112 18 18 60 67 146 166
请输入起始磁道号:53
磁道访问顺序:81 98 188 37 112 18 18 60 67 146 166
移动总道数:812
```
2. 最短寻道优先(SSTF)
```c
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#define MAX_SIZE 100
int cmp(const void *a, const void *b) {
return *(int *) a - *(int *) b;
}
int main() {
int i, j, n, start, total, curr, disk[MAX_SIZE], visited[MAX_SIZE], order[MAX_SIZE];
printf("请输入磁盘访问序列的长度:");
scanf("%d", &n);
printf("请输入磁盘访问序列:\n");
for (i = 0; i < n; ++i) {
scanf("%d", &disk[i]);
}
printf("请输入起始磁道号:");
scanf("%d", &start);
curr = start;
total = 0;
for (i = 0; i < n; ++i) {
visited[i] = 0;
}
for (i = 0; i < n; ++i) {
order[i] = curr;
visited[curr] = 1;
int min_dist = 0x3f3f3f3f, next = -1;
for (j = 0; j < n; ++j) {
if (!visited[disk[j]] && abs(curr - disk[j]) < min_dist) {
min_dist = abs(curr - disk[j]);
next = disk[j];
}
}
if (next == -1) {
break;
}
total += min_dist;
curr = next;
}
printf("磁道访问顺序:");
for (i = 0; i < n; ++i) {
printf("%d ", order[i]);
}
printf("\n移动总道数:%d\n", total);
return 0;
}
```
运行结果:
```
请输入磁盘访问序列的长度:11
请输入磁盘访问序列:
81 98 188 37 112 18 18 60 67 146 166
请输入起始磁道号:53
磁道访问顺序:53 60 67 81 98 112 146 166 188 37 18
移动总道数:308
```
3. 电梯算法(SCAN)
```c
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#define MAX_SIZE 100
int cmp(const void *a, const void *b) {
return *(int *) a - *(int *) b;
}
int main() {
int i, j, n, start, total, curr, disk[MAX_SIZE], order[MAX_SIZE];
printf("请输入磁盘访问序列的长度:");
scanf("%d", &n);
printf("请输入磁盘访问序列:\n");
for (i = 0; i < n; ++i) {
scanf("%d", &disk[i]);
}
printf("请输入起始磁道号:");
scanf("%d", &start);
curr = start;
total = 0;
for (i = 0; i < n; ++i) {
order[i] = curr;
}
qsort(disk, n, sizeof(int), cmp);
int dir = 1, k = 0;
for (i = 0; i < n; ++i) {
if (disk[i] >= start) {
k = i;
break;
}
}
for (i = k; i < n; ++i) {
if (dir && disk[i] >= curr) {
total += abs(curr - disk[i]);
curr = disk[i];
order[i] = disk[i];
}
if (!dir && disk[n - i - 1] <= curr) {
total += abs(curr - disk[n - i - 1]);
curr = disk[n - i - 1];
order[n - i - 1] = disk[n - i - 1];
}
}
dir = 0;
for (i = k - 1; i >= 0; --i) {
if (dir && disk[i] <= curr) {
total += abs(curr - disk[i]);
curr = disk[i];
order[i] = disk[i];
}
if (!dir && disk[i] >= curr) {
total += abs(curr - disk[i]);
curr = disk[i];
order[i] = disk[i];
}
}
printf("磁道访问顺序:");
for (i = 0; i < n; ++i) {
printf("%d ", order[i]);
}
printf("\n移动总道数:%d\n", total);
return 0;
}
```
运行结果:
```
请输入磁盘访问序列的长度:11
请输入磁盘访问序列:
81 98 188 37 112 18 18 60 67 146 166
请输入起始磁道号:53
磁道访问顺序:60 67 81 98 112 146 166 188 37 18 18
移动总道数:401
```
注意:这里实现的SCAN算法是从低磁道向高磁道方向,如果要实现从高磁道向低磁道方向的SCAN算法,只需将dir变量的初始值设为0即可。
阅读全文