FIFO.c
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

#include <stdio.h>

int main() {
    int referenceString[10];
    int pageFaults = 0;
    int i, j, flag, pages, frames;

    // Input the number of pages and the page reference string
    printf("\nEnter the number of Pages:\t");
    scanf("%d", &pages);
    printf("\nEnter reference string values:\n");
    for(i = 0; i < pages; i++) {
        printf("Value No. [%d]:\t", i + 1);
        scanf("%d", &referenceString[i]);
    }

    // Input the number of frames
    printf("\nWhat are the total number of frames:\t");
    scanf("%d", &frames);

    int temp[frames];

    // Initialize frames with -1 (indicating empty slots)
    for(i = 0; i < frames; i++) {
        temp[i] = -1;
    }

    for(i = 0; i < pages; i++) {
        flag = 0;

        // Check if the page is already in the frames
        for(j = 0; j < frames; j++) {
            if(referenceString[i] == temp[j]) {
                flag++;
                pageFaults--;
            }
        }

        pageFaults++;

        // If page is not in frames and we have not filled all frames yet
        if((pageFaults <= frames) && (flag == 0)) {
            temp[i] = referenceString[i];
        }
        // If page is not in frames and all frames are filled
        else if(flag == 0) {
            temp[(pageFaults - 1) % frames] = referenceString[i];
        }

        // Print current state of frames
        printf("\n");
        for(j = 0; j < frames; j++) {
            printf("%d\t", temp[j]);
        }
    }

    printf("\nTotal Page Faults:\t%d\n", pageFaults);
    return 0;
}

FIFO Page Replacement Algorithm C

Code

Fell free to copy

some code

Grinning face emoji