From 11382ccddf25c7fa733df0833b270ae56afaba9a Mon Sep 17 00:00:00 2001 From: rhinemann Date: Tue, 1 Apr 2025 14:10:01 +0300 Subject: [PATCH] Main function 1.0. --- Kernel.cpp | 16 +++++++++------- Kernel.h | 4 ++-- MMU.cpp | 10 +++++----- MMU.h | 2 +- PTE.cpp | 6 ++---- PTE.h | 7 ++++--- PageTable.cpp | 7 +++++++ PageTable.h | 8 +++++--- main.cpp | 33 ++++++++++++++++++++------------- 9 files changed, 55 insertions(+), 38 deletions(-) diff --git a/Kernel.cpp b/Kernel.cpp index 7d5e9e7..7335c4e 100644 --- a/Kernel.cpp +++ b/Kernel.cpp @@ -2,10 +2,12 @@ using std::next; -Kernel::Kernel(const List &free_pages, const List &busy_pages) -: free_pages(free_pages), busy_pages(busy_pages), swapped_page(nullptr) {} +Kernel::Kernel(const unsigned int RunQ_max_length, const List &free_pages, const List &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; } } diff --git a/Kernel.h b/Kernel.h index 7ae025a..0784bf5 100644 --- a/Kernel.h +++ b/Kernel.h @@ -13,8 +13,8 @@ public: List free_pages, busy_pages; PhysicalPage *swapped_page; std::vector RunQ; - Kernel(const List &free_pages, const List &busy_pages); - void page_fault(PageTable page_table, int idx); + Kernel(unsigned int RunQ_max_length, const List &free_pages, const List &busy_pages); + void page_fault(PageTable *page_table, unsigned int idx); void stat_update(); }; diff --git a/MMU.cpp b/MMU.cpp index 8cbe660..e175a86 100644 --- a/MMU.cpp +++ b/MMU.cpp @@ -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; } diff --git a/MMU.h b/MMU.h index 938b3ed..c9973fa 100644 --- a/MMU.h +++ b/MMU.h @@ -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); }; diff --git a/PTE.cpp b/PTE.cpp index a9c4f0f..f87ce37 100644 --- a/PTE.cpp +++ b/PTE.cpp @@ -1,5 +1,3 @@ -// -// Created by rhinemann on 10.03.25. -// - #include "PTE.h" + +PTE::PTE() : P(false), R(false), M(false), PPN(0) {} diff --git a/PTE.h b/PTE.h index 1c7e7bb..f7d43aa 100644 --- a/PTE.h +++ b/PTE.h @@ -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 diff --git a/PageTable.cpp b/PageTable.cpp index 4e07828..b454138 100644 --- a/PageTable.cpp +++ b/PageTable.cpp @@ -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); } diff --git a/PageTable.h b/PageTable.h index 691d506..6ba9170 100644 --- a/PageTable.h +++ b/PageTable.h @@ -6,9 +6,11 @@ #include "PTE.h" class PageTable { - public: - std::vector entries; - PTE &operator[](unsigned int index); +public: + std::vector entries; + PTE &operator[](unsigned int index); + explicit PageTable(unsigned int entry_num); + }; #endif //PAGETABLE_H diff --git a/main.cpp b/main.cpp index 1f08d03..4671899 100644 --- a/main.cpp +++ b/main.cpp @@ -2,6 +2,7 @@ #include #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 distribution(0, 10); + std::uniform_int_distribution distribution(500, 600), access_type_distr(1, 10); List 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; }