MIT-Curricular/OS/C/Week11/qq1.c
2025-04-11 08:30:13 +05:30

234 lines
5.8 KiB
C

#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
void sstf(int requests[], int n, int head) {
int total_seek = 0;
int completed = 0;
int visited[100] = {0};
int current = head;
printf("\nSSTF Disk Scheduling\n");
printf("Seek Sequence: %d", head);
while (completed < n) {
int min_distance = INT_MAX;
int min_index = -1;
for (int i = 0; i < n; i++) {
if (!visited[i]) {
int distance = abs(requests[i] - current);
if (distance < min_distance) {
min_distance = distance;
min_index = i;
}
}
}
visited[min_index] = 1;
current = requests[min_index];
total_seek += min_distance;
completed++;
printf(" -> %d", current);
}
printf("\nTotal Seek Time: %d\n", total_seek);
}
void scan(int requests[], int n, int head, int disk_size) {
int total_seek = 0;
int direction = 1; // 1 for moving right, 0 for moving left
int current = head;
printf("\nSCAN Disk Scheduling\n");
printf("Seek Sequence: %d", head);
// Sort requests
for (int i = 0; i < n; i++) {
for (int j = 0; j < n - i - 1; j++) {
if (requests[j] > requests[j + 1]) {
int temp = requests[j];
requests[j] = requests[j + 1];
requests[j + 1] = temp;
}
}
}
// Find position of head in sorted array
int index;
for (index = 0; index < n; index++) {
if (requests[index] >= head)
break;
}
// Move right
for (int i = index; i < n; i++) {
current = requests[i];
printf(" -> %d", current);
total_seek += abs(current - head);
head = current;
}
// Move to the end of disk
printf(" -> %d", disk_size - 1);
total_seek += abs(disk_size - 1 - head);
head = disk_size - 1;
// Move left
for (int i = index - 1; i >= 0; i--) {
current = requests[i];
printf(" -> %d", current);
total_seek += abs(current - head);
head = current;
}
printf("\nTotal Seek Time: %d\n", total_seek);
}
void cscan(int requests[], int n, int head, int disk_size) {
int total_seek = 0;
int current = head;
printf("\nC-SCAN Disk Scheduling\n");
printf("Seek Sequence: %d", head);
// Sort requests
for (int i = 0; i < n; i++) {
for (int j = 0; j < n - i - 1; j++) {
if (requests[j] > requests[j + 1]) {
int temp = requests[j];
requests[j] = requests[j + 1];
requests[j + 1] = temp;
}
}
}
// Find position of head in sorted array
int index;
for (index = 0; index < n; index++) {
if (requests[index] >= head)
break;
}
// Move right
for (int i = index; i < n; i++) {
current = requests[i];
printf(" -> %d", current);
total_seek += abs(current - head);
head = current;
}
// Move to the end of disk
printf(" -> %d", disk_size - 1);
total_seek += abs(disk_size - 1 - head);
// Move to the beginning
printf(" -> 0");
total_seek += disk_size - 1;
head = 0;
// Move right again
for (int i = 0; i < index; i++) {
current = requests[i];
printf(" -> %d", current);
total_seek += abs(current - head);
head = current;
}
printf("\nTotal Seek Time: %d\n", total_seek);
}
void clook(int requests[], int n, int head) {
int total_seek = 0;
int current = head;
printf("\nC-LOOK Disk Scheduling\n");
printf("Seek Sequence: %d", head);
// Sort requests
for (int i = 0; i < n; i++) {
for (int j = 0; j < n - i - 1; j++) {
if (requests[j] > requests[j + 1]) {
int temp = requests[j];
requests[j] = requests[j + 1];
requests[j + 1] = temp;
}
}
}
// Find position of head in sorted array
int index;
for (index = 0; index < n; index++) {
if (requests[index] >= head)
break;
}
// Move right
for (int i = index; i < n; i++) {
current = requests[i];
printf(" -> %d", current);
total_seek += abs(current - head);
head = current;
}
// Move to first request
for (int i = 0; i < index; i++) {
current = requests[i];
printf(" -> %d", current);
total_seek += abs(current - head);
head = current;
}
printf("\nTotal Seek Time: %d\n", total_seek);
}
int main() {
int requests[100], n, head, disk_size, choice;
printf("Enter the number of disk requests: ");
scanf("%d", &n);
printf("Enter the disk requests: ");
for (int i = 0; i < n; i++) {
scanf("%d", &requests[i]);
}
printf("Enter the initial head position: ");
scanf("%d", &head);
printf("Enter the disk size (0 to size-1): ");
scanf("%d", &disk_size);
do {
printf("\n\nDisk Scheduling Algorithms\n");
printf("1. SSTF (Shortest Seek Time First)\n");
printf("2. SCAN\n");
printf("3. C-SCAN\n");
printf("4. C-LOOK\n");
printf("5. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
sstf(requests, n, head);
break;
case 2:
scan(requests, n, head, disk_size);
break;
case 3:
cscan(requests, n, head, disk_size);
break;
case 4:
clook(requests, n, head);
break;
case 5:
printf("Exiting program...\n");
break;
default:
printf("Invalid choice!\n");
}
} while (choice != 5);
return 0;
}