- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOS.java
More file actions
Latest commit
492 lines (383 loc) · 13.1 KB
/
Copy pathOS.java
File metadata and controls
492 lines (383 loc) · 13.1 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
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
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
importjava.util.ArrayList;
importjava.util.Optional;
publicclassOS {
privatestaticKernelkernel; // A reference to the Kernel.
publicstaticCallTypecurrentCall; // The current call to the Kernel.
publicstaticArrayList<Object> parameters; // The Parameters for the Kernel Call.
publicstaticObjectretval; // The return value from the Kernel.
publicenumCallType{
ALLOCATE, CLOSE, CREATE, FREE, SEARCHPID, GETPID, GET_MAPPING, OPEN, READ, SEEK, SEND_MESSAGE, SLEEP, SWITCH, WAIT_MESSAGE, WRITE
}
publicenumPriority{
REALTIME, INTERACTIVE, BACKGROUND
}
// Init Methods:
/**
* Starts the initial process of the OS.
*
* @param init The first process to run.
*/
publicstaticvoidstartUp(UserLandProcessinit){
dbMes("OS: StartUp");
kernel = newKernel();
parameters = newArrayList<Object>();
retval = newObject();
createProcess(init);
createProcess(newIdleProcess(), Priority.BACKGROUND);
}
// Kernel Calls:
/**
* Adds a process to the scheduler.
*
* @param up The process to be added.
* @return The PID of the new process.
*/
publicstaticOptional<Integer> createProcess(UserLandProcessup) {
dbMes("OS: Creating new Process: " + up.getClass());
// Reset the parameters
parameters.clear();
parameters.add(up);
parameters.add(Priority.INTERACTIVE);
// Set currentCall
currentCall = CallType.CREATE;
// Switch to Kernal
switchToKernel();
while(true){ // The processes are async, so this will sometimes run before Kernel can update it.
try{
return (Optional<Integer>) retval;
} catch (Exceptione){
try{
Thread.sleep(5);
} catch(Exceptionex){}
}
}
}
/**
* Adds a process to the scheduler.
*
* @param up The process to be added.
* @param priority The priority of the new process.
* @return The PID of the new Process.
*/
publicstaticOptional<Integer> createProcess(UserLandProcessup, Prioritypriority){
dbMes("OS: Creating new Process: " + up.getClass() + " Priority: " + priority.toString());
parameters.clear();
parameters.add(up);
parameters.add(priority);
currentCall = CallType.CREATE;
switchToKernel();
while(true){ // The processes are async, so this will sometimes run before Kernel can update it.
try{
return (Optional<Integer>) retval;
} catch (Exceptione){
try{
Thread.sleep(5);
} catch(Exceptionex){}
}
}
}
/**
* Switches to the next process in the queue.
*/
publicstaticvoidswitchProcess(){
dbMes("OS: Switching process");
retval = null;
parameters.clear();
currentCall = CallType.SWITCH;
switchToKernel();
}
/**
* Pauses the currentlyRunning process and doesn't put it back in the queue unitl after the specified time has elapsed.
*
* @param milliseconds The amount of time that the process wants to sleep for.
*/
publicstaticvoidsleep(intmilliseconds){
dbMes("OS: Sleep");
retval = null;
parameters.clear();
parameters.add(milliseconds);
currentCall = CallType.SLEEP;
switchToKernel();
}
/**
* Opens the desired device and returns the File IDentification number (FID) of the device.
*
* @param device
* @return The FID of the opened device, or -1 if it fails.
*/
publicstaticOptional<Integer> open(Stringdevice){
OS.dbMes("OS: Opening " + device);
parameters.clear();
parameters.add(device);
currentCall = CallType.OPEN;
switchToKernel();
while(true){ // The processes are async, so this will sometimes run before Kernel can update it.
try{
return (Optional<Integer>) retval;
} catch (Exceptione){
try{
Thread.sleep(5);
} catch(Exceptionex){}
}
}
}
/**
* Reads data from the specifed device.
*
* @param FID The File ID of the device to be read from.
* @param size The amount of data in bytes to be read.
* @return
*/
publicstaticOptional<byte[]> read(intFID, intsize){
OS.dbMes("OS: Reading " + FID + " Size: " + size);
parameters.clear();
parameters.add(FID);
parameters.add(size);
currentCall = CallType.READ;
switchToKernel();
while(true){ // The processes are async, so this will sometimes run before Kernel can update it.
try{
return (Optional<byte[]>) retval;
} catch (Exceptione){
try{
Thread.sleep(5);
} catch(Exceptionex){}
}
}
}
/**
* Moves the curser within the specified device.
*
* @param FID The File ID of the device to be seeked.
* @param size The distance to be seeked
*/
publicstaticvoidseek(intFID, intsize){
OS.dbMes("OS: Seeking FID: " + FID + " Size: " + size);
parameters.clear();
parameters.add(FID);
parameters.add(size);
currentCall = CallType.SEEK;
switchToKernel();
}
/**
* Writes information to the specifed device.
*
* @param FID The File ID of the device being writen to.
* @param data The data passed to the device.
* @return The amount of data written to the device.
*/
publicstaticOptional<Integer> write(intFID, byte[] data){
OS.dbMes("OS: Writing to " + FID + " Data: " + data);
parameters.clear();
parameters.add(FID);
parameters.add(data);
currentCall = CallType.WRITE;
switchToKernel();
while(true){ // The processes are async, so this will sometimes run before Kernel can update it.
try{
return (Optional<Integer>) retval;
} catch (Exceptione){
try{
Thread.sleep(5);
} catch(Exceptionex){}
}
}
}
/**
* Closes the device specified by the FID.
*
* @param FID The File ID of the device to be closed.
*/
publicstaticvoidclose(intFID){
OS.dbMes("OS: Closing " + FID);
parameters.clear();
parameters.add(FID);
currentCall = CallType.CLOSE;
switchToKernel();
}
/**
* Gets the current process's PID.
*
* @return The PID of the process.
*/
publicstaticintgetPID(){
OS.dbMes("OS: Get PID");
parameters.clear();
currentCall = CallType.GETPID;
switchToKernel();
while(true){ // The processes are async, so this will sometimes run before Kernel can update it.
try{
return (int) retval;
} catch (Exceptione){
try{
Thread.sleep(5);
}
catch(InterruptedExceptionex){}
}
}
}
/**
* Gets the PID of the named process.
*
* @param name The name of the desired process.
* @return The PID of the named process, or -1 on failure.
*/
publicstaticOptional<Integer> searchPID(Stringname){
OS.dbMes("OS: Get PID of \"" + name + "\"");
parameters.clear();
parameters.add(name);
currentCall = CallType.SEARCHPID;
switchToKernel();
while(true){ // The processes are async, so this will sometimes run before Kernel can update it.
try{
return (Optional<Integer>) retval;
} catch (Exceptione){
try{
Thread.sleep(5);
}
catch(Exceptionex){
}
}
}
}
/**
* Sends a message to the desired process.
*
* @param message The message to be sent.
*/
publicstaticvoidsendMessage(Messagemessage){
dbMes("OS: Send message: " + newString(message.getData()));
parameters.clear();
parameters.add(message);
currentCall = CallType.SEND_MESSAGE;
switchToKernel();
}
/**
* Pauses the process until it recives a message.
*
* @return The most recent messge sent to the process.
*/
publicstaticMessagewaitForMessage(){
dbMes("OS: Wait Message");
parameters.clear();
currentCall = CallType.WAIT_MESSAGE;
switchToKernel();
while(true){ // The processes are async, so this will sometimes run before Kernel can update it.
try{
if(retval != null)
return (Message) retval;
} catch (Exceptione){
try{
Thread.sleep(5);
}
catch(Exceptionex){
}
}
}
}
/**
* Gets the mapping for the provided virutal page number, and stores it into the tlp.
*
* @param virtualPageNum The virtual page number of the desired memory or -1 on failure.
*/
publicstaticvoidgetMapping(intvirtualPageNum){
dbMes("OS: Get Mapping");
parameters.clear();
parameters.add(virtualPageNum);
currentCall = CallType.GET_MAPPING;
switchToKernel();
}
/**
* Allocates memory.
*
* @param size The amount of bytes in memory to be allocated.
* @return The first pointer in the allocated memory.
*/
publicstaticOptional<Integer> allocateMemory(intsize){
dbMes("OS: Allocate Memory");
if(size % UserLandProcess.PAGE_SIZE != 0){
dbMes("OS: Size " + size + " is not a multiple of " + UserLandProcess.PAGE_SIZE + ".");
returnOptional.empty();
}
// Change to number of pages added because we don't support removing anything smaller anyway.
size = size / UserLandProcess.PAGE_SIZE;
parameters.clear();
parameters.add(size);
currentCall = CallType.ALLOCATE;
switchToKernel();
while(true){ // The processes are async, so this will sometimes run before Kernel can update it.
try{
if(retval != null)
return (Optional<Integer>) retval;
} catch (Exceptione){
try{
Thread.sleep(5);
}
catch(Exceptionex){
}
}
}
}
/**
* Frees the specified page in memory.
*
* @param pointer The start of the addresses in memory to be freed.
* @param size The amound of memory in bytes to be freed in memory.
*
* @return True if the freeing was successfull. False otherwize.
*/
publicstaticbooleanfreeMemory(intpointer, intsize){
dbMes("OS: Allocate Memory");
if(pointer % UserLandProcess.PAGE_SIZE !=0){
dbMes("OS: Size " + pointer + " is not a multiple of " + UserLandProcess.PAGE_SIZE + ".");
returnfalse;
}
if(size % UserLandProcess.PAGE_SIZE !=0){
dbMes("OS: Size " + size + " is not a multiple of " + UserLandProcess.PAGE_SIZE + ".");
returnfalse;
}
// Change to page number because we don't support removeing anything smaller than a page.
pointer = pointer / UserLandProcess.PAGE_SIZE;
// Change to number of pages added because we don't support removing anything smaller anyway.
size = size / UserLandProcess.PAGE_SIZE;
parameters.clear();
parameters.add(pointer);
parameters.add(size);
currentCall = CallType.FREE;
switchToKernel();
while(true){ // The processes are async, so this will sometimes run before Kernel can update it.
try{
if(retval != null)
return (boolean) retval;
} catch (Exceptione){
try{
Thread.sleep(5);
}
catch(Exceptionex){
}
}
}
}
// Helper Methods:
/**
* Starts the Kernel and stops the current process.
*/
privatestaticvoidswitchToKernel(){
dbMes("OS: Switching to kernel");
// Store the process here in case the kernel thread changes currentlyRunning before we can call stop.
PCBstopMe = kernel.getCurrentlyRunning();
kernel.start();
if(stopMe != null)
stopMe.stop();
//kernel.stopCurrentProcesss();
}
// Debugging Help:
/**
* EXTRA METHOD!!! Prints a message to the terminal to help bebugging.
*
* @param message The message printed to the terminal.
*/
publicstaticvoiddbMes(Stringmessage){
System.err.println(" ||"+message);
}
}