《操作系统》的实验代码。
Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

143 Zeilen
5.2 KiB

vor 12 Jahren
vor 12 Jahren
  1. #include <defs.h>
  2. #include <x86.h>
  3. #include <stdio.h>
  4. #include <string.h>
  5. #include <swap.h>
  6. #include <swap_fifo.h>
  7. #include <list.h>
  8. /* [wikipedia]The simplest Page Replacement Algorithm(PRA) is a FIFO algorithm. The first-in, first-out
  9. * page replacement algorithm is a low-overhead algorithm that requires little book-keeping on
  10. * the part of the operating system. The idea is obvious from the name - the operating system
  11. * keeps track of all the pages in memory in a queue, with the most recent arrival at the back,
  12. * and the earliest arrival in front. When a page needs to be replaced, the page at the front
  13. * of the queue (the oldest page) is selected. While FIFO is cheap and intuitive, it performs
  14. * poorly in practical application. Thus, it is rarely used in its unmodified form. This
  15. * algorithm experiences Belady's anomaly.
  16. *
  17. * Details of FIFO PRA
  18. * (1) Prepare: In order to implement FIFO PRA, we should manage all swappable pages, so we can
  19. * link these pages into pra_list_head according the time order. At first you should
  20. * be familiar to the struct list in list.h. struct list is a simple doubly linked list
  21. * implementation. You should know howto USE: list_init, list_add(list_add_after),
  22. * list_add_before, list_del, list_next, list_prev. Another tricky method is to transform
  23. * a general list struct to a special struct (such as struct page). You can find some MACRO:
  24. * le2page (in memlayout.h), (in future labs: le2vma (in vmm.h), le2proc (in proc.h),etc.
  25. */
  26. list_entry_t pra_list_head;
  27. /*
  28. * (2) _fifo_init_mm: init pra_list_head and let mm->sm_priv point to the addr of pra_list_head.
  29. * Now, From the memory control struct mm_struct, we can access FIFO PRA
  30. */
  31. static int
  32. _fifo_init_mm(struct mm_struct *mm)
  33. {
  34. list_init(&pra_list_head);
  35. mm->sm_priv = &pra_list_head;
  36. //cprintf(" mm->sm_priv %x in fifo_init_mm\n",mm->sm_priv);
  37. return 0;
  38. }
  39. /*
  40. * (3)_fifo_map_swappable: According FIFO PRA, we should link the most recent arrival page at the back of pra_list_head qeueue
  41. */
  42. static int
  43. _fifo_map_swappable(struct mm_struct *mm, uintptr_t addr, struct Page *page, int swap_in)
  44. {
  45. list_entry_t *head=(list_entry_t*) mm->sm_priv;
  46. list_entry_t *entry=&(page->pra_page_link);
  47. assert(entry != NULL && head != NULL);
  48. //record the page access situlation
  49. /*LAB3 EXERCISE 2: YOUR CODE*/
  50. //(1)link the most recent arrival page at the back of the pra_list_head qeueue.
  51. return 0;
  52. }
  53. /*
  54. * (4)_fifo_swap_out_victim: According FIFO PRA, we should unlink the earliest arrival page in front of pra_list_head qeueue,
  55. * then set the addr of addr of this page to ptr_page.
  56. */
  57. static int
  58. _fifo_swap_out_victim(struct mm_struct *mm, struct Page ** ptr_page, int in_tick)
  59. {
  60. list_entry_t *head=(list_entry_t*) mm->sm_priv;
  61. assert(head != NULL);
  62. assert(in_tick==0);
  63. /* Select the victim */
  64. /*LAB3 EXERCISE 2: YOUR CODE*/
  65. //(1) unlink the earliest arrival page in front of pra_list_head qeueue
  66. //(2) set the addr of addr of this page to ptr_page
  67. return 0;
  68. }
  69. static int
  70. _fifo_check_swap(void) {
  71. cprintf("write Virt Page c in fifo_check_swap\n");
  72. *(unsigned char *)0x3000 = 0x0c;
  73. assert(pgfault_num==4);
  74. cprintf("write Virt Page a in fifo_check_swap\n");
  75. *(unsigned char *)0x1000 = 0x0a;
  76. assert(pgfault_num==4);
  77. cprintf("write Virt Page d in fifo_check_swap\n");
  78. *(unsigned char *)0x4000 = 0x0d;
  79. assert(pgfault_num==4);
  80. cprintf("write Virt Page b in fifo_check_swap\n");
  81. *(unsigned char *)0x2000 = 0x0b;
  82. assert(pgfault_num==4);
  83. cprintf("write Virt Page e in fifo_check_swap\n");
  84. *(unsigned char *)0x5000 = 0x0e;
  85. assert(pgfault_num==5);
  86. cprintf("write Virt Page b in fifo_check_swap\n");
  87. *(unsigned char *)0x2000 = 0x0b;
  88. assert(pgfault_num==5);
  89. cprintf("write Virt Page a in fifo_check_swap\n");
  90. *(unsigned char *)0x1000 = 0x0a;
  91. assert(pgfault_num==6);
  92. cprintf("write Virt Page b in fifo_check_swap\n");
  93. *(unsigned char *)0x2000 = 0x0b;
  94. assert(pgfault_num==7);
  95. cprintf("write Virt Page c in fifo_check_swap\n");
  96. *(unsigned char *)0x3000 = 0x0c;
  97. assert(pgfault_num==8);
  98. cprintf("write Virt Page d in fifo_check_swap\n");
  99. *(unsigned char *)0x4000 = 0x0d;
  100. assert(pgfault_num==9);
  101. cprintf("write Virt Page e in fifo_check_swap\n");
  102. *(unsigned char *)0x5000 = 0x0e;
  103. assert(pgfault_num==10);
  104. cprintf("write Virt Page a in fifo_check_swap\n");
  105. assert(*(unsigned char *)0x1000 == 0x0a);
  106. *(unsigned char *)0x1000 = 0x0a;
  107. assert(pgfault_num==11);
  108. return 0;
  109. }
  110. static int
  111. _fifo_init(void)
  112. {
  113. return 0;
  114. }
  115. static int
  116. _fifo_set_unswappable(struct mm_struct *mm, uintptr_t addr)
  117. {
  118. return 0;
  119. }
  120. static int
  121. _fifo_tick_event(struct mm_struct *mm)
  122. { return 0; }
  123. struct swap_manager swap_manager_fifo =
  124. {
  125. .name = "fifo swap manager",
  126. .init = &_fifo_init,
  127. .init_mm = &_fifo_init_mm,
  128. .tick_event = &_fifo_tick_event,
  129. .map_swappable = &_fifo_map_swappable,
  130. .set_unswappable = &_fifo_set_unswappable,
  131. .swap_out_victim = &_fifo_swap_out_victim,
  132. .check_swap = &_fifo_check_swap,
  133. };