From e312fbdbd6cd39ee9b77d5f90774d6b83100bf9b Mon Sep 17 00:00:00 2001 From: 423A35C7 <609514299@qq.com> Date: Tue, 7 May 2024 21:50:16 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=8C=E6=88=90=E5=AE=9E=E9=AA=8C3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 实现第二次机会页面置换算法 --- labcodes_answer/lab3_result/kern/mm/swap_fifo.c | 32 +++++++++++++++++++------ 1 file changed, 25 insertions(+), 7 deletions(-) diff --git a/labcodes_answer/lab3_result/kern/mm/swap_fifo.c b/labcodes_answer/lab3_result/kern/mm/swap_fifo.c index 4f9dc8f..3c231b1 100644 --- a/labcodes_answer/lab3_result/kern/mm/swap_fifo.c +++ b/labcodes_answer/lab3_result/kern/mm/swap_fifo.c @@ -69,7 +69,25 @@ _fifo_swap_out_victim(struct mm_struct *mm, struct Page ** ptr_page, int in_tick //(1) unlink the earliest arrival page in front of pra_list_head qeueue //(2) set the addr of addr of this page to ptr_page /* Select the tail */ - list_entry_t *le = head->prev; + // 从后往前,最后面的是最早访问的 + list_entry_t *current = head->prev; + list_entry_t *le = NULL; + for (; current != head; current = current->prev) { + pte_t *ptep = get_pte(mm->pgdir, le2vma(current, vm_start), 0); + cprintf("@@@ %d %x :a page\n", *ptep & PTE_A, current); + if (*ptep & PTE_A) { // 访问位为1 + *ptep &= !PTE_A; // 清除访问位 + list_del(current); // 从链表中移除 + list_add(head, current); // 添加到链表头部 + cprintf("### %d %x :clear access\n", *ptep & PTE_A, current); + } else { + le = current; + break; + } + } + if (le == NULL) { + le = head->prev; // 循环一遍找不到就最后一个 + } assert(head!=le); struct Page *p = le2page(le, pra_page_link); list_del(le); @@ -97,26 +115,26 @@ _fifo_check_swap(void) { assert(pgfault_num==5); cprintf("write Virt Page b in fifo_check_swap\n"); *(unsigned char *)0x2000 = 0x0b; - assert(pgfault_num==5); + assert(pgfault_num==6); // 从这里开始缺页次数与FIFO不同了 cprintf("write Virt Page a in fifo_check_swap\n"); *(unsigned char *)0x1000 = 0x0a; assert(pgfault_num==6); cprintf("write Virt Page b in fifo_check_swap\n"); *(unsigned char *)0x2000 = 0x0b; - assert(pgfault_num==7); + assert(pgfault_num==6); cprintf("write Virt Page c in fifo_check_swap\n"); *(unsigned char *)0x3000 = 0x0c; - assert(pgfault_num==8); + assert(pgfault_num==7); cprintf("write Virt Page d in fifo_check_swap\n"); *(unsigned char *)0x4000 = 0x0d; - assert(pgfault_num==9); + assert(pgfault_num==8); cprintf("write Virt Page e in fifo_check_swap\n"); *(unsigned char *)0x5000 = 0x0e; - assert(pgfault_num==10); + assert(pgfault_num==8); cprintf("write Virt Page a in fifo_check_swap\n"); assert(*(unsigned char *)0x1000 == 0x0a); *(unsigned char *)0x1000 = 0x0a; - assert(pgfault_num==11); + assert(pgfault_num==9); return 0; }