sstf program in c
#include <stdio.h>
#include <stdlib.h>
int abs_diff(int a, int b) {
return abs(a - b);
}
int sstf(int arr[], int head, int size) {
int total_distance = 0;
int visited[size];
for (int i = 0; i < size; i++) {
visited[i] = 0;
}
int current = head;
for (int i = 0; i < size; i++) {
int min_distance = __INT_MAX__;
int next;
for (int j = 0; j < size; j++) {
if (!visited[j] && abs_diff(arr[j], current) < min_distance) {
min_distance = abs_diff(arr[j], current);
next = j;
}
}
visited[next] = 1;
total_distance += min_distance;
current = arr[next];
}
return total_distance;
}
int main() {
int arr[] = {176, 79, 34, 60, 92, 11, 41, 114};
int head = 50;
int size = sizeof(arr) / sizeof(arr[0]);
int distance = sstf(arr, head, size);
printf("Total distance: %d\n", distance);
return 0;
}
Explanation of the SSTF Program in C
The provided program is an implementation of the Shortest Seek Time First (SSTF) disk scheduling algorithm. It calculates the total distance traveled by the disk head when accessing a sequence of disk requests.
Step 1: Including necessary libraries
The program begins by including the necessary libraries - stdio.h
and stdlib.h
. These libraries provide functions for input/output operations and memory allocation, respectively.
Step 2: Defining the absolute difference function
The program defines a helper function abs_diff
that calculates the absolute difference between two integers. It takes two integers a
and b
as parameters and returns the absolute difference between them using the abs
function from the stdlib.h
library.
Step 3: Implementing the SSTF function
The program defines the sstf
function, which takes an array arr
representing the disk request sequence, an integer head
representing the current position of the disk head, and an integer size
representing the size of the array.
Step 4: Initializing variables
In the sstf
function, the total distance traveled by the disk head is initialized to 0. An array visited
of size size
is created to keep track of visited disk requests. Each element of the visited
array is initialized to 0, indicating that no requests have been visited yet.
Step 5: Processing the disk requests
Inside a loop that iterates size
times, the program finds the request with the minimum seek time from the current disk head position. It initializes the min_distance
variable to a large value (__INT_MAX__
) and the next
variable to store the index of the next request.
Step 6: Finding the next request
Another loop is used to iterate through the disk requests. For each request that has not been visited yet (!visited[j]
), the program calculates the absolute difference between the request and the current disk head position using the abs_diff
function. If this difference is smaller than the current minimum distance, it updates the min_distance
and next
variables.
Step 7: Marking the next request as visited
After finding the next request with the minimum seek time, the program marks it as visited by setting visited[next]
to 1.
Step 8: Updating the total distance
The program adds the minimum seek time (stored in min_distance
) to the total_distance
variable.
Step 9: Updating the current disk head position
The program updates the current disk head position to the position of the next request (current = arr[next]
).
Step 10: Returning the total distance
After processing all disk requests, the sstf
function returns the total_distance
traveled by the disk head.
Step 11: Implementing the main function
The program defines the main
function, which is the entry point of the program. It initializes an array arr
with a sequence of disk requests, sets the initial position of the disk head (head
), and calculates the size of the array (size
).
Step 12: Calculating and printing the total distance
The program calls the sstf
function with the provided disk requests, head position, and array size. It stores the returned total distance in the distance
variable and prints it using the printf
function.
Step 13: Returning from the main function
The main
function returns 0, indicating successful execution of the program.