-
Notifications
You must be signed in to change notification settings - Fork 0
/
LRU-paging-algorithm.c
93 lines (75 loc) · 2.32 KB
/
LRU-paging-algorithm.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
/*
Simulating the physical memory and its paging process
using the Least Recently Used (LRU) algorithm and
counting the page faults
*/
#include <stdio.h>
#include <stdlib.h>
int findLRU(int* timestamps, int noPhysPages) {
int min = timestamps[0], lruIndex = 0;
for (int i = 1; i < noPhysPages; i++) {
if (timestamps[i] < min) {
min = timestamps[i];
lruIndex = i;
}
}
return lruIndex;
}
int main(int argc, char** argv) {
if (argc != 4) {
printf("Wrong inputs. Should be 3 arguments!\n");
return 0;
}
unsigned int noPhysPages = atoi(argv[1]);
unsigned int pageSize = atoi(argv[2]);
char* fileName = argv[3];
if (noPhysPages == 0 || pageSize == 0) {
printf("Page size or number of physical pages cannot be 0!\n");
return 0;
}
printf("No physical pages = %u, page size = %u\n", noPhysPages, pageSize);
printf("Reading memory trace from %s...\n", fileName);
FILE* fp = fopen(fileName, "r");
if (fp == NULL) {
printf("Failed to open file: %s\n", fileName);
return 0;
}
unsigned int* frames = malloc(noPhysPages * sizeof(unsigned int));
int* timestamps = malloc(noPhysPages * sizeof(int));
for (int i = 0; i < noPhysPages; i++) {
frames[i] = -1;
timestamps[i] = 0;
}
unsigned int address, page;
unsigned int pageFaults = 0, references = 0;
int time = 0;
while (fscanf(fp, "%u", &address) != EOF) {
page = address / pageSize;
int found = 0;
for (unsigned int i = 0; i < noPhysPages; i++) {
if (frames[i] == page) {
found = 1; // Page found
timestamps[i] = time++; // Update the access time
break;
}
}
if (!found) {
pageFaults++;
int lruIndex = -1;
if (references < noPhysPages) {
lruIndex = references;
} else {
lruIndex = findLRU(timestamps, noPhysPages);
}
frames[lruIndex] = page;
timestamps[lruIndex] = time++;
}
references++;
}
fclose(fp);
printf("Read %u memory references\n", references);
printf("Result: %u page faults\n", pageFaults);
free(frames);
free(timestamps);
return 0;
}