磁盘调度算法比较差异的c语言算法
时间: 2023-09-01 21:08:48 浏览: 82
下面是几种常见的磁盘调度算法的C语言实现,以便比较它们之间的差异。
1. 先来先服务算法(FCFS):
```c
#include <stdio.h>
#include <stdlib.h>
// 磁道总数
#define CYLINDER_NUM 200
// 请求序列长度
#define REQUEST_NUM 8
// 请求序列
int requests[REQUEST_NUM] = { 176, 79, 34, 60, 92, 11, 41, 114 };
int main() {
// 当前磁道号
int cur_pos = 50;
// 总的寻道距离
int total_distance = 0;
// 执行FCFS算法
for (int i = 0; i < REQUEST_NUM; i++) {
int distance = abs(requests[i] - cur_pos);
total_distance += distance;
cur_pos = requests[i];
printf("Service request %d at cylinder %d\n", i, cur_pos);
}
printf("Total distance: %d\n", total_distance);
return 0;
}
```
2. 最短寻找时间优先算法(SSTF):
```c
#include <stdio.h>
#include <stdlib.h>
// 磁道总数
#define CYLINDER_NUM 200
// 请求序列长度
#define REQUEST_NUM 8
// 请求序列
int requests[REQUEST_NUM] = { 176, 79, 34, 60, 92, 11, 41, 114 };
int main() {
// 当前磁道号
int cur_pos = 50;
// 请求序列中下一个要服务的请求的下标
int next_request = 0;
// 总的寻道距离
int total_distance = 0;
// 执行SSTF算法
while (next_request < REQUEST_NUM) {
// 找到离当前磁道最近的请求
int min_distance = CYLINDER_NUM;
int min_index = -1;
for (int i = next_request; i < REQUEST_NUM; i++) {
int distance = abs(requests[i] - cur_pos);
if (distance < min_distance) {
min_distance = distance;
min_index = i;
}
}
// 如果找到了,则服务该请求
if (min_index != -1) {
total_distance += min_distance;
cur_pos = requests[min_index];
printf("Service request %d at cylinder %d\n", min_index, cur_pos);
next_request = min_index + 1;
}
}
printf("Total distance: %d\n", total_distance);
return 0;
}
```
3. 扫描算法(SCAN):
```c
#include <stdio.h>
#include <stdlib.h>
// 磁道总数
#define CYLINDER_NUM 200
// 请求序列长度
#define REQUEST_NUM 8
// 请求序列
int requests[REQUEST_NUM] = { 176, 79, 34, 60, 92, 11, 41, 114 };
// 扫描方向
enum direction { UP, DOWN };
int main() {
// 当前磁道号
int cur_pos = 50;
// 扫描方向
enum direction dir = UP;
// 请求序列中下一个要服务的请求的下标
int next_request = 0;
// 总的寻道距离
int total_distance = 0;
// 执行SCAN算法
while (next_request < REQUEST_NUM) {
// 扫描方向向上
if (dir == UP) {
// 找到下一个离当前磁道最近的请求
int min_distance = CYLINDER_NUM;
int min_index = -1;
for (int i = next_request; i < REQUEST_NUM; i++) {
if (requests[i] >= cur_pos && requests[i] - cur_pos < min_distance) {
min_distance = requests[i] - cur_pos;
min_index = i;
}
}
// 如果找到了,则服务该请求
if (min_index != -1) {
total_distance += min_distance;
cur_pos = requests[min_index];
printf("Service request %d at cylinder %d\n", min_index, cur_pos);
next_request = min_index + 1;
}
// 如果没找到,则改变扫描方向,从CYLINDER_NUM - 1开始向下扫描
else {
dir = DOWN;
}
}
// 扫描方向向下
else {
// 找到下一个离当前磁道最近的请求
int min_distance = CYLINDER_NUM;
int min_index = -1;
for (int i = next_request; i < REQUEST_NUM; i++) {
if (requests[i] <= cur_pos && cur_pos - requests[i] < min_distance) {
min_distance = cur_pos - requests[i];
min_index = i;
}
}
// 如果找到了,则服务该请求
if (min_index != -1) {
total_distance += min_distance;
cur_pos = requests[min_index];
printf("Service request %d at cylinder %d\n", min_index, cur_pos);
next_request = min_index + 1;
}
// 如果没找到,则改变扫描方向,从0开始向上扫描
else {
dir = UP;
}
}
}
printf("Total distance: %d\n", total_distance);
return 0;
}
```
4. 循环扫描算法(C-SCAN):
```c
#include <stdio.h>
#include <stdlib.h>
// 磁道总数
#define CYLINDER_NUM 200
// 请求序列长度
#define REQUEST_NUM 8
// 请求序列
int requests[REQUEST_NUM] = { 176, 79, 34, 60, 92, 11, 41, 114 };
int main() {
// 当前磁道号
int cur_pos = 50;
// 请求序列中下一个要服务的请求的下标
int next_request = 0;
// 总的寻道距离
int total_distance = 0;
// 执行C-SCAN算法
while (next_request < REQUEST_NUM) {
// 找到当前扫描方向下的最远请求
int max_index = -1;
for (int i = next_request; i < REQUEST_NUM; i++) {
if (requests[i] >= cur_pos) {
max_index = i;
break;
}
}
// 如果没找到,则将磁头移到最外侧,重新扫描
if (max_index == -1) {
total_distance += CYLINDER_NUM - 1 - cur_pos;
cur_pos = 0;
continue;
}
// 计算当前扫描方向下的总的寻道距离
int cur_distance = 0;
for (int i = next_request; i <= max_index; i++) {
cur_distance += abs(requests[i] - cur_pos);
cur_pos = requests[i];
printf("Service request %d at cylinder %d\n", i, cur_pos);
}
total_distance += cur_distance;
next_request = max_index + 1;
// 如果请求序列中还有请求,则将磁头移到最外侧,重新扫描
if (next_request < REQUEST_NUM) {
total_distance += CYLINDER_NUM - 1 - cur_pos;
cur_pos = CYLINDER_NUM - 1;
}
}
printf("Total distance: %d\n", total_distance);
return 0;
}
```
以上是几种常见的磁盘调度算法的C语言实现,它们之间的主要差异在于服务请求的顺序和计算寻道距离的方式。
阅读全文