-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdisk_scheduling.c
65 lines (61 loc) · 2.11 KB
/
disk_scheduling.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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#include <stdio.h>
#include <math.h>
int initialPosition, numberOfSeeks, cylinders[10], totalHeadMovements = 0, totalSeekTime = 0;
struct Cylinder {
int cylinder, distance;
_Bool visited;
};
void FCFS (struct Cylinder *cylinders, int seekTime) {
int difference;
for (int i = 0; i < numberOfSeeks; i++) {
difference = abs(initialPosition-cylinders[i].cylinder);
totalHeadMovements += difference;
cylinders[i].distance = difference;
initialPosition = cylinders[i].cylinder;
}
totalSeekTime = totalHeadMovements * seekTime;
}
void SSTF (struct Cylinder *cylinders, int seekTime) {
int difference;
while (1) {
int min = 9999, index;
for (int i = 0; i < numberOfSeeks; i++) {
if (cylinders[i].visited) continue;
difference = abs(initialPosition-cylinders[i].cylinder);
if (difference < min) {
min = difference;
index = i;
}
}
if (min == 9999) break;
cylinders[index].visited = 1;
cylinders[index].distance = min;
totalHeadMovements += min;
initialPosition = cylinders[index].cylinder;
}
}
int main() {
int seekTime, choice;
struct Cylinder cylinders[10];
printf("Enter the number of cylinders to visit: ");
scanf("%d", &numberOfSeeks);
printf("Enter the cylinders one by one: ");
for (int i = 0; i < numberOfSeeks; i++) {
scanf("%d", &cylinders[i].cylinder);
cylinders[i].visited = 0;
}
printf("Enter the initial head position: ");
scanf("%d", &initialPosition);
printf("Enter the seek time per movement (ms): ");
scanf("%d", &seekTime);
printf("Choose the disk scheduling algorithm:\n1. FCFS\n2. SSTF\nEnter your choice: ");
scanf("%d", &choice);
if (choice == 1) FCFS(cylinders, seekTime);
else if (choice == 2) SSTF(cylinders, seekTime);
else {
printf("Invalid choice!\n");
return 0;
}
printf("\nTotal head movements: %d", totalHeadMovements);
printf("\nTotal seek time: %d ms\n", totalSeekTime);
}