-
Notifications
You must be signed in to change notification settings - Fork 0
/
Process.java
309 lines (277 loc) · 7.3 KB
/
Process.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
import static java.lang.Math.ceil;
import java.util.List;
import java.util.ArrayList;
import java.util.concurrent.TimeUnit;
/**
*
* Process class for simulating process in memory. not real process are being
* used.
*
* @version 1.0 May 2018
* @author Amanda Camacho <[email protected]>, Benjamin Amos
*/
public class Process extends Thread {
private int pid;
private int executionTime;
private int size;
private String name;
private int status;
private int memoryTime; // Total memory time
private int totalTime; // Total memory it needs to finish.
private int runningTime; // Running time.
private List<Page> pageTable;
private int numberOfPages;
/**
* Constructor for process
*
* @param name Name that process will have
* @param size Size in MB that process will use
* @param pageSize Size in MB that of pages
*/
public Process(String name, int size, int pageSize) {
this.pid = (int) (Math.random() * 10000);
this.name = name;
this.size = size;
this.status = 3;
this.memoryTime = 0;
this.totalTime = (int) (Math.random() * 100);
this.runningTime = 0;
this.numberOfPages = (size % pageSize != 0) ? (int) (ceil(size / pageSize) + 1) : size / pageSize;
this.pageTable = new ArrayList<Page>();
new Thread(this, this.name);
}
/**
* Overrided process for Thread run(). Runs the process in memory and checks for
* update in status
*
*/
public void run() {
Boolean endTime = false;
while (true) {
try {
TimeUnit.SECONDS.sleep(1);
if (this.status == 3) {
totalTime--;
runningTime++;
memoryTime++;
if (totalTime <= 0) {
totalTime = 0;
endTime = true;
}
} else if (this.status == 2 || this.status == 1) {
memoryTime++;
}
if (endTime && totalTime == 0) {
this.status = 0;
break;
} // Same as Thread.sleep(5000);
} catch (InterruptedException e) {
System.out.println("interrupted.");
}
}
}
/**
* Retrieves total memory time
*
* @return int Process memory time
*/
public int getTotalTime() {
return this.totalTime;
}
/**
* Sets process memory time
*
* @param time Time that process will be in memory
*/
public void setTotalTime(int time) {
this.totalTime = time;
}
/**
* Retrieves process time in memory so far
*
* @return int Process time in memory at calling time
*/
public int getRunningTime() {
return this.runningTime;
}
/**
* Adds to process running time
*
*/
public void updateRunningTime() {
this.runningTime++;
}
/**
* Reduces process time left in memory
*
*/
public void updateTotalTime() {
this.totalTime--;
}
/**
* Retrieves process id
*
* @return int process id
*/
public int getPid() {
return this.pid;
}
/**
* Retrieves process lifetime in memory
*
* @return int Time that process will use memory
*/
public int executionTime() {
return this.executionTime;
}
/**
* Retrieves process size
*
* @return int Memory size in MB
*/
public int getSize() {
return this.size;
}
/**
* Retrieves process name
*
* @return String Process name
*/
public String name() {
return this.name;
}
/**
* Retrieves process status translated to text
*
* @return String process status
*/
public String status() {
if (this.status == 0) {
return "Finalizado";
} else if (this.status == 1) {
return "Listo";
} else if (this.status == 2) {
return "Bloqueado";
} else if (this.status == 3) {
return "Ejecucion";
}
return "";
}
/**
* Retrieves process status integer
*
* @return int Process status
*/
public int statusInt() {
return this.status;
}
/**
* Changes process status
*
* @param status Process status to be set
*/
public void setStatus(int status) {
this.status = status;
}
/**
* Synchronized method for killing process
*
*/
public synchronized void killProccess() {
if (this.status != 0) {
this.status = 0;
this.pageTable.clear();
}
return;
}
/**
* Retrieves process time in memory
*
* @return int Process time in memory
*/
public int memoryTime() {
return this.memoryTime;
}
/**
* Checks if process should be in memory
*
* @return true if process needs memory time, otherwise false
*/
public boolean isInMemory() {
return this.memoryTime > 0;
}
/**
* Retrieves number of pages holding the process
*
* @return int Number of pages where process is in
*/
public int getNumberOfPages() {
return this.numberOfPages;
}
/**
* Retrieves process status integer identifier
*
* @return int Process status integer
*/
public int getStatus() {
return status;
}
/**
* Retrieves a list containing all pages that holds the process
*
* @return List<Page> List of pages holding the process
*/
public List<Page> getPageTable() {
return this.pageTable;
}
/**
* Sets process page table
*
*/
public void setPageTable(List<Page> pageTable) {
this.pageTable = pageTable;
}
/**
* Adds page to process page table
*
* @param page New page that will hold some size of the process
*/
public void addPage(Page page) {
this.pageTable.add(page);
}
/**
* Sets process status integer identifier
*
* @param status
*/
public void setProcessStatus(int status) {
this.status = status;
}
/**
* Just for printing process info in a nice way
*
* @return String with process info
*/
public String toString() {
String stringName = this.pid + " Process " + this.name + ".\n";
String stringInfo = "In Memory: " + this.isInMemory() + ".\n";
String stringInfo2 = "Process Status: " + this.status + "\n";
String stringInfo3 = "Process Execution Time: " + this.executionTime + ".\n";
String stringInfo4 = "Process Memory Time Elapsed: " + this.memoryTime + ".\n";
String stringInfo5 = "Proccess Size: " + this.size + ".\n";
return stringName + stringInfo + stringInfo2 + stringInfo3 + stringInfo4 + stringInfo5;
}
/**
* Prints all pages that process is using
*
* @return String Pages being used by process
*/
public String printPages() {
String pages = "";
for (Page page : this.pageTable) {
pages += String.valueOf(page.id()) + " ";
}
return pages;
}
}