-
Notifications
You must be signed in to change notification settings - Fork 2
/
LRU.c
113 lines (94 loc) · 2.64 KB
/
LRU.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
#include <stdio.h>
#include <stdbool.h>
#include <string.h>
#include <limits.h>
struct PageTable
{
int frame_no;
int last_time_of_access;
bool valid;
};
bool isPagePresent(struct PageTable PT[], int page)
{
if (PT[page].valid == 1)
return true;
return false;
}
void updatePageTable(struct PageTable PT[], int page, int fr_no, int status, int access_time)
{
PT[page].valid = status;
if (status == 1)
{
PT[page].last_time_of_access = access_time;
PT[page].frame_no = fr_no;
}
}
void printFrameContents(int frame[], int no_of_frames)
{
for (int i = 0; i < no_of_frames; i++)
printf("%d ", frame[i]);
printf("\n");
}
void searchLRUPage(struct PageTable PT[], int frame[], int no_of_frames, int *LRU_page_index)
{
int min = INT_MAX;
for (int i = 0; i < no_of_frames; i++)
{
if (PT[frame[i]].last_time_of_access < min)
{
min = PT[frame[i]].last_time_of_access;
*LRU_page_index = i;
}
}
}
int main()
{
int i, n, no_of_frames, page_fault = 0, current = 0;
bool flag = false;
printf("\n Enter the no. of pages:\n");
scanf("%d", &n);
int reference_string[n];
printf("\n Enter the reference string(different page numbers) :\n");
for (int i = 0; i < n; i++)
scanf("%d", &reference_string[i]);
printf("\n Enter the no. of frames you want to give to the process :");
scanf("%d", &no_of_frames);
int frame[no_of_frames];
memset(frame, -1, no_of_frames * sizeof(int));
struct PageTable PT[50];
for (int i = 0; i < 50; i++)
PT[i].valid = 0;
printf("\n****The Contents inside the Frame array at different time:****\n");
for (int i = 0; i < n; i++)
{
if (!(isPagePresent(PT, reference_string[i])))
{
page_fault++;
if (flag == false && current < no_of_frames)
{
frame[current] = reference_string[i];
printFrameContents(frame, no_of_frames);
updatePageTable(PT, reference_string[i], current, 1, i);
current = current + 1;
if (current == no_of_frames)
{
flag = true;
}
}
else
{
int LRU_page_index;
searchLRUPage(PT, frame, no_of_frames, &LRU_page_index);
updatePageTable(PT, frame[LRU_page_index], -1, 0, i);
frame[LRU_page_index] = reference_string[i];
printFrameContents(frame, no_of_frames);
updatePageTable(PT, reference_string[i], LRU_page_index, 1, i);
}
}
PT[reference_string[i]].last_time_of_access = i;
}
printf("\nTotal No. of Page Faults = %d\n", page_fault);
printf("\nPage Fault ratio = %.2f\n", (float)page_fault / n);
printf("\nPage Hit Ratio = %.2f\n", (float)(n - page_fault) / n);
return 0;
}