-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathpaging_fifo.c
88 lines (78 loc) · 2.18 KB
/
paging_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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
#define FRAMES 4
struct Frame{
int content;
int arrivalTime;
};
void displayFrames(struct Frame **pages){
printf("| ");
for(int i=0; i<FRAMES; i++){
if(pages[i]->content == -1)
printf(" | ");
else
printf("%d | ", pages[i]->content);
}
putchar('\n');
}
int lru(struct Frame **pages, int *refStr, int n){
int freepages = FRAMES;
int time = -1, faults = 0;
// Traverse Reference String
for(int i=0; i<n; i++){
time++;
displayFrames(pages);
// Check if page already exist
int found = 0;
for(int j=0; j<FRAMES; j++){
if(pages[j]->content == refStr[i]){
found = 1;
break;
}
}
if(found) continue; else faults++;
// Check if gap exist
found = 0;
for(int j=0; j<FRAMES; j++){
if(pages[j]->content == -1){
found = 1;
pages[j]->content = refStr[i];
pages[j]->arrivalTime = time;
break;
}
}
if(found) continue;
// Else, do replacement as per fifo
// Find min arrival time and corresponding frame
int min_frame = -1, min_time = INT_MAX;
for(int j=0; j<FRAMES; j++){
if(pages[j]->arrivalTime < min_time){
min_frame = j;
min_time = pages[j]->arrivalTime;
}
}
// Swap current page with victim frame
pages[min_frame]->content = refStr[i];
pages[min_frame]->arrivalTime = time;
}
return faults;
}
void main(){
struct Frame **pages = malloc(sizeof(pages)*FRAMES);
for(int i=0; i<FRAMES; i++){
pages[i] = malloc(sizeof(struct Frame));
pages[i]->content = -1;
pages[i]->arrivalTime = -1;
}
int n;
printf("Length of reference string: ");
scanf("%d", &n);
int *refStr = malloc(sizeof(int)*n);
printf("ReferenceString: ");
for(int i=0; i<n; i++)
scanf("%d", &refStr[i]);
// Do algorithms
int faults = lru(pages, refStr, n);
printf("Page Faults: %d\n", faults);
}