Repository navigation
Expand file tree
/
Copy pathMemoryManagement.java
More file actions
183 lines (172 loc) · 5.87 KB
/
Copy pathMemoryManagement.java
File metadata and controls
183 lines (172 loc) · 5.87 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
import java.util.BitSet;
import java.util.Random;
public class MemoryManagement implements MemoryInterface
{
private static MemoryManagement memoryManagement = null;
public byte[][] physicalPages = new byte[1024][1024];
public BitSet freeList = new BitSet(1024);
public int tlbVirtual = -1;
public int tlbPhysical = -1;
private int previousMemory;
public int diskPage = 0;
public int disk;
private MemoryManagement()
{
disk = FakeFileSystem.getFileSystem().open("disk");
}
public static MemoryManagement getMemoryManagement()
{
if(memoryManagement == null)
{
memoryManagement = new MemoryManagement();
}
return memoryManagement;
}
public void writeMemory(int address, byte value) throws RescheduleException
{
int vPage = Math.round(address / 1024);
if(tlbVirtual != vPage)
mapTLB(address);
if(tlbPhysical == -1)
{
throw new RescheduleException();
}
else
{
OS.GetOS().scheduler.runningProcess.virtualMemory[tlbVirtual].isDirty = true;
int offset = address % 1024;
physicalPages[tlbPhysical][offset] = value;
}
}
public byte readMemory(int address) throws RescheduleException
{
int vPage = Math.round(address / 1024);
if(tlbVirtual != vPage)
mapTLB(address);
if(tlbPhysical == -1)
{
throw new RescheduleException();
}
else
{
int offset = address % 1024;
return physicalPages[tlbPhysical][offset];
}
}
public void mapTLB(int address) throws RescheduleException
{
try
{
tlbVirtual = Math.round(address / 1024);
VirtualToPhysicalMapping map = OS.GetOS().scheduler.runningProcess.virtualMemory[tlbVirtual];
if(map.physicalPage == -1) //Was promised a page, thanks for following the rules
{
//Has data written on disk, guess it was old school notes
if(map.diskPage != -1)
{
map.physicalPage = assignPage();
}
else
{
map.physicalPage = assignPage();
}
}
tlbPhysical = map.physicalPage;
}
catch(Exception e)
{
System.out.println(e.getMessage());
throw new RescheduleException();
}
}
public int stealMemory()
{
KernelandProcess process = null;
Random rand = new Random();
int maxRange = 0; //Used to determine size of virtual memory for random page selection
int virtual = 0;
int physicalPage = -1;
while(physicalPage == -1)
{
process = OS.GetOS().scheduler.getRandomProcess();
for(int i = 0; i < 1024 && process.virtualMemory[i] != null; i++)
{
maxRange++;
}
if(maxRange > 0)
{
virtual = rand.nextInt(maxRange);
if(process.virtualMemory[virtual] != null && process.processID != OS.GetOS().scheduler.runningProcess.processID)
physicalPage = process.virtualMemory[virtual].physicalPage;
}
maxRange = 0;
}
//Shows if the page is being written to disk
//System.out.println("isDirty: " + process.virtualMemory[virtual].isDirty);
if(process.virtualMemory[virtual].isDirty)
{
if(process.virtualMemory[virtual].diskPage == -1)
{
process.virtualMemory[virtual].diskPage = diskPage;
System.out.println(diskPage);
diskPage++;
}
FakeFileSystem.getFileSystem().seek(disk, process.virtualMemory[virtual].diskPage * 1024);
FakeFileSystem.getFileSystem().write(disk, physicalPages[physicalPage]);
process.virtualMemory[virtual].isDirty = false;
process.virtualMemory[virtual].physicalPage = -1;
}
for(int i = 0; i < physicalPages[process.virtualMemory[virtual].physicalPage].length; i++)
{
physicalPages[process.virtualMemory[virtual].physicalPage][i] = 0;
}
//Used to debug, shows which process is being stolen from, what process is running and what page is being stolen.
//System.out.println(process.processID + " , Running: " + OS.GetOS().scheduler.runningProcess.processID + "; Page: " + physicalPage);
return physicalPage;
}
public int assignPage()
{
int freeBit = freeList.nextClearBit(0);
if(freeBit < 1024)
{
freeList.set(freeBit);
return freeBit;
}
else
{
return stealMemory();
}
}
public int sbrk(int amount)
{
int result = previousMemory;
int pageAmount = (int)Math.ceil((float)amount / 1024);
previousMemory = pageAmount * 1024;
for(int i = 0; i < pageAmount; i++)
{
boolean spaceFound = false;
for(int virtuali = 0; virtuali < 1024 && spaceFound == false; virtuali++)
{
if(OS.GetOS().scheduler.runningProcess.virtualMemory[virtuali] == null)
{
OS.GetOS().scheduler.runningProcess.virtualMemory[virtuali] = new VirtualToPhysicalMapping();
spaceFound = true;
}
}
}
return result;
}
public void freeMemory(KernelandProcess process)
{
VirtualToPhysicalMapping[] vMemory = process.virtualMemory;
for(int i = 0; i < vMemory.length; i++)
{
if(vMemory[i] != null)
{
freeList.clear(vMemory[i].physicalPage);
}
}
tlbVirtual = -1;
tlbPhysical = -1;
}
}