-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdisk_sch_SSTF.c
More file actions
70 lines (54 loc) · 1.96 KB
/
Copy pathdisk_sch_SSTF.c
File metadata and controls
70 lines (54 loc) · 1.96 KB
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#include <stdio.h>
#include <stdlib.h>
int find_closest(int requests[], int n, int head) {
int min_distance = abs(requests[0] - head);
int index = 0;
for (int i = 1; i < n; i++) {
int distance = abs(requests[i] - head);
if (distance < min_distance) {
min_distance = distance;
index = i;
}
}
return index;
}
int main() {
int size, n, head, step = 1;
float seek_time_unit, rotational_latency;
// Inputs
printf("Enter the total disk size (number of tracks): ");
scanf("%d", &size);
printf("Enter the number of disk requests: ");
scanf("%d", &n);
int requests[n];
printf("Enter the disk request sequence (track numbers):\n");
for (int i = 0; i < n; i++) {
scanf("%d", &requests[i]);
}
printf("Enter the initial head position: ");
scanf("%d", &head);
printf("Enter the seek time per track (e.g., 0.1 ms): ");
scanf("%f", &seek_time_unit);
printf("Enter the rotational latency per access (e.g., 2.0 ms): ");
scanf("%f", &rotational_latency);
// SSTF scheduling
printf("\nSeek Sequence (SSTF):\n");
printf("Step\tFrom\tTo\tTracks Moved\tSeek Time (ms)\n");
float total_seek_time = 0;
while (n > 0) {
int index = find_closest(requests, n, head);
int distance = abs(requests[index] - head);
float seek_time = distance * seek_time_unit + rotational_latency;
printf("%d\t%d\t%d\t%d\t\t%.2f\n", step++, head, requests[index], distance, seek_time);
total_seek_time += seek_time;
head = requests[index];
// Remove the processed request
for (int i = index; i < n - 1; i++) {
requests[i] = requests[i + 1];
}
n--;
}
printf("\nTotal Seek Time: %.2f ms\n", total_seek_time);
printf("Average Seek Time per request: %.2f ms\n", total_seek_time / (step - 1));
return 0;
}