You are on page 1of 8

OPERATING SYSTEMS LAB

DISK SCHEDULING ALGORITHMS


NAME: Ishan Mishra
REG.NO.: 21BIT0138

1.Aim: disk scheduling algorithms in

 FCFS:
Algorithm:
1. Start the program.
2. Read the number of request queue elements from the user and store it in the variable
numRequests.
3. Create an integer array requestQueue of size numRequests to store the request queue
sequence.
4. Read the request queue sequence from the user and store each element in the
requestQueue array.
5. Read the head position from the user and store it in the variable headPosition.
6. Initialize the variable totalSeekTime to 0 to keep track of the total seek time.
7. Initialize the variable currentHeadPosition with the value of headPosition to represent the
initial head position.
8. Iterate through the request queue sequence:
 Calculate the seek distance by subtracting the current head position from the
request queue element.
 If the seek distance is negative, take its absolute value.
 Add the seek distance to the total seek time.
 Update the current head position with the value of the request queue element.
 Print the movement details, including the previous head position, current head
position, and seek distance.
9. Print the total seek time.
10. End the program.

Code:

#include <stdio.h>

int main() {

printf("ishan mishra 21BIT0138\n");

int numRequests, headPosition, i;

printf("Enter the number of request queue elements: ");

scanf("%d", &numRequests);

int requestQueue[numRequests];
printf("Enter the request queue sequence: ");

for (i = 0; i < numRequests; i++) {

scanf("%d", &requestQueue[i]);

printf("Enter head position: ");

scanf("%d", &headPosition);

int totalSeekTime = 0;

int currentHeadPosition = headPosition;

for (i = 0; i < numRequests; i++) {

int seekDistance = requestQueue[i] - currentHeadPosition;

if (seekDistance < 0) {

seekDistance = -seekDistance;

totalSeekTime += seekDistance;

currentHeadPosition = requestQueue[i];

printf("Moved from %d to %d, Seek Distance: %d\n", currentHeadPosition - seekDistance,


currentHeadPosition, seekDistance);

printf("\nTotal Seek Time: %d\n", totalSeekTime);

return 0;

}
 SSTF

Algorithm:

1. Start the program.

2. Print your name and student ID.

3. Declare the variables numRequests, headPosition, and i.

4. Prompt the user to enter the number of request queue elements and read the value into
numRequests.

5. Declare an integer array requestQueue of size numRequests.

6. Prompt the user to enter the request queue sequence and read each element into the
requestQueue array using a loop.

7. Prompt the user to enter the head position and read the value into headPosition.

8. Declare the variables maximum and minimum and initialize maximum to 0 and minimum to
the first element of requestQueue.

9. Iterate through the requestQueue array using a loop:

 Update maximum if the current element is greater than maximum.

 Update minimum if the current element is smaller than minimum.

10. Calculate the distance headMinDistance as the difference between headPosition and
minimum.

11. Calculate the distance maxMinDistance as the difference between maximum and minimum.

12. Print the total seek time by adding headMinDistance and maxMinDistance.
13. End the program.

Code:

#include <stdio.h>

int main() {

printf("ishan mishra 21BIT0138\n");

int numRequests, headPosition, i;

printf("Enter the number of request queue elements: ");

scanf("%d", &numRequests);

int requestQueue[numRequests];

printf("Enter the request queue sequence: ");

for (i = 0; i < numRequests; i++) {

scanf("%d", &requestQueue[i]);

printf("Enter head position: ");

scanf("%d", &headPosition);

int maximum = 0;

int minimum = requestQueue[0];

for (int i = 0; i < numRequests; i++) {

if (requestQueue[i] > maximum) {

maximum = requestQueue[i];

if (requestQueue[i] < minimum) {

minimum = requestQueue[i];

int headMinDistance = headPosition - minimum;


int maxMinDistance = maximum - minimum;

printf("Total seek time = %d ", headMinDistance + maxMinDistance);

return 0;

 SCAN

Algorithm:

1. Start the program.

2. Prompt the user to enter the number of tracks and read the value into numTracks.

3. Prompt the user to enter the number of request queue elements and read the value into
numRequests.

4. Declare an integer array requestQueue of size numRequests.

5. Prompt the user to enter the request queue sequence and read each element into the
requestQueue array using a loop.

6. Prompt the user to enter the head position and read the value into headPosition.

7. Prompt the user to choose the direction of movement (1 for right-to-left, 2 for left-to-right)
and read the value into direction.

8. Declare the variables maxTrack and minTrack and initialize maxTrack to 0 and minTrack to
the first element of requestQueue.

9. Iterate through the requestQueue array using a loop:

 Update maxTrack if the current element is greater than maxTrack.

 Update minTrack if the current element is smaller than minTrack.

10. If the chosen direction is 1 (right-to-left):

 Calculate the distance from the head position to the maximum track as (numTracks -
1) - headPosition.

 Calculate the distance from the minimum track to the maximum track as (numTracks
- 1) - minTrack.
 Compute the total seek time as the sum of the two distances.

 Print the total seek time.

11. If the chosen direction is 2 (left-to-right):

 Calculate the distance from the head position to the minimum track as headPosition
- 0.

 Calculate the distance from the maximum track to the minimum track as maxTrack -
0.

 Compute the total seek time as the sum of the two distances.

 Print the total seek time.

12. If the chosen direction is neither 1 nor 2, print an error message indicating an invalid
direction choice.

13. End the program.

Code:

#include <stdio.h>

int main() {

printf("ishan mishra 21BIT0138\n");

int numTracks, numRequests, headPosition, i;

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

scanf("%d", &numTracks);

printf("Enter the number of request queue elements: ");

scanf("%d", &numRequests);

int requestQueue[numRequests];

printf("Enter the request queue sequence: ");

for (i = 0; i < numRequests; i++) {

scanf("%d", &requestQueue[i]);

}
printf("Enter head position: ");

scanf("%d", &headPosition);

int direction;

printf("Type 1 to go right-to-left\n");

printf("Type 2 to go left-to-right\n");

scanf("%d", &direction);

int maxTrack = 0;

int minTrack = requestQueue[0];

for (i = 0; i < numRequests; i++) {

if (requestQueue[i] > maxTrack) {

maxTrack = requestQueue[i];

if (requestQueue[i] < minTrack) {

minTrack = requestQueue[i];

if (direction == 1) {

int distanceToMin = (numTracks - 1) - headPosition;

int distanceToMax = (numTracks - 1) - minTrack;

int totalSeekTime = distanceToMin + distanceToMax;

printf("Total seek time: %d\n", totalSeekTime);

else if (direction == 2) {

int distanceToMin = headPosition - 0;

int distanceToMax = maxTrack - 0;

int totalSeekTime = distanceToMin + distanceToMax;

printf("Total seek time: %d\n", totalSeekTime);


}

else {

printf("Invalid direction choice.\n");

return 0;

You might also like