Professional Documents
Culture Documents
OS Lab 10
OS Lab 10
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() {
scanf("%d", &numRequests);
int requestQueue[numRequests];
printf("Enter the request queue sequence: ");
scanf("%d", &requestQueue[i]);
scanf("%d", &headPosition);
int totalSeekTime = 0;
if (seekDistance < 0) {
seekDistance = -seekDistance;
totalSeekTime += seekDistance;
currentHeadPosition = requestQueue[i];
return 0;
}
SSTF
Algorithm:
4. Prompt the user to enter the number of request queue elements and read the value into
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.
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() {
scanf("%d", &numRequests);
int requestQueue[numRequests];
scanf("%d", &requestQueue[i]);
scanf("%d", &headPosition);
int maximum = 0;
maximum = requestQueue[i];
minimum = requestQueue[i];
return 0;
SCAN
Algorithm:
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.
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.
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.
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.
12. If the chosen direction is neither 1 nor 2, print an error message indicating an invalid
direction choice.
Code:
#include <stdio.h>
int main() {
scanf("%d", &numTracks);
scanf("%d", &numRequests);
int requestQueue[numRequests];
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;
maxTrack = requestQueue[i];
minTrack = requestQueue[i];
if (direction == 1) {
else if (direction == 2) {
else {
return 0;