43 lines
1.3 KiB
C++
43 lines
1.3 KiB
C++
#include "Kernel.h"
|
|
|
|
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 = nullptr;
|
|
if (!this->free_pages.is_empty()) {
|
|
page = free_pages.get_head()->prev;
|
|
free_pages.remove(page);
|
|
busy_pages.insert_tail(page);
|
|
} else {
|
|
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;
|
|
page_table[idx].R = false;
|
|
page_table[idx].M = false;
|
|
}
|
|
}
|
|
|
|
void Kernel::stat_update() {
|
|
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;
|
|
}
|
|
}
|