MeMOry RePlacement alGorithms

feat: Comment the second.py algorithm.

+15 -14
+15 -14
second.py
··· 37 37 # Make space in physical memory depending on the order. 38 38 def make_space(s): 39 39 def rec(page_table: dict[int, Bits], order: list[int], page: int) -> tuple[int, list[int]]: 40 - print("-" * 80) 41 - print("\ttable:", page_table) 42 - print("\torder:", order) 43 - print("\tpage:", page) 44 - print("-" * 80) 45 40 # if the page table says that the page's R bit was set: 46 41 if page_table[page].read == True: 47 42 # rotate the queue: s_.order[1:] + [page] ··· 49 44 # the next page to check for is: s_.order[0] 50 45 return rec(page_table | {page: Bits(False, False)}, order[1:] + [order[0]], order[0]) 51 46 else: 52 - print("=" * 80) 53 - print("final page:", page) 54 - print("=" * 80) 55 47 return page, order 56 - # Finally, return the new state, without that page. 57 48 last_page, new_order = rec(s.page_table, s.order[1:] + [s.order[0]], s.order[0]) 49 + # Finally, return the new state, without that page. 58 50 return State(s.faults, s.hits, new_order[:-1], {k: v for k, v in s.page_table.items() if k != last_page}, s.physical_capacity) 59 51 52 + # This function simply inserts a page into the page table. 60 53 def insert(s, page: int): 61 54 return State(s.faults, s.hits, s.order + [page], s.page_table | {page: Bits(False, False)}, s.physical_capacity) 62 55 56 + # This is the algorithm for second chance page replacement. 63 57 def second(state: State, access: tuple[str,int]) -> State: 64 58 page = access[1] 65 - print("-" * 80) 66 - print("table:", state.page_table) 67 - print("order:", state.order) 68 - print("page:", page) 69 - print("-" * 80) 59 + # If the page is in the page table, that is a page hit. 70 60 if page in state.page_table: 61 + # Also, the page's R bit must be set to 1. 71 62 return state.modify(page, True).hit() 72 63 else: 64 + # If there isn't any space in the page table, call make_space() 73 65 if len(state.page_table) == state.physical_capacity: 74 66 return state.make_space().insert(page).fault() 75 67 else: 68 + # If there is space in the page table, we can simply insert the page. 76 69 return state.insert(page).fault() 77 70 78 71 def main(): 72 + # The user must provide the physical capacity and the file path to the 73 + # access sequence. 79 74 if len(sys.argv) != 3: 80 75 print("USAGE:", sys.argv[0], "number_of_pages", "file_path") 81 76 print("\twhere:\n\t\tnumber_of_pages : int\n\t\tfile_path : string") 82 77 78 + # The second argument is the file path. 83 79 file_path = sys.argv[2] 84 80 81 + # This simply parses the access sequence file. 85 82 accesses = [] 86 83 with open(file_path) as f: 87 84 for pair in f.read().split(): 88 85 mode, page = pair.split(":") 89 86 accesses.append((mode, int(page))) 90 87 88 + # The physical capacity is the first argument. 91 89 limit = int(sys.argv[1]) 90 + # The result is simply the evaluation of the second algorithm on the access 91 + # sequence. 92 92 res = fold(second, State(0, 0, [], dict(), limit), accesses) 93 93 94 + # Print the result. 94 95 print("Faults:", res.faults, "\nHits:", res.hits) 95 96 96 97 main()