/
PageTable.cs
134 lines (119 loc) · 3.89 KB
/
PageTable.cs
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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Project2_CS452
{
class PageTable
{
public List<PageTableEntry> entryList;
public int pid;
private VirtualMemory vm;
private PhysicalMemory pm;
private Process owningProcess;
public PageTable(int size, int _pid, VirtualMemory _vm, PhysicalMemory _pm, Process p)
{
pid = _pid;
entryList = new List<PageTableEntry>();
vm = _vm;
pm = _pm;
owningProcess = p;
for (int x = 0; x < 64; x++)
{
if(x < 16)
entryList.Add(new PageTableEntry(-1, true, false, -1, pid));
else if(x > 16 && x < 64)
entryList.Add(new PageTableEntry(-1, false, false, -1, pid));
}
}
public List<PageTableEntry> getEntryList()
{
return entryList;
}
public bool inPageTable(int frameId)
{
bool inTable = false;
foreach (PageTableEntry pte in entryList)
{
if (frameId == pte.getFrame())
{
inTable = true;
break;
}
}
return inTable;
}
public void addPage(int frameId, int step)
{
bool added = false;
for (int x = 0; x < 16; x++)
{
//an empty valid frame was found in page table
if (entryList[x].isEmpty())
{
entryList[x].setFrame(frameId);
added = true;
pm.addPage(entryList[x], owningProcess.getPid());
break;
}
}
if (added)
return;
//a valid page wasnt found, make room for the frame in valid page
int leasedUsed = 0;
int leasedUsedIndex = 0;
int index = 0;
foreach (PageTableEntry pte in entryList)
{
//find the page which was lease recently used
int timeDelta = step - pte.getLastModified();
if (timeDelta > leasedUsed)
{
leasedUsed = timeDelta;
leasedUsedIndex = index;
}
index++;
}
//save a copy of the frame being removed in next open space
//TODO: if the frame being removed was in memory, do proper computations
for (int x = 16; x < 64; x++)
{
if (entryList[x].isEmpty())
{
//copy page info to new frame
PageTableEntry pte = entryList[x];
pte.setFrame(entryList[index].getFrame());
pte.setModified(step);
pte.setResident(false);
pte.setValid(false);
break;
}
}
//overwrite the old valid page
PageTableEntry npte = entryList[index];
npte.setFrame(frameId);
npte.setModified(step);
pm.addPage(npte, owningProcess.getPid());
}
public void update()
{
//TODO
foreach (PageTableEntry pte in entryList)
{
int phyFrameId = pm.inFrame(pte);
bool acctuallyInMem = (phyFrameId != -1);
if (pte.isResident() != acctuallyInMem)
{
pte.setResident(!pte.isResident());
pte.setResident(false);
}
if (acctuallyInMem)
{
pte.setSecondary(phyFrameId);
pte.setResident(true);
}
}
}
}
}