Repository navigation
Expand file tree
/
Copy pathMemory.cpp
More file actions
258 lines (218 loc) · 7.45 KB
/
Copy pathMemory.cpp
File metadata and controls
258 lines (218 loc) · 7.45 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
#include <iostream>
#include <algorithm>
#include <vector>
#include "CPU.h"
#include "Memory.h"
using namespace std;
Memory::Memory()
{
address = 0;
size = 0;
// Initialize virtual page table with empty pages
virtual_page_table.resize(virtual_page_count);
for (int i = 0; i < virtual_page_count; ++i) {
virtual_page_table[i].resize(1); // Each virtual page can hold one process
virtual_page_table[i][0].IsValid = false; // Mark all pages as invalid initially
}
}
// Destructor for the Memory class.
Memory::~Memory()
{
// Calculate the number of physical pages needed.
size_t physicalPagesNeeded = physical_memory.size() / page_size;
// Resize the virtual page table to accommodate the calculated physical pages.
virtual_page_table.resize(physicalPagesNeeded);
// Loop through the virtual page table.
for (int i = 0; i < virtual_page_table.size(); i++)
{
// Loop through each entry in the virtual page table.
for (int j = 0; j < virtual_page_table[i].size(); j++)
{
// Create a new Page object
Page page;
// Set the flags of the page.
virtual_page_table[i][j].IsDirty = false;
virtual_page_table[i][j].IsValid = true;
// Set the process_id of the page.
page.process_id = 0;
// Add the page to the virtual page table
virtual_page_table[i].push_back(page);
}
}
}
void Memory::schedule_process(const PCB& process)
{
//push the process into the queue
readyQueue.push(process);
//move the process to a temp vector to sort through
vector<PCB> temp;
while (!readyQueue.empty()) {
temp.push_back(readyQueue.front());
readyQueue.pop();
}
//sort the queue based on priority
sort(temp.begin(), temp.end(), [](const PCB& a, const PCB& b)
{
return a.Priority > b.Priority;
});
//transfer the elements back to readyQueue
for (auto& PCB : temp) {
readyQueue.push(PCB);
}
}
void Memory::writeMemory(int address, int value)
{
if (address < 0 || address >= physical_memory.size())
{
std::cerr << "error" << std::endl;
}
int page = (address >> 8) & 0xFFFFFF; // Extract the 24-bit page
int offset = address & 0xFF; // Extract the 8-bit offset
//look up the specific memory address
int physical_address = page * page_size + offset;
//set the address to the value
physical_memory[physical_address] = value;
}
int Memory::readMemory(int address) const
{
//check if addres is wihtin in memory
if (address < 0 || address >= physical_memory.size())
{
std::cerr << "error" << std::endl;
return -1; //returns the error state
}
int page = (address >> 8) & 0xFFFFFF; // Extract the 24-bit page
int offset = address & 0xFF; // Extract the 8-bit offset
//look up the specific memory address
int physical_address = page * page_size + offset;
return physical_memory[physical_address];
}
void Memory::print_stats(const PCB& process) const
{
cout << "Process ID: " << process.ProcessID << endl;
cout << "Page number: " << page_num << endl;
cout << "Number of context switches: " << context_switch << endl;
}
// Function to load program into memory for a given process
void Memory::load_program(const PCB& process, const std::vector<int>& program)
{
size_t newIndexPage = 0;
// Calculate the number of pages required for the program
size_t num_pages_needed = (program.size() + page_size - 1) / page_size;
}
void Memory::execute_processes()
{
while (!readyQueue.empty())
{
PCB currentProcess = readyQueue.front();
readyQueue.pop();
//check if time has expired
if (currentProcess.QuantumTime > 0)
{
currentProcess.QuantumTime--;
if (currentProcess.QuantumTime == 0)
{
contextSwitch();
continue; //skip to the next process
}
}
if (currentProcess.SleepCounter == WaitingSleep)
{
currentProcess.SleepCounter--;
if (currentProcess.SleepCounter == 0)
{
//if sleep counter reaches 0, then ready the process
currentProcess.state = Ready;
schedule_process(currentProcess);
continue;
}
}
//check if the process has terminated
if (currentProcess.state == Terminated) {
continue; //skip to next process
}
}
}
void Memory::contextSwitch()
{
//save the current process
PCB currentProcess = readyQueue.front();
readyQueue.pop();
//load the next process from the PCB
PCB nextProcess = readyQueue.front();
//perfrom context switches by swapping the states
swap(currentProcess.state, nextProcess.state);
swap(currentProcess.QuantumTime, nextProcess.QuantumTime);
swap(currentProcess.SleepCounter, nextProcess.SleepCounter);
// Push the current process back into the ready queue
readyQueue.push(currentProcess);
}
void Memory::unload_process()
{
//checks if the queue is empty. if not then remove the process
while (!readyQueue.empty())
{
PCB processID = readyQueue.front();
if (readyQueue.front().ProcessID)
{
readyQueue.pop();
}
}
}
void Memory::allocateHeapMemory(int rx, int ry)
{
size_t size = static_cast<int>(rx);
Page page;
HeapSegment allocateMemory = HeapPages[address / page_size];
allocateMemory.allocated = true; //set the allocation to true
allocateMemory.size = size; //update the size
if (allocateMemory.allocated)
{
//Page is in physical memory
page.IsDirty = true; //Mark page as dirty
cout << "Successfully allocated memory" << size << " bytes at " << address << endl;
}
else
{
//mark the page not dirty when allocation fails
cerr << "Error: allocation failed" << endl;
page.IsDirty = false;
}
}
void Memory::freeHeapMemory(size_t ry)
{
HeapSegment deallocateMemory = HeapPages[address / page_size];
deallocateMemory.allocated = false;
deallocateMemory.size = 0; //update the size to 0
cout << "Memory has been freed from the heap" << address << endl;
}
// Accesses memory for a given process with read or write operation.
void Memory::accessMemory(int processID, int address, bool isWriteOperation)
{
// Calculate the page number and offset within the page.
int pageNum = address / page_size;
int offset = address % page_size;
// Reference to the page in the virtual page table.
Page& page = virtual_page_table[processID][pageNum];
// Check if the page is valid.
if (page.IsValid)
{
// If it's a write operation, mark the page as dirty.
if (isWriteOperation)
{
page.IsDirty = true;
}
// If it's a read operation, invalidate the page.
else
{
page.IsValid = false;
}
}
// If the page is invalid.
else
{
// Set the page as valid and not dirty.
virtual_page_table[processID][pageNum].IsValid = true;
virtual_page_table[processID][pageNum].IsDirty = false;
}
}