-
Notifications
You must be signed in to change notification settings - Fork 0
/
7 SSTF.c
48 lines (36 loc) · 1.35 KB
/
7 SSTF.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#include <stdio.h>
#include <stdlib.h>
#define TOTAL_REQUESTS 8 // Total number of disk requests
void calculateSeekTime(int initialPosition, int requests[]) {
int current = initialPosition;
int totalSeekTime = 0;
int completed[TOTAL_REQUESTS] = {0}; // Array to keep track of completed requests
printf("Disk Head Movement:\n");
printf("%d -> ", current);
for (int i = 0; i < TOTAL_REQUESTS; i++) {
int minDistance = 9999; // Initialize with a large value
int minIndex = -1;
// Find the request with the shortest seek time
for (int j = 0; j < TOTAL_REQUESTS; j++) {
if (!completed[j]) {
int distance = abs(requests[j] - current);
if (distance < minDistance) {
minDistance = distance;
minIndex = j;
}
}
}
// Mark the selected request as completed
completed[minIndex] = 1;
current = requests[minIndex];
totalSeekTime += minDistance;
printf("%d -> ", current);
}
printf("\nTotal Seek Time: %d\n", totalSeekTime);
}
int main() {
int initialPosition = 50; // Initial position of the disk head
int requests[TOTAL_REQUESTS] = { 98, 183, 37, 122, 14, 124, 65, 67 }; // Disk requests
calculateSeekTime(initialPosition, requests);
return 0;
}