-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCpuSchedulingSimulator.java
More file actions
188 lines (156 loc) · 5.69 KB
/
CpuSchedulingSimulator.java
File metadata and controls
188 lines (156 loc) · 5.69 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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
import java.util.*;
public class CpuSchedulingSimulator {
static final String TRACE = "trace";
static final String SHOW_STATISTICS = "stats";
static int processCount;
static int lastInstant = 20;
static List<Process> processes = new ArrayList<>();
static char[][] timeline;
static int[] finishTime;
static int[] turnAroundTime;
static double[] normTurn;
static List<Algorithm> algorithms = new ArrayList<>();
static String operation;
static class Process {
String name;
int arrival;
int service;
int priority;
Process(String name, int arrival, int service, int priority) {
this.name = name;
this.arrival = arrival;
this.service = service;
this.priority = priority;
}
}
static class Algorithm {
char id;
int quantum;
Algorithm(char id, int quantum) {
this.id = id;
this.quantum = quantum;
}
}
static void initializeStats() {
timeline = new char[lastInstant][processCount];
finishTime = new int[processCount];
turnAroundTime = new int[processCount];
normTurn = new double[processCount];
}
static void clearTimeline() {
for (int i = 0; i < lastInstant; i++) {
Arrays.fill(timeline[i], ' ');
}
}
static void fillInWaitTime() {
for (int i = 0; i < processCount; i++) {
int arrival = processes.get(i).arrival;
for (int t = arrival; t < finishTime[i]; t++) {
if (timeline[t][i] != '*') {
timeline[t][i] = '.';
}
}
}
}
static void runFCFS() {
int time = processes.get(0).arrival;
for (int i = 0; i < processCount; i++) {
Process p = processes.get(i);
int start = Math.max(time, p.arrival);
int end = start + p.service;
finishTime[i] = end;
turnAroundTime[i] = end - p.arrival;
normTurn[i] = (double) turnAroundTime[i] / p.service;
for (int t = start; t < end; t++) timeline[t][i] = '*';
for (int t = p.arrival; t < start; t++) timeline[t][i] = '.';
time = end;
}
}
static void runRoundRobin(int quantum) {
Deque<Integer> readyQueue = new ArrayDeque<>();
int[] remaining = new int[processCount];
boolean[] finished = new boolean[processCount];
int time = 0, j = 0;
for (int i = 0; i < processCount; i++) remaining[i] = processes.get(i).service;
while (time < lastInstant) {
while (j < processCount && processes.get(j).arrival <= time) {
readyQueue.addLast(j);
j++;
}
if (!readyQueue.isEmpty()) {
int pid = readyQueue.pollFirst();
Process p = processes.get(pid);
int slice = Math.min(quantum, remaining[pid]);
for (int s = 0; s < slice && time < lastInstant; s++) {
timeline[time][pid] = '*';
time++;
while (j < processCount && processes.get(j).arrival <= time) {
readyQueue.addLast(j);
j++;
}
}
remaining[pid] -= slice;
if (remaining[pid] == 0 && !finished[pid]) {
finishTime[pid] = time;
turnAroundTime[pid] = time - p.arrival;
normTurn[pid] = (double) turnAroundTime[pid] / p.service;
finished[pid] = true;
} else {
readyQueue.addLast(pid);
}
} else {
time++; // idle cycle
}
}
fillInWaitTime();
}
static void execute(Algorithm algo) {
if (operation.equals(TRACE)) {
System.out.println("\nRunning " + (algo.id == '1' ? "FCFS" : "RR-" + algo.quantum));
}
switch (algo.id) {
case '1':
runFCFS();
break;
case '2':
runRoundRobin(algo.quantum);
break;
}
printTimeline();
printStatistics();
}
static void printTimeline() {
System.out.println("\nTimeline:");
for (int t = 0; t < lastInstant; t++) System.out.print(t % 10 + " ");
System.out.println("\n--------------------------------------------");
for (int i = 0; i < processCount; i++) {
System.out.print(processes.get(i).name + " |");
for (int t = 0; t < lastInstant; t++) {
System.out.print(timeline[t][i] + "|");
}
System.out.println();
}
System.out.println("--------------------------------------------");
}
static void printStatistics() {
System.out.println("\nStatistics:");
for (int i = 0; i < processCount; i++) {
System.out.printf("Process %s - Finish: %d, Turnaround: %d, Normalized: %.2f\n",
processes.get(i).name, finishTime[i], turnAroundTime[i], normTurn[i]);
}
}
public static void main(String[] args) {
operation = TRACE;
processes.add(new Process("P1", 0, 5, 1));
processes.add(new Process("P2", 1, 3, 2));
processes.add(new Process("P3", 2, 8, 1));
processCount = processes.size();
initializeStats();
algorithms.add(new Algorithm('1', 0)); // FCFS
algorithms.add(new Algorithm('2', 2)); // Round Robin with quantum 2
for (Algorithm algo : algorithms) {
clearTimeline();
execute(algo);
}
}
}