forked from Aditya-devp/Contributershub
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lru_page replacement.c
144 lines (98 loc) · 2.14 KB
/
lru_page replacement.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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
#include<stdio.h>
void lru(int string[20], int n, int size)
{
//Creating array for block storage
int frames[n];
//Creating queue to maintain least recently accessed element
int lru_queue[n];
//Initializing each block with -1
for(int i = 0; i < n; i++)
{
frames[i] = -1;
lru_queue[i] = -1;
}
//Index to insert element
int index = 0;
int lru_page;
//Counters
int page_miss = 0;
int page_hits = 0;
//Traversing each symbol in lru
for(int i = 0; i < size; i++)
{
int symbol = string[i];
int flag = 0;
for(int j = 0; j<n;j++)
{
if(symbol == frames[j])
{
flag = 1;
//Find position of j in queue
int pos;
for(int k = 0; k < n; k++)
{
if (lru_queue[k] == symbol)
{
pos = k;
break;
}
}
//Move all elements after pos index up
for(int x = pos; x < n - 1; x++)
lru_queue[x] = lru_queue[x + 1];
//Add symbol index to end of queue
lru_queue[n - 1] = symbol;
break;
}
}
if(flag == 1)
{
printf("\nSymbol: %d Frame: ", symbol);
for(int j = 0; j < n; j++)
printf("%d ", frames[j]);
page_hits += 1;
}
else
{
if (index < n)
{
frames[index++] = symbol;
}
else{
//Find index of lru_page and replace
lru_page = lru_queue[0];
for(int k = 0; k < n; k++)
{
if(frames[k] == lru_page)
{
frames[k] = symbol;
}
}
}
page_miss += 1;
printf("\nSymbol: %d Frame: ", symbol);
for(int j = 0; j < n; j++)
printf("%d ", frames[j]);
//Shifting and adding new symbol to end of queue
for(int x = 0; x < n - 1; x++)
lru_queue[x] = lru_queue[x + 1];
lru_queue[n - 1] = symbol;
}
}
printf("\nPage hits: %d", page_hits);
printf("\nPage misses/Page faults: %d\n", page_miss);
}
//Main
int main(){
int size, no_frames, i;
printf("Enter the no. of pages: ");
scanf("%d", &size);
int string[size];
printf("Enter the page numbers: ");
for(i = 0;i < size; i++)
scanf("%d",&string[i]);
printf("Enter the no. of frames: ");
scanf("%d", &no_frames);
lru(string,no_frames,size);
return 0;
}