Untitled

 avatar
unknown
c_cpp
16 days ago
1.2 kB
3
Indexable
#include <iostream>
#include <vector>
#include <unordered_set>

using namespace std;

void simulatePaging(const vector<int>& pageSequence, int numFrames) {
    vector<int> memory;
    unordered_set<int> pageSet;

    for (int page : pageSequence) {
        cout << "Accessing page: " << page << endl;

        // If the page is not in memory and there's space
        if (pageSet.find(page) == pageSet.end()) {
            if (memory.size() < numFrames) {
                memory.push_back(page);
                pageSet.insert(page);
                cout << "Page " << page << " loaded into memory." << endl;
            } else {
                cout << "Memory is full! Page " << page << " could not be loaded." << endl;
            }
        } else {
            cout << "Page " << page << " is already in memory." << endl;
        }

        // Display current state of memory
        cout << "Current memory state: ";
        for (int p : memory) cout << p << " ";
        cout << endl << "-----------------------------" << endl;
    }
}

int main() {
    vector<int> pageSequence = {1, 2, 3, 4, 5, 6, 1, 2};
    int numFrames = 4;
    simulatePaging(pageSequence, numFrames);
    return 0;
}
Leave a Comment