-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLRU.c
126 lines (111 loc) · 3.79 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
114
115
116
117
118
119
120
121
122
123
124
125
126
//C Program to Implement the LRU(Least Recently Used) Page replacement Algorithm
#include<stdio.h>
#include<stdbool.h>
#include<string.h>
#include<limits.h>
struct PageTable
{
int frame_no;
int last_time_of_access;
bool valid;
};
//Function to check if referenced/asked page is already present in frame[] or not
//Returns true if page is already present else returns false
bool isPagePresent(struct PageTable PT[],int page)
{
if(PT[page].valid == 1)
return true;
return false;
}
//Function to update the page table
//Return Nothing
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;
}
}
//Function to print the frame contents
//Return nothing
void printFrameContents(int frame[],int no_of_frames)
{
for(int i=0;i<no_of_frames;i++)
printf("%d ",frame[i]);
printf("\n");
}
//Function to find the victim page index in frame[]
//Return that LRU page index using call by address
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);
//create reference string array
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);
//create frame array to store the pages at different point of times
int frame[no_of_frames];
memset(frame,-1,no_of_frames*sizeof(int));
struct PageTable PT[50] ; //asume page table can have entries for page 0 to 49
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++)
{
//search the ith page in all allocated frames
if( ! (isPagePresent(PT,reference_string[i])))
{
page_fault++; // Increase the count of 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)
{
//current=0;
flag=true;
}
}
else //frame are full , APPLY LRU Algo
{
//search the LRU page( victim page) with the help of PT
//mark that page as INVALID in Page Table
int LRU_page_index;
searchLRUPage(PT,frame,no_of_frames,&LRU_page_index);
updatePageTable(PT,frame[LRU_page_index], -1 ,0,i); //send invalid frame_no =-1
frame[LRU_page_index]=reference_string[i];
printFrameContents(frame,no_of_frames);
//Update PT
updatePageTable(PT,reference_string[i],LRU_page_index,1,i);
}
}
//Update the Page Access time for reference_string[i]
PT[reference_string[i]].last_time_of_access = i;
} //end of for loop
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;
}