Finished all methods. Main function and debug printing pending.

This commit is contained in:
rhinemann 2025-03-31 22:20:01 +03:00
parent 359ffa755c
commit 5de65255e1
11 changed files with 81 additions and 84 deletions

View File

@ -1,19 +1,29 @@
#include "Kernel.h"
Kernel::Kernel(const List<PhysicalPage> &free_pages, const List<PhysicalPage> &busy_pages)
: free_pages(free_pages), busy_pages(busy_pages) {
}
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) {}
void Kernel::page_fault(PageTable page_table, const int idx) {
PhysicalPage *page;
PhysicalPage *page = nullptr;
if (!this->free_pages.is_empty()) {
page = free_pages.last();
page = free_pages.get_head()->prev;
free_pages.remove(page);
busy_pages.insert_tail(page);
} else {
page = nullptr; // TODO: page replacement algorithm
for (PhysicalPage *curr = busy_pages.get_head();; curr = curr->next) {
if ((*curr->PT)[curr->idx].R)
(*curr->PT)[curr->idx].R = false;
else {
curr->PT->entries.erase(next(curr->PT->entries.begin(), curr->idx));
page = curr;
busy_pages.set_head(curr->next);
break;
}
}
}
if (page) {
page_table[idx].PPN = page->PPN;
page_table[idx].P = true;
@ -23,7 +33,10 @@ void Kernel::page_fault(PageTable page_table, const int idx) {
}
void Kernel::stat_update() {
for (int i = 0; i < 10; ++i) {
// TODO: update busy page stats
int i;
PhysicalPage *curr;
for (i = 0, curr = busy_pages.get_head(); i < 10; ++i, curr = curr->next) {
if ((*curr->PT)[curr->idx].R)
(*curr->PT)[curr->idx].R = false;
}
}

View File

@ -11,7 +11,7 @@
class Kernel {
public:
List<PhysicalPage> free_pages, busy_pages;
// TODO: RunQ
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);

104
List.h
View File

@ -3,96 +3,84 @@
template<typename T>
class List {
T *head, *tail;
T *head;
unsigned int num;
const unsigned int idx;
public:
explicit List(unsigned int idx);
T *first();
T *last();
void insert_head(T *elem);
void insert_tail(T *elem);
void insert_before(T *elem1, T *elem2);
void remove(T *elem);
bool is_empty();
[[nodiscard]] unsigned int get_num() const;
public:
explicit List();
[[nodiscard]] T *get_head();
void set_head(T *head);
void insert_head(T *elem);
void insert_tail(T *elem);
void insert_before(T *elem, T *reference_point);
void remove(T *elem);
bool is_empty();
[[nodiscard]] unsigned int get_num() const;
};
#define for_each_in_list(elem, list) \
for ( \
(elem) = (list)->first(); \
(elem) != nullptr; \
(elem) = (list)->next(elem) \
)
template<typename T>
List<T>::List(const unsigned int idx) : head{nullptr}, tail{nullptr}, num{0}, idx{idx} {
List<T>::List() : head{nullptr}, num{0} {
}
template<typename T>
T * List<T>::first() {
return this->head;
T *List<T>::get_head() {
return head;
}
template<typename T>
T * List<T>::last() {
return this->tail;
void List<T>::set_head(T *head) {
this->head = head;
}
template<typename T>
void List<T>::insert_head(T *elem) {
const unsigned int idx{this->idx};
if (!head)
elem->next = elem->prev = elem;
else {
elem->next = head;
elem->prev = head->prev;
head->prev->next = elem;
head->prev = elem;
}
elem->next[idx] = this->head;
elem->prev[idx] = nullptr;
if (this->head != nullptr)
this->head->prev[idx] = elem;
if (this->tail == nullptr)
this->tail = elem;
this->head = elem;
++this->num;
}
template<typename T>
void List<T>::insert_tail(T *elem) {
const unsigned int idx{this->idx};
elem->next[idx] = nullptr;
elem->prev[idx] = this->tail;
if (this->tail != nullptr)
this->tail->next[idx] = elem;
if (this->head == nullptr)
this->head = elem;
this->tail = elem;
if (!head)
elem->next = elem->prev = elem;
else {
elem->next = head;
elem->prev = head->prev;
head->prev->next = elem;
head->prev = elem;
}
++this->num;
}
template<typename T>
void List<T>::insert_before(T *elem1, T *elem2) {
const unsigned int idx{this->idx};
elem1->next[idx] = elem2;
elem1->prev[idx] = elem2->prev[idx];
elem2->prev[idx] = elem1;
if (elem1->prev[idx] == nullptr)
this->head = elem1;
void List<T>::insert_before(T *elem, T *reference_point) {
elem->next = reference_point;
elem->prev = reference_point->prev;
reference_point->prev = elem;
if (elem->prev == nullptr)
this->head = elem;
else
elem1->prev[idx]->next[idx] = elem1;
elem->prev->next = elem;
++this->num;
}
template<typename T>
void List<T>::remove(T *elem) {
const unsigned int idx{this->idx};
if (elem->next[idx] != nullptr)
elem->next[idx]->prev[idx] = elem->prev[idx];
if (elem->prev[idx] != nullptr)
elem->prev[idx]->next[idx] = elem->next[idx];
if (elem->next)
elem->next->prev = elem->prev;
if (elem->prev)
elem->prev->next = elem->next;
if (this->head == elem)
this->head = elem->next[idx];
if (this->tail == elem)
this->tail = elem->prev[idx];
this->head = elem->next;
--this->num;
}

1
MMU.h
View File

@ -12,7 +12,6 @@ enum AccessType {
class MMU {
public:
static void access(Kernel kernel, PageTable table, int index, AccessType type);
// TODO type of access
};

View File

@ -1,5 +1,5 @@
#include "PageTable.h"
PTE & PageTable::operator[](const int index) {
PTE &PageTable::operator[](const unsigned int index) {
return this->entries.at(index);
}

View File

@ -1,7 +1,3 @@
//
// Created by rhinemann on 10.03.25.
//
#ifndef PAGETABLE_H
#define PAGETABLE_H
@ -12,7 +8,7 @@
class PageTable {
public:
std::vector<PTE> entries;
PTE &operator[](int index);
PTE &operator[](unsigned int index);
};
#endif //PAGETABLE_H

View File

@ -1,6 +1,7 @@
#include "PhysicalPage.h"
PhysicalPage::PhysicalPage(const unsigned int PPN): PPN(PPN) {
PhysicalPage::PhysicalPage(const unsigned int PPN)
: PPN(PPN), idx(0), PT{nullptr}, next{nullptr}, prev(nullptr) {
}
void PhysicalPage::take_up(PageTable *PT, const unsigned int idx) {

View File

@ -1,7 +1,3 @@
//
// Created by rhinemann on 10.03.25.
//
#ifndef PHYSICALPAGE_H
#define PHYSICALPAGE_H
@ -11,7 +7,7 @@ class PhysicalPage {
public:
unsigned int PPN, idx;
PageTable *PT;
PhysicalPage *next[2], *prev[2];
PhysicalPage *next, *prev;
explicit PhysicalPage(unsigned int PPN);

View File

@ -4,6 +4,6 @@ Process::Process(PageTable *pageTable, const unsigned int executionTime)
: pageTable(pageTable), executionTime(executionTime), elapsedTime(0) {
}
bool Process::is_finished() {
bool Process::is_finished() const {
return elapsedTime >= executionTime;
}

View File

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

View File

@ -11,7 +11,7 @@ int main() {
std::mt19937 gen(rd());
std::uniform_int_distribution<unsigned int> distribution(0, 10);
List<PhysicalPage> free_pages(0), busy_pages(0);
List<PhysicalPage> free_pages, busy_pages;
Kernel kernel(free_pages, busy_pages);
for (int i = 0; i < PAGE_N; ++i) {
@ -22,10 +22,14 @@ int main() {
kernel.RunQ.emplace_back(nullptr, distribution(gen));
}
while (!kernel.RunQ.empty()) {
}
for (const auto process : kernel.RunQ)
std::cout << process.executionTime << std::endl;
// std::cout << MAX_PROC << std::endl;
std::cout << MAX_PROC << std::endl;
return 0;
}