1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
#include <iostream> #include <vector> #include <queue> #include <unordered_map> // Constants const int PAGE_SIZE = 4; // Number of pages in memory const int NUM_PAGES = 10; // Total number of pages in the virtual memory // Class to simulate a virtual memory system class VirtualMemory { public: VirtualMemory(int pageSize) : pageSize(pageSize) {} // Access a page void accessPage(int pageNumber) { if (pageTable.find(pageNumber) == pageTable.end()) { // Page fault: page not in memory if (memory.size() >= pageSize) { // Memory is full, need to evict a page int evictPage = pageQueue.front(); pageQueue.pop(); pageTable.erase(evictPage); std::cout << "Page " << evictPage << " evicted from memory.\n"; } // Load the page into memory memory.push_back(pageNumber); pageTable[pageNumber] = memory.size() - 1; pageQueue.push(pageNumber); std::cout << "Page " << pageNumber << " loaded into memory.\n"; } else { // Page hit: page is already in memory std::cout << "Page " << pageNumber << " is already in memory.\n"; } } // Print current memory state void printMemory() const { std::cout << "Current memory state: "; for (int page : memory) { std::cout << page << " "; } std::cout << std::endl; } private: int pageSize; std::vector<int> memory; std::unordered_map<int, int> pageTable; // Maps page number to its position in memory std::queue<int> pageQueue; // Keeps track of page replacement order }; int main() { VirtualMemory vm(PAGE_SIZE); // Simulate accessing pages std::vector<int> pageRequests = {1, 2, 3, 4, 1, 5, 6, 2, 7, 8, 9, 10, 5, 3}; for (int pageNumber : pageRequests) { vm.accessPage(pageNumber); vm.printMemory(); } return 0; } |
Explanation
- Constants:
PAGE_SIZE
: The maximum number of pages that can be held in memory at a time.NUM_PAGES
: Total number of unique pages in virtual memory.
Advertisement - VirtualMemory Class:
- Attributes:
pageSize
: Maximum capacity of pages in memory.memory
: Vector to store pages currently in memory.pageTable
: Maps page numbers to their positions in memory for quick lookup.pageQueue
: Queue to keep track of the order of pages for FIFO replacement.
Advertisement - Methods:
accessPage(int pageNumber)
: Handles page access. If the page is not in memory (page fault), it evicts the oldest page (if memory is full) and loads the new page.printMemory()
: Prints the current state of memory.
- Attributes:
- Main Function:
- Creates an instance of
VirtualMemory
with the specified page size. - Simulates accessing a sequence of pages.
- Calls
accessPage
for each page in the sequence, then prints the memory state.
Advertisement - Creates an instance of
Usage
- Page Access: The simulation processes a sequence of page requests, managing memory with FIFO page replacement.
- Memory State: After each page request, the current state of memory is printed, showing which pages are currently loaded.