mirror of
https://github.com/Manoj-HV30/sp-lab.git
synced 2026-05-16 19:35:27 +00:00
85 lines
2.3 KiB
C
85 lines
2.3 KiB
C
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
|
|
#define MAX 50
|
|
|
|
|
|
void fcfs(int requests[], int n, int head) {
|
|
int total = 0;
|
|
|
|
printf("\n--- FCFS ---\n");
|
|
printf("Order: %d", head);
|
|
|
|
for (int i = 0; i < n; i++) {
|
|
total += abs(requests[i] - head);
|
|
head = requests[i];
|
|
printf(" -> %d", head);
|
|
}
|
|
|
|
printf("\nTotal Head Movement: %d\n", total);
|
|
}
|
|
|
|
// ── SCAN ──────────────────────────────────────────────────────────────────
|
|
void scan(int requests[], int n, int head, int disk_size) {
|
|
int total = 0;
|
|
int req[MAX];
|
|
|
|
// copy and sort requests
|
|
for (int i = 0; i < n; i++) req[i] = requests[i];
|
|
for (int i = 0; i < n - 1; i++)
|
|
for (int j = 0; j < n - i - 1; j++)
|
|
if (req[j] > req[j+1]) { int t = req[j]; req[j] = req[j+1]; req[j+1] = t; }
|
|
|
|
// find where head sits in sorted array
|
|
int pos = 0;
|
|
while (pos < n && req[pos] < head) pos++;
|
|
|
|
printf("\n--- SCAN (moving towards higher tracks first) ---\n");
|
|
printf("Order: %d", head);
|
|
|
|
// go right (higher) first
|
|
for (int i = pos; i < n; i++) {
|
|
total += abs(req[i] - head);
|
|
head = req[i];
|
|
printf(" -> %d", head);
|
|
}
|
|
if (head != disk_size - 1) {
|
|
|
|
total += abs((disk_size - 1) - head);
|
|
head = disk_size - 1;
|
|
|
|
printf(" -> %d", head);
|
|
}
|
|
|
|
// go left (lower)
|
|
for (int i = pos - 1; i >= 0; i--) {
|
|
total += abs(req[i] - head);
|
|
head = req[i];
|
|
printf(" -> %d", head);
|
|
}
|
|
|
|
printf("\nTotal Head Movement: %d\n", total);
|
|
}
|
|
|
|
// ── MAIN ──────────────────────────────────────────────────────────────────
|
|
int main() {
|
|
int requests[MAX], n, head, disk_size;
|
|
|
|
printf("Enter number of requests: ");
|
|
scanf("%d", &n);
|
|
printf("Enter requests: ");
|
|
for (int i = 0; i < n; i++)
|
|
scanf("%d", &requests[i]);
|
|
|
|
printf("Enter initial head position: ");
|
|
scanf("%d", &head);
|
|
|
|
printf("Enter disk size: ");
|
|
scanf("%d", &disk_size);
|
|
|
|
fcfs(requests, n, head);
|
|
scan(requests, n, head, disk_size);
|
|
|
|
return 0;
|
|
}
|