#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
// FCFS算法
int fcfs(int disk_queue[], int length, int head) {
int total_movement = 0;
int current_track = head;
for (int i = 0; i < length; i++) {
int movement = abs(disk_queue[i] - current_track);
total_movement += movement;
current_track = disk_queue[i];
}
return total_movement;
}
// SSTF算法
int sstf(int disk_queue[], int length, int head) {
int total_movement = 0;
int current_track = head;
while (length > 0) {
int min_distance = INT_MAX;
int min_distance_index = -1;
for (int i = 0; i < length; i++) {
int distance = abs(disk_queue[i] - current_track);
if (distance < min_distance) {
min_distance = distance;
min_distance_index = i;
}
}
int closest_track = disk_queue[min_distance_index];
int movement = abs(closest_track - current_track);
total_movement += movement;
current_track = closest_track;
// 移除已访问的磁道
for (int i = min_distance_index; i < length - 1; i++) {
disk_queue[i] = disk_queue[i + 1];
}
length--;
}
return total_movement;
}
// SCAN算法
int scan(int disk_queue[], int length, int head) {
int total_movement = 0;
int current_track = head;
int direction = 1; // 1: right, -1: left
// 将磁道按升序排序
for (int i = 0; i < length - 1; i++) {
for (int j = 0; j < length - i - 1; j++) {
if (disk_queue[j] > disk_queue[j + 1]) {
int temp = disk_queue[j];
disk_queue[j] = disk_queue[j + 1];
disk_queue[j + 1] = temp;
}
}
}
// 找到当前磁头位置在排序后的磁道队列中的索引
int current_track_index = -1;
for (int i = 0; i < length; i++) {
if (disk_queue[i] == head) {
current_track_index = i;
break;
}
}
// 根据磁头位置确定初始方向
if (current_track_index < length / 2) {
direction = 1;
} else {
direction = -1;
}
// 根据方向遍历磁道
while (length > 0) {
if (direction == 1) {
// 向右移动
for (int i = current_track_index; i < length; i++) {
int track = disk_queue[i];
int movement = abs(track - current_track);
total_movement += movement;
current_track = track;
}
} else {
// 向左移动
for (int i = current_track_index; i >= 0; i--) {
int track = disk_queue[i];
int movement = abs(track - current_track);
total_movement += movement;
current_track = track;
}
}
// 移除已访问的磁道
for (int i = current_track_index; i < length - 1; i++) {
disk_queue[i] = disk_queue[i + 1];
}
length--;
if (direction == 1) {
current_track_index = (current_track_index < length) ? current_track_index : length - 1;
} else {
current_track_index = (current_track_index > 0) ? current_track_index - 1 : 0;
}
// 切换方向
if (current_track_index == 0 || current_track_index == length - 1) {
direction *= -1;
}
}
return total_movement;
}
int main() {
int length, head;
printf("Enter the number of tracks: ");
scanf("%d", &length);
int disk_queue[length];
printf("Enter the track positions:\n");
for (int i = 0; i < length; i++) {
scanf("%d", &disk_queue[i]);
}
printf("Enter the initial head position: ");
scanf("%d", &head);
int fcfs_movement = fcfs(disk_queue, length, head);
int sstf_movement = sstf(disk_queue, length, head);
int scan_movement = scan(disk_queue, length, head);
printf("Total head movement using FCFS algorithm: %d\n", fcfs_movement);
printf("Total head movement using SSTF algorithm: %d\n", sstf_movement);
printf("Total head movement using SCAN algorithm: %d\n", scan_movement);
return 0;
}
roott@ubuntu:/usr/src/linux-headers-5.8.0-43-generic$ sudo make menuconfig
HOSTCC scripts/basic/fixdep
HOSTCC scripts/kconfig/confdata.o
HOSTCC scripts/kconfig/expr.o
LEX scripts/kconfig/lexer.lex.c
/bin/sh: 1: flex: not found
make[1]: *** [scripts/Makefile.host:9: scripts/kconfig/lexer.lex.c] Error 127
make: *** [Makefile:633: menuconfig] Error 2