实验报告
实验内容
虚拟存储管理: 编写一个 C 程序模拟实现课件 Lecture24 中的请求页面置换算法包括FIFO、LRU (stack and matrix implementation)、Second chance,并设计输入用例验证结果。
实验环境
Ubuntu 20.04.2.0(64位)
基础知识
- FIFO: 如果发生缺页中断,需要换出一个页面的时候,总是选择最早进入内存的页面,即选择在内存中驻留时间最久的页面进行换出。
例:
- LRU:如果发生缺页中断,选择最长时间未使用的页面进行换出。
例:
- Second chance:
每个帧(frame)有一个second chance位,也叫做引用位。
当一个frame被引用到,它的second chance位设置为1。这表示该frame后面还有可能会被引用到,所以下次置换先跳过这个frame,也就是再给它一次机会留在内存中。这样可以减少frame置换,提高页面操作效率。
当一个新的页面被读到内存中时,它的second chance被设置为0。
当你需要替换内存中的一个页面时,使用轮询的方式来查找可以被替换的页面:
如果页面的second chance是1,那么置为0,继续查找;
如果页面的second chance是0,那么将这个页面置换出去。
例:
(参考资料1:课件)
(参考资料2)
(参考资料3)
实验设计
(详细过程请看代码注释)
- page_replacement.c
#include <stdio.h>
#include <stdlib.h>
#define MAXPAGE 3 // Max number of page in memory
void FIFO(int task_list[], int task_num);
void LRUstack(int task_list[], int task_num);
void LRUmatrix(int task_list[], int task_num);
void SecondChance(int task_list[], int task_num);
int main()
{
int task_num = 20; // total number of tasks
int task_list[20] = {7, 0, 1, 2, 0, 3, 0, 4, 2, 3, 0, 3, 2, 1, 2, 0, 1, 7, 0, 1}; // execution order of tasks
printf("Please enter the number of task(<= 20): ");
scanf("%d", &task_num);
printf("\n");
printf("Please enter the task list: ");
for (int i = 0; i < task_num; i++)
{
scanf("%d", &task_list[i]);
}
printf("\n");
printf("Max number of page in memory is %d.\n", MAXPAGE);
printf("\n\n");
// FIFO
printf("Mode: FIFO.\n");
FIFO(task_list, task_num);
printf("\n\n");
// stack implementation of LRU
printf("Mode: LRU(stack implementation).\n");
LRUstack(task_list, task_num);
printf("\n\n");
// matrix implementation of LRU
printf("Mode: LRU(matrix implementation).\n");
LRUmatrix(task_list, task_num);
printf("\n\n");
// second-chance
printf("Mode: Second chance.\n");
SecondChance(task_list, task_num);
printf("\n\n");
return 0;
}
void FIFO(int task_list[], int task_num)
{
int page[MAXPAGE]; // page table
int point = 0; // point to first coming task
int fault = 0; // the count of page fault
int i = 0, j = 0;
// initial
for (i = 0; i < MAXPAGE; i++)
{
page[i] = -1;
}
for (i = 0; i < task_num; i++)
{
for (j = 0; j < MAXPAGE; j++)
{
if (page[j] == task_list[i])
break;
}
if (j == MAXPAGE) // the task's page is not in memory
{
fault++;
page[point] = task_list[i]; // replace the first coming task
point = (point + 1) % MAXPAGE;
}
printf("Now task list is: ");
for (int k = i; k < task_num; k++)
{
printf("%d ", task_list[k]);
}
printf("\n");
printf(" So the page list in the process should change to: ");
for (j = 0; j < MAXPAGE; j++)
{
printf("%d ", page[j]);
}
printf("\n");
printf(" Now the number of page fault is: %d\n\n", fault);
}
printf("The number of page fault is %d.\n", fault);
}
void LRUstack(int task_list[], int task_num)
{
int stack[MAXPAGE]; // stack of page in memory
int top = -1; // point to the top element of stack
int fault = 0; // the count of page fault
int i, j;
// initial
for (i = 0; i < MAXPAGE; i++)
{
stack[i] = -1;
}
for (i = 0; i < task_num; i++)
{
for (j = 0; j <= top; j++)
{
if (stack[j] == task_list[i])
break;
}
if (j == top + 1 && top + 1 < MAXPAGE) // stack is not full and the task's page is not in memory
{
fault++;
stack[++top] = task_list[i];
}
else if (j == MAXPAGE) // stack is full and the task's page is not in memory
{
fault++;
// replace the bottom element of stack and put the new task to top
for (int k = 0; k < top; k++)
{
stack[k] = stack[k + 1];
}
stack[top] = task_list[i];
}
else // the task's page is in memory
{
// move the task to top
int temp = stack[j];
for (int k = j; k < top; k++)
{
stack[k] = stack[k + 1];
}
stack[top] = temp;
}
printf("Now task list is: ");
for (int k = i; k < task_num; k++)
{
printf("%d ", task_list[k]);
}
printf("\n");
printf(" So the page list in the process should change to: ");
for (j = 0; j <= top; j++)
{
printf("%d ", stack[j]);
}
printf("\n");
printf(" Now the number of page fault is: %d\n\n", fault);
}
printf("The number of page fault is %d.\n", fault);
}
void LRUmatrix(int task_list[], int task_num)
{
int matrix[MAXPAGE][MAXPAGE] = {0}; // matrix for page in memory
int page[MAXPAGE]; // page in memory
int fault = 0; // the count of page fault
int i, j;
// initial by the first three task
for (i = 0; i < MAXPAGE; i++)
{
page[i] = task_list[i];
for (j = 0; j < MAXPAGE; j++)
{
matrix[i][j] = 1;
}
for (j = 0; j < MAXPAGE; j++)
{
matrix[j][i] = 0;
}
}
fault = 3;
printf(" Now the page list in the process is: ");
for (j = 0; j < MAXPAGE; j++)
{
printf("%d ", page[j]);
}
printf("\n");
printf(" Now the number of page fault is: %d\n\n", fault);
for (i = MAXPAGE; i < task_num; i++)
{
for (j = 0; j < MAXPAGE; j++)
{
if (page[j] == task_list[i])
{
break;
}
}
if (j == MAXPAGE) // the task's page is not in memory
{
fault++;
// find the task with the smallest total of row elements
int rep = -1;
int minRow = MAXPAGE;
int minRow_j = -1;
for (int k = 0; k < MAXPAGE; k++)
{
int sumRow = 0;
for (int t = 0; t < MAXPAGE; t++)
{
sumRow += matrix[k][t];
}
if (sumRow < minRow)
{
rep = page[k];
minRow = sumRow;
minRow_j = k;
}
}
// replace the task found, and first make it's row all 1, then make it's volumn all 0
page[minRow_j] = task_list[i];
for (int k = 0; k < MAXPAGE; k++)
{
matrix[minRow_j][k] = 1;
}
for (int k = 0; k < MAXPAGE; k++)
{
matrix[k][minRow_j] = 0;
}
}
else // the task's page is in memory
{
// first make it's row all 1, then make it's volumn all 0
for (int k = 0; k < MAXPAGE; k++)
{
matrix[j][k] = 1;
}
for (int k = 0; k < MAXPAGE; k++)
{
matrix[k][j] = 0;
}
}
printf("Now task list is: ");
for (int k = i; k < task_num; k++)
{
printf("%d ", task_list[k]);
}
printf("\n");
printf(" So the page list in the process should change to: ");
for (j = 0; j < MAXPAGE; j++)
{
printf("%d ", page[j]);
}
printf("\n");
printf(" Now the number of page fault is: %d\n\n", fault);
}
printf("The number of page fault is %d.\n", fault);
}
void SecondChance(int task_list[], int task_num)
{
int page[MAXPAGE]; // page in memory
int use[MAXPAGE]; // if this page was used before, set 1, else set 0
int point = 0; // pointer in circular queue
int fault = 0; // the count of page fault
int i = 0, j = 0;
// initial
for (i = 0; i < MAXPAGE; i++)
{
page[i] = -1;
use[i] = 0;
}
for (i = 0; i < task_num; i++)
{
for (j = 0; j < MAXPAGE; j++)
{
if (page[j] == task_list[i])
break;
}
if (j == MAXPAGE) // the task's page is not in memory
{
fault++;
/* if the task that the pointer points to has been used before(use = 1),
then set it's use to 0 and let the pointer points to the next task*/
while (use[point] == 1)
{
use[point] = 0;
point = (point + 1) % MAXPAGE;
}
// if the task that the pointer points to has not been used before(use = 0), then replace it
page[point] = task_list[i];
point = (point + 1) % MAXPAGE;
}
else // the task's page is in memory, it means the task was used before, so set it's use to 1
{
use[j] = 1;
}
printf("Now task list is: ");
for (int k = i; k < task_num; k++)
{
printf("%d ", task_list[k]);
}
printf("\n");
printf(" So the page list in the process should change to: ");
for (j = 0; j < MAXPAGE; j++)
{
printf("%d ", page[j]);
}
printf("\n");
printf(" use list: ");
for (j = 0; j < MAXPAGE; j++)
{
printf("%d ", use[j]);
}
printf("\n");
printf(" Now the number of page fault is: %d\n\n", fault);
}
printf("The number of page fault is %d.\n", fault);
}
实验过程
执行20个任务,最多分配给进程3个帧,任务队列如下:
-
FIFO结果:
可以看到,每一次页面置换都是将最早在内存中的页面置换出去,执行过程与上述基础知识中FIFO图的过程一致,最后的缺页中断数也一致。 -
LRU (stack implementation) 结果:
可以看到,每一次页面置换都是把栈底的页面置换出去,如果请求的页面本来就在栈中,则将其移到栈顶。
通过在草稿纸上一步步操作并对比,可以看到,执行过程与实际操作的过程一致,最后的缺页中断数也一致。 -
LRU (matrix implementation) 结果:
每一次页面置换都是把矩阵中对应行元素之和最小的页面置换出去。
通过在草稿纸上一步步操作并对比,可以看到,执行过程与实际操作的过程一致,最后的缺页中断数也一致。 -
Second chance 结果:
可以看到,每一次页面置换都是把标记为0的最早在内存中的页面置换出去,如果请求的页面已经在内存中存在,则将其标记为1。
通过在草稿纸上一步步操作并对比,可以看到,执行过程与实际操作的过程一致,最后的缺页中断数也一致。