Experiment Detail: Develop a C program to simulate SCAN disk scheduling algorithm
Program:
#include <stdio.h>
#include <stdlib.h>
int main() {
int queue[20], n, head, i, j, seek = 0, max, diff, temp, queue1[20], queue2[20];
int temp1 = 0, temp2 = 0;
float avg;
printf("Enter the max range of disk = ");
scanf("%d", &max);
printf("Enter the initial head position = ");
scanf("%d", &head);
// Simulating the disk queue positions for testing purposes
int pos[] = {90, 120, 35, 122, 38, 128, 65, 68};
n = sizeof(pos) / sizeof(pos[0]);
printf("Disk positions to be read: ");
for (i = 0; i < n; i++) {
printf("%d ", pos[i]);
printf("\n");
for (i = 0; i < n; i++) {
temp = pos[i];
if (temp >= head) {
queue1[temp1] = temp;
temp1++;
} else {
queue2[temp2] = temp;
temp2++;
// Sort queue1 in ascending order
for (i = 0; i < temp1 - 1; i++) {
for (j = i + 1; j < temp1; j++) {
if (queue1[i] > queue1[j]) {
temp = queue1[i];
queue1[i] = queue1[j];
queue1[j] = temp;
// Sort queue2 in descending order
for (i = 0; i < temp2 - 1; i++) {
for (j = i + 1; j < temp2; j++) {
if (queue2[i] < queue2[j]) {
temp = queue2[i];
queue2[i] = queue2[j];
queue2[j] = temp;
// Combine queue1 and queue2 into the main queue
int index = 1; // Start from index 1 as queue[0] will be the head
for (i = 0; i < temp1; i++) {
queue[index++] = queue1[i];
}
queue[index++] = max; // Move to the max if SCAN reaches the end
for (i = 0; i < temp2; i++) {
queue[index++] = queue2[i];
queue[0] = head; // Head position as the starting point in queue
// Calculate total seek time
for (j = 0; j < index - 1; j++) {
diff = abs(queue[j + 1] - queue[j]);
seek += diff;
printf("Disk head moves from %d to %d with seek %d\n", queue[j], queue[j + 1], diff);
printf("Total seek time is %d\n", seek);
avg = seek / (float)n;
printf("Average seek time is %.2f\n", avg);
return 0;
OUTPUT:
Enter the max range of disk = 200
Enter the initial head position = 50
Disk positions to be read: 90 120 35 122 38 128 65 68
Disk head moves from 50 to 65 with seek 15
Disk head moves from 65 to 68 with seek 3
Disk head moves from 68 to 90 with seek 22
Disk head moves from 90 to 120 with seek 30
Disk head moves from 120 to 122 with seek 2
Disk head moves from 122 to 128 with seek 6
Disk head moves from 128 to 200 with seek 72
Disk head moves from 200 to 38 with seek 162
Disk head moves from 38 to 35 with seek 3
Total seek time is 315
Average seek time is 39.38