Main function 2.0.

This commit is contained in:
rhinemann 2025-04-01 15:22:38 +03:00
parent 11382ccddf
commit e162468079
6 changed files with 47 additions and 21 deletions

View File

@ -27,6 +27,7 @@ void Kernel::page_fault(PageTable *page_table, const unsigned int idx) {
}
if (page) {
page->take_up(page_table, idx);
(*page_table)[idx].PPN = page->PPN;
(*page_table)[idx].P = true;
(*page_table)[idx].R = false;

14
List.h
View File

@ -45,20 +45,22 @@ void List<T>::insert_head(T *elem) {
}
this->head = elem;
++this->num;
++num;
}
template<typename T>
void List<T>::insert_tail(T *elem) {
if (!head)
if (!head) {
elem->next = elem->prev = elem;
head = elem;
}
else {
elem->next = head;
elem->prev = head->prev;
head->prev->next = elem;
head->prev = elem;
}
++this->num;
++num;
}
template<typename T>
@ -70,7 +72,7 @@ void List<T>::insert_before(T *elem, T *reference_point) {
this->head = elem;
else
elem->prev->next = elem;
++this->num;
++num;
}
template<typename T>
@ -81,7 +83,7 @@ void List<T>::remove(T *elem) {
elem->prev->next = elem->next;
if (this->head == elem)
this->head = elem->next;
--this->num;
--num;
}
template<typename T>
@ -91,7 +93,7 @@ bool List<T>::is_empty() {
template<typename T>
unsigned int List<T>::get_num() const {
return this->num;
return num;
}
#endif //LIST_H

4
PTE.h
View File

@ -1,7 +1,3 @@
//
// Created by rhinemann on 10.03.25.
//
#ifndef PTE_H
#define PTE_H

View File

@ -1,9 +1,9 @@
#include "Process.h"
Process::Process(PageTable *pageTable, const unsigned int executionTime)
: pageTable(pageTable), executionTime(executionTime), elapsedTime(0) {
: pageTable(pageTable), executionTime(executionTime) {
}
bool Process::is_finished() const {
bool Process::is_finished(const unsigned int elapsedTime) const {
return elapsedTime >= executionTime;
}

View File

@ -6,9 +6,9 @@
class Process {
public:
PageTable *pageTable;
unsigned int executionTime, elapsedTime;
unsigned int executionTime;
Process(PageTable *pageTable, unsigned int executionTime);
[[nodiscard]] bool is_finished() const;
[[nodiscard]] bool is_finished(unsigned int elapsedTime) const;
};

View File

@ -1,4 +1,5 @@
#include <iostream>
#include <format>
#include <random>
#include "Kernel.h"
@ -12,11 +13,11 @@ int main() {
std::mt19937 gen(rd());
std::uniform_int_distribution<unsigned int> distribution(500, 600), access_type_distr(1, 10);
List<PhysicalPage> free_pages, busy_pages;
Kernel kernel(MAX_PROC, free_pages, busy_pages);
Kernel kernel(MAX_PROC, List<PhysicalPage>(), List<PhysicalPage>());
unsigned int elapsed_time = 0;
for (int i = 1; i < PAGE_N; ++i) {
free_pages.insert_head(new PhysicalPage(i));
for (int i = 1; i <= PAGE_N; ++i) {
kernel.free_pages.insert_head(new PhysicalPage(i));
}
for (int i = 0; i < kernel.RunQ.capacity(); ++i) {
@ -27,14 +28,40 @@ int main() {
if (kernel.RunQ.empty()) {
break;
}
std::uniform_int_distribution<unsigned int> index_distribution(0, kernel.RunQ.at(0).pageTable->entries.size() - 1);
for (int j = 0; j < 30; ++j) {
const AccessType type = access_type_distr(gen) == 1 ? WRITE : READ;
constexpr unsigned int index = 1;
const unsigned int index = index_distribution(gen);
MMU::access(&kernel, kernel.RunQ.at(0).pageTable, index, type);
}
++kernel.RunQ.at(0).elapsedTime;
if (kernel.RunQ.at(0).is_finished()) {
++elapsed_time;
// kernel.stat_update();
if (kernel.RunQ.at(0).is_finished(elapsed_time)) {
PhysicalPage *curr, *cached_next;
unsigned int j;
const unsigned int cached_len = kernel.busy_pages.get_num();
for (curr = kernel.busy_pages.get_head(), j = 0;
curr && curr->next && j < cached_len;
curr = cached_next, ++j) {
cached_next = curr->next;
if (curr->PT == kernel.RunQ.at(0).pageTable) {
kernel.busy_pages.remove(curr);
kernel.free_pages.insert_tail(curr);
}
}
delete kernel.RunQ.at(0).pageTable;
kernel.RunQ.erase(kernel.RunQ.begin());
kernel.RunQ.emplace_back(new PageTable(distribution(gen)), distribution(gen));
}
{
unsigned int busy_stat = kernel.busy_pages.get_num(), free_stat = kernel.free_pages.get_num();
std::cout << std::format("[main:metrics] Memory usage stats: busy {}, free {}, total {}", busy_stat, free_stat, free_stat + busy_stat) << std::endl;
}
}