-
Notifications
You must be signed in to change notification settings - Fork 0
/
Structure.java
321 lines (301 loc) · 11.7 KB
/
Structure.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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package GUI;
import java.util.ArrayList;
import javax.swing.JTextArea;
import javax.swing.JTextField;
/**
*
* @author schoenjj
*/
public class Structure {
//variables
ClockStarter clock;
private ArrayList<Process> list;
private ArrayList<Process> newList;
private ArrayList<Process> readyList;
private ArrayList<Process> terminatedList;
private ArrayList<Process> waitingList;
private Process runningProcess;
private JTextArea inputData;
private JTextArea outputData;
private JTextField clockField;
String[] str;
String[] temp;
int cpuTimer;
int iOTimer;
private ArrayList<String> traceTape;
int timeQuantum;
private ArrayList<Process> runningList;
boolean runningToReady;
boolean waitingToReady;
boolean runningToWaiting;
/**
* The Structure constructor uses the clockField from the main frame to use
* throughout this class.
*
* @param clockField
*/
public Structure(JTextField clockField) {
list = new ArrayList<>();
this.clockField = clockField;
clock = new ClockStarter(clockField, this);
newList = new ArrayList<>();
readyList = new ArrayList<>();
terminatedList = new ArrayList<>();
waitingList = new ArrayList<>();
traceTape = new ArrayList<>();
runningList = new ArrayList<>(1);
runningToReady = false;
}
/**
* The readData method splits the input from the text area by each new line,
* then fills the list with processes.
*
* @param inputData
* @param outputData
* @param clockField
*/
public void readData(JTextArea inputData, JTextArea outputData, JTextField clockField) {
cpuTimer = 0;
traceTape.clear();
list.clear();
newList.clear();
readyList.clear();
runningProcess = null;
waitingList.clear();
runningList.clear();
terminatedList.clear();
clock.setTime(0);
clockField.setText("0");
try {
if (inputData.getText().isEmpty()) {
outputData.append("enter some data\n");
} else {
str = inputData.getText().split("\n");
timeQuantum = Integer.parseInt(str[0]);
for (int i = 1; i < str.length; i = i + 3) {
if (isInteger(str[i]) == false) {
outputData.append("creation time must be an integer\n");
} else if (str[i + 1].length() > 3) {
outputData.append("state cannot be more than 3 letters\n");
} else {
list.add(new Process(Integer.parseInt(str[i]), "unknown", str[i + 1], str[i + 2]));
}
}
outputData.append("data read\n");
}
} catch (Exception e) {
}
}
/**
* showStatus() displays all the process and state information of the program
*
* @param outputData
* @param clockField
* @param inputData
*/
public void showStatus(JTextArea outputData, JTextField clockField, JTextArea inputData) {
outputData.append("clock -- " + clockField.getText() + "\n");
outputData.append("time quantum -- " + str[0] + "\n");
outputData.append("processList.size() -- " + list.size() + "\n");
for (int m = 0; m < list.size(); m++) {
outputData.append(" " + list.get(m) + "\n");
}
//display the new list
outputData.append("newList.size() -- " + newList.size() + "\n");
for (int q = 0; q < newList.size(); q++) {
outputData.append(" " + newList.get(q) + "\n");
}
//display the ready list
outputData.append("readyList.size() -- " + readyList.size() + "\n");
for (int y = 0; y < readyList.size(); y++) {
outputData.append(" " + readyList.get(y).changingString() + "\n");
}
//display the running list
if(!runningList.isEmpty()){
outputData.append("runningProcess -- " + runningList.get(0).changingString() + "\n");
outputData.append(" (currentTimeQuantumRemaining) -- " + timeQuantum+"\n");
}
else
outputData.append("runningProcess -- " + runningProcess + "\n");
//display the waiting list
outputData.append("waitingList.size() -- " + waitingList.size() + "\n");
for (int y = 0; y < waitingList.size(); y++) {
outputData.append(" " + waitingList.get(y).changingString() + "\n");
}
//display the terminated list
outputData.append("terminatedList.size() -- " + terminatedList.size() + "\n");
for (int y = 0; y < terminatedList.size(); y++) {
outputData.append(" " + terminatedList.get(y).changingString() + "\n");
}
outputData.append("\n");
}
/**
* processStuff() calls all the methods which moves processes around states
*/
public void processStuff(){
//check the waitingToReady boolean to see if a process has been sent from the waitingList to readyList
if(waitingToReady == true){
}
else if(waitingToReady == false && runningToWaiting == false){
processWaitingList();
}
//add new processes from the process list to the newList
addNewProcesses();
//if the runningList is not empty, process the running list
if(!runningList.isEmpty()){
processRunningList();
}
//check the runningToReady boolean to see if a process has been sent from running back to ready. if it's true, wait one more tick, if not, add it to running
if(runningToReady == true){
}
else if(runningToReady == false && waitingToReady == false){
addToRunning();
}
//if the newList is empty, call newToReady() which will add processes to the ready list from the newList
if (!newList.isEmpty()) {
newToReady();
}
//newToReady();
//set all the booleans to false so they will run unless set otherwise set
runningToReady = false;
waitingToReady = false;
runningToWaiting = false;
}
/**
* isInteger checks if an input variable is an integer and returns true if it is or false if it's not an integer
* @param input
* @return
*/
public boolean isInteger( String input ) {
try {
Integer.parseInt(input);
return true;
} catch (Exception e) {
return false;
}
}
/**
* add processes to the new list
*/
public void addNewProcesses(){
for (int i = 0; i < list.size(); i++) {
if (list.get(i).getCreationTime() == clock.getCurrentTime()) {
newList.add(list.get(i));
}
}
}
/**
* add processes from new to ready and remove the process from the new list
*/
public void newToReady(){
for (int i = 0; i < newList.size(); i++) {
if (newList.get(i).getCreationTime() + 1 <= Integer.parseInt(clockField.getText())) {
readyList.add(newList.get(i));
newList.remove(i);
}
}
}
/**
* add processes to the running process if the running process is null
*/
public void addToRunning(){
if (readyList.size() > 0 && runningList.isEmpty()) {
runningList.add(readyList.get(0));
readyList.remove(0);
temp = runningList.get(0).getNewTape().split(" ");
cpuTimer = Integer.parseInt(temp[1]);
}
}
/**
* checks the trace tape of the process and moves it to the appropriate list
*/
public void processRunningList(){
if(cpuTimer == 0 && timeQuantum >0){
if(runningList.get(0).checkForIO()== true){
timeQuantum = Integer.parseInt(str[0]);
runningList.get(0).removeFirstTwoFromNewTape();
waitingList.add(runningList.get(0));
runningList.remove(0);
runningToWaiting = true;
waitingListTimer();
}
else if(runningList.get(0).checkForIO() == false){
timeQuantum = Integer.parseInt(str[0]);
terminatedList.add(runningList.get(0));
runningList.get(0).removeFirstTwoFromNewTape();
runningList.remove(0);
}
}
else if(timeQuantum == 0 && cpuTimer > 0){
timeQuantum = Integer.parseInt(str[0]);
runningList.get(0).setNewTape(runningList.get(0).getNewTape());
readyList.add(runningList.get(0));
runningList.remove(0);
runningToReady = true;
}
else if(timeQuantum == 0 && cpuTimer ==0){
if(runningList.get(0).checkForIO() == true){
timeQuantum = Integer.parseInt(str[0]);
runningList.get(0).removeFirstTwoFromNewTape();
waitingList.add(runningList.get(0));
runningList.remove(0);
runningToWaiting = true;
waitingListTimer();
}
else if(runningList.get(0).checkForIO() == false){
timeQuantum = Integer.parseInt(str[0]);
terminatedList.add(runningList.get(0));
runningList.get(0).removeFirstTwoFromNewTape();
runningList.remove(0);
}
}
else {
timeQuantum--;
cpuTimer--;
runningList.get(0).setCPUTimer(cpuTimer);
System.out.println("running List: "+runningList.get(0).changingString());
}
}
/**
* waitingListTimer sets the waiting list timer
*/
public void waitingListTimer(){
String[] waitingTemp;
waitingTemp = waitingList.get(0).getNewTape().split(" ");
System.out.println("waitingList: "+waitingList.get(0).changingString());
iOTimer = Integer.parseInt(waitingTemp[1]);
System.out.println("iotimer: "+iOTimer);
}
/**
* processWaitingList() processes the waiting list, decrements the IOTimer, and moves processes where they need to be moved to
*/
public void processWaitingList(){
for (int i = 0; i < waitingList.size(); i++) {
if (iOTimer == 0) {
if(waitingList.get(i).checkForIO() == true){
waitingList.get(i).removeFirstTwoFromNewTape();
readyList.add(waitingList.get(i));
waitingList.remove(i);
waitingToReady = true;
}
else if(waitingList.get(i).checkForIO() == false){
waitingList.get(i).removeFirstTwoFromNewTape();
terminatedList.add(waitingList.get(i));
waitingList.remove(i);
}
}
else{
iOTimer--;
waitingList.get(i).setIOTimer(iOTimer);
System.out.println("iOTimer: "+iOTimer);
}
}
}
public ClockStarter getClock(){
return clock;
}
}