用C语言编写Cscan算法、look调度算法、Clook调度算法
时间: 2023-12-24 20:04:16 浏览: 125
基于C语言的磁盘调度算法的实现.zip
5星 · 资源好评率100%
1. C语言实现Cscan算法:
```c
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#define MAX 100
void sort(int n, int arr[]) {
for (int i = 0; i < n; i++) {
for (int j = 0; j < n - i - 1; j++) {
if (arr[j] > arr[j + 1]) {
int temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
}
int main() {
int n, head, i, j, seek_time = 0, max, disk[MAX], left[MAX], right[MAX];
printf("Enter the number of disk requests: ");
scanf("%d", &n);
printf("Enter the requests: ");
for (i = 0; i < n; i++) {
scanf("%d", &disk[i]);
}
printf("Enter the initial head position: ");
scanf("%d", &head);
// Divide the requests into left and right of the head
int left_cnt = 0, right_cnt = 0;
for (i = 0; i < n; i++) {
if (disk[i] < head) {
left[left_cnt++] = disk[i];
} else {
right[right_cnt++] = disk[i];
}
}
// Sort the left and right requests in ascending order
sort(left_cnt, left);
sort(right_cnt, right);
// Calculate seek time
for (i = 0; i < 2; i++) {
if (i == 0) {
max = left[left_cnt - 1];
for (j = left_cnt - 2; j >= 0; j--) {
seek_time += abs(max - left[j]);
max = left[j];
}
seek_time += max - 0;
} else {
max = right[0];
for (j = 1; j < right_cnt; j++) {
seek_time += abs(max - right[j]);
max = right[j];
}
seek_time += 199 - max;
}
}
printf("Total seek time: %d\n", seek_time);
return 0;
}
```
2. C语言实现look调度算法:
```c
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#define MAX 100
void sort(int n, int arr[]) {
for (int i = 0; i < n; i++) {
for (int j = 0; j < n - i - 1; j++) {
if (arr[j] > arr[j + 1]) {
int temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
}
int main() {
int n, head, i, j, seek_time = 0, max, disk[MAX];
printf("Enter the number of disk requests: ");
scanf("%d", &n);
printf("Enter the requests: ");
for (i = 0; i < n; i++) {
scanf("%d", &disk[i]);
}
printf("Enter the initial head position: ");
scanf("%d", &head);
// Sort the requests in ascending order
sort(n, disk);
// Find the index of the head position in the requests
int index = 0;
for (i = 0; i < n; i++) {
if (disk[i] == head) {
index = i;
break;
}
}
// Move the head from the initial position towards the end
for (i = index; i < n - 1; i++) {
seek_time += abs(disk[i + 1] - disk[i]);
}
// If there are requests after the head position, move the head to the last request
if (index != n - 1) {
seek_time += abs(disk[n - 1] - disk[index]);
}
// Move the head from the last request towards the beginning
for (i = n - 1; i > 0; i--) {
seek_time += abs(disk[i] - disk[i - 1]);
}
printf("Total seek time: %d\n", seek_time);
return 0;
}
```
3. C语言实现Clook调度算法:
```c
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#define MAX 100
void sort(int n, int arr[]) {
for (int i = 0; i < n; i++) {
for (int j = 0; j < n - i - 1; j++) {
if (arr[j] > arr[j + 1]) {
int temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
}
int main() {
int n, head, i, j, seek_time = 0, max, disk[MAX], left[MAX], right[MAX];
printf("Enter the number of disk requests: ");
scanf("%d", &n);
printf("Enter the requests: ");
for (i = 0; i < n; i++) {
scanf("%d", &disk[i]);
}
printf("Enter the initial head position: ");
scanf("%d", &head);
// Divide the requests into left and right of the head
int left_cnt = 0, right_cnt = 0;
for (i = 0; i < n; i++) {
if (disk[i] < head) {
left[left_cnt++] = disk[i];
} else {
right[right_cnt++] = disk[i];
}
}
// Sort the left and right requests in ascending order
sort(left_cnt, left);
sort(right_cnt, right);
// Calculate seek time
max = right[0];
for (i = 1; i < right_cnt; i++) {
seek_time += abs(max - right[i]);
max = right[i];
}
seek_time += abs(max - left[0]);
max = left[0];
for (i = 1; i < left_cnt; i++) {
seek_time += abs(max - left[i]);
max = left[i];
}
printf("Total seek time: %d\n", seek_time);
return 0;
}
```
阅读全文