Main function 1.0.

This commit is contained in:
rhinemann 2025-04-01 14:10:01 +03:00
parent 5de65255e1
commit 11382ccddf
9 changed files with 55 additions and 38 deletions

View File

@ -2,10 +2,12 @@
using std::next;
Kernel::Kernel(const List<PhysicalPage> &free_pages, const List<PhysicalPage> &busy_pages)
: free_pages(free_pages), busy_pages(busy_pages), swapped_page(nullptr) {}
Kernel::Kernel(const unsigned int RunQ_max_length, const List<PhysicalPage> &free_pages, const List<PhysicalPage> &busy_pages)
: free_pages(free_pages), busy_pages(busy_pages), swapped_page(nullptr) {
RunQ.reserve(RunQ_max_length);
}
void Kernel::page_fault(PageTable page_table, const int idx) {
void Kernel::page_fault(PageTable *page_table, const unsigned int idx) {
PhysicalPage *page = nullptr;
if (!this->free_pages.is_empty()) {
page = free_pages.get_head()->prev;
@ -25,10 +27,10 @@ void Kernel::page_fault(PageTable page_table, const int idx) {
}
if (page) {
page_table[idx].PPN = page->PPN;
page_table[idx].P = true;
page_table[idx].R = false;
page_table[idx].M = false;
(*page_table)[idx].PPN = page->PPN;
(*page_table)[idx].P = true;
(*page_table)[idx].R = false;
(*page_table)[idx].M = false;
}
}

View File

@ -13,8 +13,8 @@ public:
List<PhysicalPage> free_pages, busy_pages;
PhysicalPage *swapped_page;
std::vector<Process> RunQ;
Kernel(const List<PhysicalPage> &free_pages, const List<PhysicalPage> &busy_pages);
void page_fault(PageTable page_table, int idx);
Kernel(unsigned int RunQ_max_length, const List<PhysicalPage> &free_pages, const List<PhysicalPage> &busy_pages);
void page_fault(PageTable *page_table, unsigned int idx);
void stat_update();
};

10
MMU.cpp
View File

@ -1,10 +1,10 @@
#include "MMU.h"
void MMU::access(Kernel kernel, PageTable table, const int index, const AccessType type) {
if (!table[index].P)
kernel.page_fault(table, index);
table[index].R = true;
void MMU::access(Kernel *kernel, PageTable *table, const unsigned int index, const AccessType type) {
if ((*table)[index].P)
kernel->page_fault(table, index);
(*table)[index].R = true;
if (type == WRITE)
table[index].M = true;
(*table)[index].M = true;
}

2
MMU.h
View File

@ -11,7 +11,7 @@ enum AccessType {
class MMU {
public:
static void access(Kernel kernel, PageTable table, int index, AccessType type);
static void access(Kernel *kernel, PageTable *table, unsigned int index, AccessType type);
};

View File

@ -1,5 +1,3 @@
//
// Created by rhinemann on 10.03.25.
//
#include "PTE.h"
PTE::PTE() : P(false), R(false), M(false), PPN(0) {}

7
PTE.h
View File

@ -6,9 +6,10 @@
#define PTE_H
class PTE {
public:
bool P, R, M;
unsigned int PPN;
public:
bool P, R, M;
unsigned int PPN;
PTE();
};
#endif //PTE_H

View File

@ -1,5 +1,12 @@
#include "PageTable.h"
PageTable::PageTable(const unsigned int entry_num) {
entries.reserve(entry_num);
for (int i = 0; i < entry_num; ++i) {
entries.emplace_back();
}
}
PTE &PageTable::operator[](const unsigned int index) {
return this->entries.at(index);
}

View File

@ -6,9 +6,11 @@
#include "PTE.h"
class PageTable {
public:
std::vector<PTE> entries;
PTE &operator[](unsigned int index);
public:
std::vector<PTE> entries;
PTE &operator[](unsigned int index);
explicit PageTable(unsigned int entry_num);
};
#endif //PAGETABLE_H

View File

@ -2,6 +2,7 @@
#include <random>
#include "Kernel.h"
#include "MMU.h"
static int MAX_PROC = 10;
static int PAGE_N = 10;
@ -9,27 +10,33 @@ static int PAGE_N = 10;
int main() {
std::random_device rd;
std::mt19937 gen(rd());
std::uniform_int_distribution<unsigned int> distribution(0, 10);
std::uniform_int_distribution<unsigned int> distribution(500, 600), access_type_distr(1, 10);
List<PhysicalPage> free_pages, busy_pages;
Kernel kernel(free_pages, busy_pages);
Kernel kernel(MAX_PROC, free_pages, busy_pages);
for (int i = 0; i < PAGE_N; ++i) {
free_pages.insert_tail(new PhysicalPage(i));
for (int i = 1; i < PAGE_N; ++i) {
free_pages.insert_head(new PhysicalPage(i));
}
for (int i = 0; i < MAX_PROC; ++i) {
kernel.RunQ.emplace_back(nullptr, distribution(gen));
for (int i = 0; i < kernel.RunQ.capacity(); ++i) {
kernel.RunQ.emplace_back(new PageTable(distribution(gen)), distribution(gen));
}
while (!kernel.RunQ.empty()) {
for (int i = 0; i < 10000; ++i) {
if (kernel.RunQ.empty()) {
break;
}
for (int j = 0; j < 30; ++j) {
const AccessType type = access_type_distr(gen) == 1 ? WRITE : READ;
constexpr unsigned int index = 1;
MMU::access(&kernel, kernel.RunQ.at(0).pageTable, index, type);
}
++kernel.RunQ.at(0).elapsedTime;
if (kernel.RunQ.at(0).is_finished()) {
kernel.RunQ.erase(kernel.RunQ.begin());
}
}
for (const auto process : kernel.RunQ)
std::cout << process.executionTime << std::endl;
std::cout << MAX_PROC << std::endl;
return 0;
}