0% found this document useful (0 votes)
12 views5 pages

Disk Scheduling algo-FCFS and SCAN

!!!best
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views5 pages

Disk Scheduling algo-FCFS and SCAN

!!!best
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

#include <stdio.

h>
#include <stdlib.h>

#define MAX_REQUESTS 100

void fcfs(int requests[], int n, int head) {


int total_head_movement = 0;
int current_position = head;

printf("FCFS Scheduling Order:\n");


for (int i = 0; i < n; i++) {
printf("Move from %d to %d\n", current_position, requests[i]);
total_head_movement += abs(current_position - requests[i]);
current_position = requests[i];
}

printf("Total head movement: %d\n", total_head_movement);


}

void scan(int requests[], int n, int head) {


int total_head_movement = 0;
int current_position = head;

// Sort requests using bubble sort


int sorted_requests[MAX_REQUESTS];
for (int i = 0; i < n; i++) {
sorted_requests[i] = requests[i];
}
for (int i = 0; i < n - 1; i++) {
for (int j = 0; j < n - i - 1; j++) {
if (sorted_requests[j] > sorted_requests[j + 1]) {
int temp = sorted_requests[j];
sorted_requests[j] = sorted_requests[j + 1];
sorted_requests[j + 1] = temp;
}
}
}

printf("SCAN Scheduling Order:\n");

// Move to the right first


for (int i = 0; i < n; i++) {
if (sorted_requests[i] >= current_position) {
printf("Move from %d to %d\n", current_position, sorted_requests[i]);
total_head_movement += abs(current_position - sorted_requests[i]);
current_position = sorted_requests[i];
}
}

// Reverse direction
printf("Move from %d to %d\n", current_position, sorted_requests[n - 1]);
total_head_movement += abs(current_position - sorted_requests[n - 1]);
current_position = sorted_requests[n - 1];

// Move to the left


for (int i = n - 1; i >= 0; i--) {
if (sorted_requests[i] <= current_position) {
printf("Move from %d to %d\n", current_position, sorted_requests[i]);
total_head_movement += abs(current_position - sorted_requests[i]);
current_position = sorted_requests[i];
}
}

printf("Total head movement: %d\n", total_head_movement);


}

int main() {
int requests[MAX_REQUESTS];
int n, head;

printf("Enter the number of requests: ");


scanf("%d", &n);

if (n > MAX_REQUESTS) {
printf("Error: Maximum requests exceeded.\n");
return 1;
}
printf("Enter the requests:\n");
for (int i = 0; i < n; i++) {
scanf("%d", &requests[i]);
}

printf("Enter the initial head position: ");


scanf("%d", &head);

fcfs(requests, n, head);
printf("\n");
scan(requests, n, head);

return 0;
}
Enter the number of requests: 8
Enter the requests:
98
183
37
122
14
124
65
67
Enter the initial head position: 53
FCFS Scheduling Order:
Move from 53 to 98
Move from 98 to 183
Move from 183 to 37
Move from 37 to 122
Move from 122 to 14
Move from 14 to 124
Move from 124 to 65
Move from 65 to 67
Total head movement: 278

SCAN Scheduling Order:


Move from 53 to 65
Move from 65 to 67
Move from 67 to 98
Move from 98 to 122
Move from 122 to 124
Move from 124 to 183
Move from 183 to 14
Move from 14 to 37
Total head movement: 288

You might also like