0% found this document useful (0 votes)
5 views2 pages

CSCAN

The document is a C program that calculates the total head movement for a disk scheduling algorithm based on user inputs. It sorts a sequence of disk requests, determines the initial head position, and computes the total movement based on the specified direction (high or low). The program outputs the total head movement required to service all requests.

Uploaded by

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

CSCAN

The document is a C program that calculates the total head movement for a disk scheduling algorithm based on user inputs. It sorts a sequence of disk requests, determines the initial head position, and computes the total movement based on the specified direction (high or low). The program outputs the total head movement required to service all requests.

Uploaded by

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

#include<stdio.

h>
#include<stdlib.h>
int main()
{
int RQ[100], i, j, n, THM = 0, initial, size, move;
printf("Enter the number of Requests\n");
scanf_s("%d", &n);
printf("Enter the Requests sequence\n");
for (i = 0; i < n; i++)
scanf_s("%d", &RQ[i]);
printf("Enter initial head position\n");
scanf_s("%d", &initial);
printf("Enter total disk size\n");
scanf_s("%d", &size);
printf("Enter the head movement direction for high 1 and for low 0\n");
scanf_s("%d", &move);

for (i = 0; i < n; i++)


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

}
}

int index;
for (i = 0; i < n; i++)
{
if (initial < RQ[i])
{
index = i;
break;
}
}

// if movement is towards high value


if (move == 1)
{
for (i = index; i < n; i++)
{
THM = THM + abs(RQ[i] - initial);
initial = RQ[i];
}
// last movement for max size
THM = THM + abs(size - RQ[i - 1] - 1);
/*movement max to min disk */
THM = THM + abs(size - 1 - 0);
initial = 0;
for (i = 0; i < index; i++)
{
THM = THM + abs(RQ[i] - initial);
initial = RQ[i];
}
}
// if movement is towards low value
else
{
for (i = index - 1; i >= 0; i--)
{
THM = THM + abs(RQ[i] - initial);
initial = RQ[i];
}
// last movement for min size
THM = THM + abs(RQ[i + 1] - 0);
/*movement min to max disk */
THM = THM + abs(size - 1 - 0);
initial = size - 1;
for (i = n - 1; i >= index; i--)
{
THM = THM + abs(RQ[i] - initial);
initial = RQ[i];

}
}

printf("Total Head Movement is %d", THM);


return 0;
}

You might also like