-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDiskScheduling.java
More file actions
132 lines (105 loc) · 4.33 KB
/
DiskScheduling.java
File metadata and controls
132 lines (105 loc) · 4.33 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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
import java.util.*;
public class DiskScheduling {
// FCFS Algorithm
public static int calculateFCFS(int[] queue, int start) {
int totalDistance = 0;
int currentPosition = start;
for (int request : queue) {
totalDistance += Math.abs(request - currentPosition);
currentPosition = request;
}
return totalDistance;
}
// SSTF Algorithm
public static int calculateSSTF(int[] queue, int start) {
int totalDistance = 0;
int currentPosition = start;
boolean[] visited = new boolean[queue.length];
for (int i = 0; i < queue.length; i++) {
int closest = findClosest(queue, currentPosition, visited);
totalDistance += Math.abs(queue[closest] - currentPosition);
currentPosition = queue[closest];
visited[closest] = true;
}
return totalDistance;
}
private static int findClosest(int[] queue, int currentPosition, boolean[] visited) {
int minDistance = Integer.MAX_VALUE;
int index = -1;
for (int i = 0; i < queue.length; i++) {
if (!visited[i] && Math.abs(queue[i] - currentPosition) < minDistance) {
minDistance = Math.abs(queue[i] - currentPosition);
index = i;
}
}
return index;
}
// SCAN Algorithm
public static int calculateSCAN(int[] queue, int start, int diskSize) {
int totalDistance = 0;
int currentPosition = start;
List<Integer> sortedQueue = new ArrayList<>();
for (int request : queue) {
sortedQueue.add(request);
}
Collections.sort(sortedQueue);
int idx = sortedQueue.indexOf(start);
for (int i = idx; i >= 0; i--) {
totalDistance += Math.abs(sortedQueue.get(i) - currentPosition);
currentPosition = sortedQueue.get(i);
}
if (idx > 0) {
totalDistance += currentPosition; // Move to 0 if needed
currentPosition = 0;
}
for (int i = idx + 1; i < sortedQueue.size(); i++) {
totalDistance += Math.abs(sortedQueue.get(i) - currentPosition);
currentPosition = sortedQueue.get(i);
}
return totalDistance;
}
// C-SCAN Algorithm
public static int calculateCSCAN(int[] queue, int start, int diskSize) {
int totalDistance = 0;
int currentPosition = start;
List<Integer> sortedQueue = new ArrayList<>();
for (int request : queue) {
sortedQueue.add(request);
}
Collections.sort(sortedQueue);
int idx = sortedQueue.indexOf(start);
for (int i = idx; i < sortedQueue.size(); i++) {
totalDistance += Math.abs(sortedQueue.get(i) - currentPosition);
currentPosition = sortedQueue.get(i);
}
if (idx < sortedQueue.size() - 1) {
totalDistance += diskSize - 1 - currentPosition; // Move to the end
totalDistance += diskSize - 1; // Move to start (0)
currentPosition = 0;
}
for (int i = 0; i < idx; i++) {
totalDistance += Math.abs(sortedQueue.get(i) - currentPosition);
currentPosition = sortedQueue.get(i);
}
return totalDistance;
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter the number of cylinders: ");
int diskSize = scanner.nextInt();
System.out.print("Enter the initial head position: ");
int start = scanner.nextInt();
System.out.print("Enter the size of queue: ");
int n = scanner.nextInt();
int[] queue = new int[n];
System.out.println("Enter the queue of disk positions to read:");
for (int i = 0; i < n; i++) {
queue[i] = scanner.nextInt();
}
System.out.println("Total distance moved by Disk Head using FCFS: " + calculateFCFS(queue, start));
System.out.println("Total distance moved by Disk Head using SSTF: " + calculateSSTF(queue, start));
System.out.println("Total distance moved by Disk Head using SCAN: " + calculateSCAN(queue, start, diskSize));
System.out.println("Total distance moved by Disk Head using C-SCAN: " + calculateCSCAN(queue, start, diskSize));
scanner.close();
}
}