-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlru_v2.c
158 lines (143 loc) · 3.76 KB
/
lru_v2.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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
#include <stdio.h>
#include <assert.h>
#include <unistd.h>
#include <getopt.h>
#include <stdlib.h>
#include "pagetable.h"
extern int memsize;
extern int debug;
extern struct frame *coremap;
// used for the list implementation of lru
static struct node *head;
// count the number of nodes in our list, the maximum should be memsize
static int cnt;
/* Page to evict is chosen using the accurate LRU algorithm.
* Returns the page frame number (which is also the index in the coremap)
* for the page that is to be evicted.
*/
int lru_evict() {
// we only need to evict the head of the list, because it is the oldest node
unsigned int to_go = head->frame;
return to_go;
}
/* This function is called on each access to a page to update any information
* needed by the lru algorithm.
* Input: The page table entry for the page that is being accessed.
*/
void lru_ref(pgtbl_entry_t *p) {
// if the list is not full
if (cnt < memsize) {
// we first evaluate the head node, if cnt == 0, we assignment it and return
if (cnt == 0) {
head->frame = p->frame;
cnt = 1;
return;
}
// use previous and temp to keep track of the node in the list
struct node* temp = head;
struct node* previous = head;
while (temp->next != NULL) {
// if temp->frame == p->frame
if (temp->frame == p->frame) {
// need to record this node, delete this node, and put it at the end of the list
// if this node is the head node
if (temp == head) {
// go to the last node
while (temp->next != NULL) {
temp = temp->next;
}
// change the head node
// put previous node (which is previous head) to be temp->next
head = head->next;
temp->next = previous;
temp->next->next = NULL;
return;
}
// else if it is not the head node
else {
struct node* pNode = temp;
temp = temp->next;
previous->next = temp;
// loop to the end
while (temp->next != NULL) {
temp = temp->next;
}
// assign the pNode to the end of the list
temp->next = pNode;
return;
}
}
// else temp->frame isn't p->frame
else {
previous = temp;
temp = temp->next;
}
}
// here we loop to the end but still haven't found the p->frame
struct node* newNode = (struct node*)malloc(sizeof(struct node));
newNode->next = NULL;
newNode->frame = p->frame;
temp->next = newNode;
// we should add cnt
cnt ++;
return;
}
// else the list is full
// p->frame might be inside the list, might not be inside the list
else {
struct node* temp = head;
struct node* previous = head;
while (temp->next != NULL) {
// if temp->frame == p->frame
if (temp->frame == p->frame) {
// if it is the head node
if (temp == head) {
// loop to the end
while (temp->next != NULL) {
temp = temp->next;
}
head = head->next;
temp->next = previous;
temp->next->next = NULL;
return;
}
else {
struct node* pNode = temp;
temp = temp->next;
previous->next = temp;
while (temp->next != NULL) {
temp = temp->next;
}
temp->next = pNode;
return;
}
}
// else haven't found it yet
else {
previous = temp;
temp = temp->next;
}
}
// if we cannot find it, we move head to the end of the list and change its frame
previous = head;
head = head->next;
previous = temp->next;
temp->next->frame = p->frame;
temp->next->next = NULL;
return;
}
}
/* Initialize any data structures needed for this
* replacement algorithm
*/
void lru_init() {
// initialize the head of the list
head = (struct node*)malloc(sizeof(struct node));
// initialize the frame = 0
head->frame = 0;
// initialize the next pointer points to NULL
head->next = NULL;
// initialize the cnt = 0
cnt = 0;
return;
}