Skip to content

Commit 3a8bb93

Browse files
committed
feat(assign5): ✨ implement LRU replacement algorithm
1 parent 33c5414 commit 3a8bb93

File tree

2 files changed

+90
-69
lines changed

2 files changed

+90
-69
lines changed

assign5/lru_replacement.cpp

Lines changed: 42 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,59 @@
11
/**
2-
* Assignment 5: Page replacement algorithms
2+
* Assignment 5: Page replacement algorithms
33
* @file lru_replacement.cpp
4-
* @author ??? (TODO: your name)
4+
* @author Caden Jamason and Adrian Reyes
55
* @brief A class implementing the LRU page replacement algorithms
66
* @version 0.1
77
*/
8-
//You must complete the all parts marked as "TODO". Delete "TODO" after you are done.
9-
// Remember to add sufficient and clear comments to your code
108

119
#include "lru_replacement.h"
1210

13-
// TODO: Add your implementation here
1411
LRUReplacement::LRUReplacement(int num_pages, int num_frames)
15-
: Replacement(num_pages, num_frames)
16-
{
17-
// TODO: Complete this constructor
18-
}
12+
: Replacement(num_pages, num_frames) {}
1913

20-
// TODO: Add your implementations for desctructor, touch_page, load_page, replace_page here
21-
LRUReplacement::~LRUReplacement()
22-
{
23-
// TODO: Add necessary code here
24-
}
14+
LRUReplacement::~LRUReplacement() {}
2515

26-
// Accesss a page alreay in physical memory
27-
void LRUReplacement::touch_page(int page_num)
28-
{
29-
// TODO: Update your data structure LRU replacement
16+
void LRUReplacement::touch_page(int page_num) {
17+
// Move accessed page to front of list
18+
lru_list.erase(page_map[page_num]);
19+
lru_list.push_front(page_num);
20+
page_map[page_num] = lru_list.begin();
3021
}
3122

32-
// Access an invalid page, but free frames are available
3323
void LRUReplacement::load_page(int page_num) {
34-
// TODO: Update your data structure LRU replacement and pagetable
24+
// Calculate frame number for new page
25+
int frame_num = total_frames - free_frames;
26+
27+
// Update page table with frame number and mark page as valid
28+
page_table[page_num].frame_num = frame_num;
29+
page_table[page_num].valid = true;
30+
31+
// Move new page to front of list
32+
lru_list.push_front(page_num);
33+
page_map[page_num] = lru_list.begin();
3534
}
3635

37-
// Access an invalid page and no free frames are available
3836
int LRUReplacement::replace_page(int page_num) {
39-
// TODO: Update your data structure LRU replacement and pagetable
40-
return 0;
41-
}
37+
// Least recently used page is at the back of the list
38+
int victim_page = lru_list.back();
39+
lru_list.pop_back();
40+
41+
// Update page table for victim page
42+
page_table[victim_page].valid = false;
43+
44+
// Use the frame of the victim page for the new page
45+
page_table[page_num].frame_num = page_table[victim_page].frame_num;
46+
47+
// Update page table for new page
48+
page_table[page_num].valid = true;
49+
50+
// Move new page to front of list
51+
lru_list.push_front(page_num);
52+
page_map[page_num] = lru_list.begin();
53+
54+
// Remove victim page from map
55+
page_map.erase(victim_page);
56+
57+
// Return victim page number
58+
return victim_page;
59+
}

assign5/lru_replacement.h

Lines changed: 48 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,58 +1,61 @@
11
/**
2-
* Assignment 5: Page replacement algorithms
2+
* Assignment 5: Page replacement algorithms
33
* @file lru_replacement.h
4-
* @author ??? (TODO: your name)
4+
* @author Caden Jamason and Adrian Reyes
55
* @brief A class implementing the LRU page replacement algorithms
66
* @version 0.1
77
*/
8-
//You must complete the all parts marked as "TODO". Delete "TODO" after you are done.
9-
// Remember to add sufficient and clear comments to your code
108

119
#pragma once
1210

13-
// Remember to add comments to your code
14-
1511
#include "replacement.h"
12+
#include <list>
13+
#include <unordered_map>
1614

1715
/**
18-
* @brief A class to simulate the least recently used (LRU) page replacement algorithm.
16+
* @brief A class to simulate the least recently used (LRU) page replacement
17+
* algorithm.
1918
*/
20-
class LRUReplacement : public Replacement
21-
{
22-
// TODO: Add your implementation to this class
19+
class LRUReplacement : public Replacement {
20+
private:
21+
// Maintains order of page accesses
22+
std::list<int> lru_list;
23+
24+
// Stores page number and its corresponding iterator in list
25+
std::unordered_map<int, std::list<int>::iterator> page_map;
26+
2327
public:
24-
/**
25-
* @brief Constructor
26-
* @param num_pages
27-
* @param num_frames
28-
*/
29-
LRUReplacement(int num_pages, int num_frames);
30-
31-
/**
32-
* @brief Destructor
33-
*/
34-
virtual ~LRUReplacement();
35-
36-
/**
37-
* @brief Accesss a page alreay in physical memory
38-
* It may be overridden in a subclass
39-
* @param page_num The logical page number.
40-
*/
41-
virtual void touch_page(int page_num);
42-
43-
/**
44-
* @brief Access an invalid page, but free frames are available.
45-
* Assign the page to an available frame, not replacement needed
46-
* @param page_num The logical page number.
47-
*/
48-
virtual void load_page(int page_num);
49-
50-
/**
51-
* @brief Access an invalid page, and there is no free frame.
52-
* Replace the page with the page that has been in memory the longest.
53-
* @param page_num The logical page number.
54-
* @return Selected victim page #
55-
*/
56-
virtual int replace_page(int page_num);
57-
58-
};
28+
/**
29+
* @brief Constructor
30+
* @param num_pages
31+
* @param num_frames
32+
*/
33+
LRUReplacement(int num_pages, int num_frames);
34+
35+
/**
36+
* @brief Destructor
37+
*/
38+
virtual ~LRUReplacement();
39+
40+
/**
41+
* @brief Accesss a page already in physical memory
42+
* It may be overridden in a subclass
43+
* @param page_num The logical page number.
44+
*/
45+
virtual void touch_page(int page_num);
46+
47+
/**
48+
* @brief Access an invalid page, but free frames are available.
49+
* Assign the page to an available frame, not replacement needed
50+
* @param page_num The logical page number.
51+
*/
52+
virtual void load_page(int page_num);
53+
54+
/**
55+
* @brief Access an invalid page, and there is no free frame.
56+
* Replace the page with the page that has been in memory the longest.
57+
* @param page_num The logical page number.
58+
* @return Selected victim page #
59+
*/
60+
virtual int replace_page(int page_num);
61+
};

0 commit comments

Comments
 (0)