-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCPU.java
More file actions
58 lines (48 loc) · 1.51 KB
/
CPU.java
File metadata and controls
58 lines (48 loc) · 1.51 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
import java.util.ArrayList;
public class CPU {
public static int clock = 0; // this should be incremented on every CPU cycle
private Scheduler scheduler;
private MMU mmu;
private Process[] processes;
private int currentProcess;
boolean flag = false;
public CPU(Scheduler scheduler, MMU mmu, Process[] processes) {
this.scheduler = scheduler;
this.mmu = mmu;
this.processes = processes;
}
int i=0;
public void run() {
/* TODO: you need to add some code here
* Hint: you need to run tick() in a loop, until there is nothing else to do... */
do
{
tick();
i++;
}
while (i<(processes.length));
}
public void tick() {
/* TODO: you need to add some code here
* Hint: this method should run once for every CPU cycle */
if(processes[i].getPCB().getState()==ProcessState.NEW)
{
if (mmu.loadProcessIntoRAM(processes[i]))
{
scheduler.addProcess(processes[i]);
processes[i].waitInBackground();
i--;
}
}
else
{
scheduler.getNextProcess();
processes[i].run();
processes[i].getWaitingTime();
processes[i].getResponseTime();
processes[i].getPCB().setState(ProcessState.TERMINATED,clock);
scheduler.removeProcess(processes[i]);
mmu.remove(processes[i]);
}
}
}