使用C语言写出磁盘调度算法(先来先服务,最短寻道时间优先,基于扫描的磁盘调度算法,最少完成两种算法的设计,其中扫描算法必须选择一种,其他算法任选一种,并计算平均寻道长度。)
时间: 2023-08-08 17:05:45 浏览: 150
好的,我来为您介绍一下这三种磁盘调度算法的实现。
1. 先来先服务(FCFS)算法
FCFS算法就是按照磁盘请求的顺序进行先后服务,也就是先服务先完成。具体实现如下:
```c
#include <stdio.h>
#include <stdlib.h>
#define MAX 1000
int queue[MAX];
int fcfs(int *queue, int n, int start) {
int sum = 0;
for (int i = 0; i < n; i++) {
sum += abs(queue[i] - start);
start = queue[i];
}
return sum;
}
int main() {
int n, start;
printf("请输入磁盘请求数:");
scanf("%d", &n);
printf("请输入起始磁道号:");
scanf("%d", &start);
printf("请输入磁盘请求序列:");
for (int i = 0; i < n; i++) {
scanf("%d", &queue[i]);
}
int res = fcfs(queue, n, start);
printf("平均寻道长度为:%d\n", res);
return 0;
}
```
2. 最短寻道时间优先(SSTF)算法
SSTF算法就是每次选择距离当前磁头最近的请求进行服务。具体实现如下:
```c
#include <stdio.h>
#include <stdlib.h>
#define MAX 1000
int queue[MAX];
int cmp(const void *a, const void *b) {
return *(int *)a - *(int *)b;
}
int sstf(int *queue, int n, int start) {
int sum = 0, head = start, i, j;
while (n) {
int min = abs(head - queue[0]);
for (i = 1; i < n; i++) {
if (abs(head - queue[i]) < min) {
min = abs(head - queue[i]);
j = i;
}
}
sum += min;
head = queue[j];
for (i = j; i < n - 1; i++) {
queue[i] = queue[i + 1];
}
n--;
}
return sum;
}
int main() {
int n, start;
printf("请输入磁盘请求数:");
scanf("%d", &n);
printf("请输入起始磁道号:");
scanf("%d", &start);
printf("请输入磁盘请求序列:");
for (int i = 0; i < n; i++) {
scanf("%d", &queue[i]);
}
qsort(queue, n, sizeof(int), cmp);
int res = sstf(queue, n, start);
printf("平均寻道长度为:%d\n", res);
return 0;
}
```
3. 基于扫描的磁盘调度算法
基于扫描的磁盘调度算法就是按照一个方向(例如向外)进行服务,直到服务完毕后再返回到起点,再按照相反的方向(例如向内)进行服务,直到服务完毕。具体实现如下:
```c
#include <stdio.h>
#include <stdlib.h>
#define MAX 1000
int queue[MAX];
int cmp(const void *a, const void *b) {
return *(int *)a - *(int *)b;
}
int scan(int *queue, int n, int start) {
int sum = 0, head = start, i, j;
qsort(queue, n, sizeof(int), cmp);
for (i = 0; i < n; i++) {
if (queue[i] >= head) {
break;
}
}
j = i - 1;
for (i = j; i >= 0; i--) {
sum += abs(head - queue[i]);
head = queue[i];
}
for (i = j + 1; i < n; i++) {
sum += abs(head - queue[i]);
head = queue[i];
}
return sum;
}
int main() {
int n, start;
printf("请输入磁盘请求数:");
scanf("%d", &n);
printf("请输入起始磁道号:");
scanf("%d", &start);
printf("请输入磁盘请求序列:");
for (int i = 0; i < n; i++) {
scanf("%d", &queue[i]);
}
int res = scan(queue, n, start);
printf("平均寻道长度为:%d\n", res);
return 0;
}
```
以上就是三种磁盘调度算法的实现,如有不懂之处请及时提出。
阅读全文
相关推荐
![application/msword](https://img-home.csdnimg.cn/images/20210720083327.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![application/x-rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![docx](https://img-home.csdnimg.cn/images/20241231044901.png)
![doc](https://img-home.csdnimg.cn/images/20241231044833.png)
![application/msword](https://img-home.csdnimg.cn/images/20210720083327.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)