JustPaste
HomeCategoriesAboutDonateContactTerms of UsePrivacy Policy
JustPaste

Free online notepad — write and share instantly

Navigate

  • Home
  • Timeline
  • Categories

Info

  • About
  • Donate
  • Contact

Legal

  • Terms of Use
  • Privacy Policy

© 2026 JustPaste.app. All rights reserved.

Made with ♥ by JustPaste

Paging | JustPaste.app
about 4 hours ago11 views
👨‍💻Programming

Paging

#include <stdio.h>

int main() {
    int p[] = {1, 2, 3, 1, 4, 5};
    int n = 6, f = 3, frame[10], last[10], freq[10];
    int choice, faults = 0, ptr = 0;

    printf("1. FIFO\n2. LRU\n3. LFU\nChoice: ");
    scanf("%d", &choice);

    for (int j = 0; j < f; j++)
        frame[j] = -1, last[j] = 0, freq[j] = 0;

    for (int i = 0; i < n; i++) {
        int hit = 0, pos = -1;

        for (int j = 0; j < f; j++)
            if (frame[j] == p[i]) {
                hit = 1;
                last[j] = i;
                freq[j]++;
                break;
            }

        if (hit) continue;

        if (choice == 1) {             // FIFO
            pos = ptr;
            ptr = (ptr + 1) % f;
        }
        else if (choice == 2) {        // LRU
            int oldest = n + 1;
            for (int j = 0; j < f; j++) {
                if (frame[j] == -1) { pos = j; break; }
                if (last[j] < oldest)
                    oldest = last[j], pos = j;
            }
        }
        else if (choice == 3) {        // LFU
            int least = n + 1;
            for (int j = 0; j < f; j++) {
                if (frame[j] == -1) { pos = j; break; }
                if (freq[j] < least)
                    least = freq[j], pos = j;
            }
        }
        else {
            printf("Invalid choice!\n");
            return 1;
        }

        frame[pos] = p[i];
        last[pos] = i;
        freq[pos] = 1;
        faults++;
    }

    printf("Page faults: %d\n", faults);
    return 0;
}

⚠️Content was pasted as plain text and auto-formatted as a code block. Use the Code Block button in the editor for proper formatting.

← Back to timeline