forked from Nada-Iftene/CPU-Scheduling-Simulation--Threads
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Algorithm.java
189 lines (141 loc) · 6.95 KB
/
Algorithm.java
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
189
import javax.swing.table.DefaultTableModel;
import java.util.ArrayList;
/*
*
*
*/
public class Algorithm {
ArrayList<ClassInputData> file;
public Algorithm(ArrayList<ClassInputData> file) {
this.file = file;
}
protected void shortJobFirstNoPreemptif(ArrayList<ClassInputData> file, DataVisualizationPanel frame, int quantum) {
int currentTime = 0;
int numberOfProcess = file.size();
ArrayList<ClassInputData> waitingList = new ArrayList<>();
DefaultTableModel processingModel = (DefaultTableModel) frame.processingTable.getModel();
while (numberOfProcess > 0) {
System.out.println("current time: " + currentTime);
System.out.println();
System.out.println("process filling phase:");
for (int i = 0; i < file.size(); i++) {
//if the processus are arriving
if (file.get(i).getTempArriver() <= currentTime && !file.get(i).isPassed()) {
System.out.println("the processus " + file.get(i).getName() + " was added to the waiting list");
waitingList.add(file.get(i));
file.get(i).setPassed(true);
processingModel.setValueAt("" + currentTime, i, 1);
}
}
System.out.println();
System.out.println("process working phase:");
if (waitingList.size() > 0) {
int processusToPass = 0;
int shortestTime = waitingList.get(0).getTempCpu();
for (int i = 0; i < waitingList.size(); i++) {
if (waitingList.get(i).getTempCpu() < shortestTime) {
processusToPass = i;
shortestTime = waitingList.get(i).getTempCpu();
}
}
currentTime += quantum;
System.out.println("processus " + waitingList.get(processusToPass).getName() + " is being processed...");
waitingList.get(processusToPass).processing(quantum);
if (waitingList.get(processusToPass).getTempCpu() <= 0) {
int indexOfProcessLeaving = 0;
while (waitingList.get(processusToPass).getName() != file.get(indexOfProcessLeaving).getName()) {
++indexOfProcessLeaving;
}
System.out.println("processus " + waitingList.get(processusToPass).getName() + " left the system");
file.get(processusToPass).setExitTime(currentTime);
waitingList.remove(processusToPass);
--numberOfProcess;
processingModel.setValueAt("" + currentTime, indexOfProcessLeaving, 2);
}
} else {
System.out.println("no Processus are waiting");
++currentTime;
}
//---------
System.out.println();
if (file.size() > 0) {
System.out.println("remaining processes: ");
boolean check = false;
for (int i = 0; i < file.size(); i++) {
if (!file.get(i).isPassed()) {
System.out.println("Processus " + file.get(i).getName());
check = true;
}
}
if (!check) {
System.out.println("No Proceses remained");
}
}
System.out.println();
}
}
protected void roundRobinNonPreemptif(ArrayList<ClassInputData> file, int quantum, DataVisualizationPanel frame) {
int currentTime = 0;
int numberOfProcess = file.size();
ArrayList<ClassInputData> waitingList = new ArrayList<>();
DefaultTableModel processingModel = (DefaultTableModel) frame.processingTable.getModel();
while (numberOfProcess > 0) {
System.out.println("current time: " + currentTime);
System.out.println();
System.out.println("process filling phase:");
for (int i = 0; i < file.size(); i++) {
//if the processus are arriving
if (file.get(i).getTempArriver() <= currentTime && !file.get(i).isPassed()) {
System.out.println("the processus " + file.get(i).getName() + " was added to the waiting list");
waitingList.add(file.get(i));
file.get(i).setPassed(true);
processingModel.setValueAt("" + currentTime, i, 1);
}
}
System.out.println();
System.out.println("process working phase:");
if (waitingList.size() > 0) {
currentTime += quantum;
System.out.println("processus " + waitingList.get(0).getName() + " is being processed...");
waitingList.get(0).processing(quantum);
if (waitingList.get(0).getTempCpu() > 0) {
ClassInputData p = waitingList.get(0);
//waitingList.add(waitingList.size() - 1, waitingList.get(0));
waitingList.remove(0);
waitingList.add(p);
System.out.println("processus " + waitingList.get(0).getName() + " is the head of the list");
System.out.println("processus " + waitingList.get(waitingList.size() - 1).getName() + " is the tail of the list");
} else {
System.out.println("processus " + waitingList.get(0).getName() + " has left the system");
--numberOfProcess;
int indexOfProcessLeaving = 0;
while (waitingList.get(0).getName() != file.get(indexOfProcessLeaving).getName()) {
++indexOfProcessLeaving;
}
processingModel.setValueAt("" + currentTime, indexOfProcessLeaving, 2);
file.get(0).setExitTime(currentTime);
waitingList.remove(0);
}
} else {
System.out.println("No processes available");
currentTime++;
}
//---------
System.out.println();
if (file.size() > 0) {
System.out.println("remaining processes: ");
boolean check = false;
for (int i = 0; i < file.size(); i++) {
if (!file.get(i).isPassed()) {
System.out.println("Processus " + file.get(i).getName());
check = true;
}
}
if (!check) {
System.out.println("No Proceses remained");
}
}
System.out.println();
}
}
}