1、第5次实验14281147王飞实验五页面置换算法1. 实验目的设计和实现最佳置换算法、先进先出置换算法、最近最久未使用置换算法、页面缓冲置换算法;通过页面访问序列随机发生器实现对上述算法的测试及性能比较。2. 实验基础知识及背景说明(1) 请求分页虚拟内存管理请求分页虚拟内存管理是建立在基本分页基础上的,为了能支持虚拟存储器功能,而增加了请求调页功能和置换功能。(2) 工作集多数程序都显示出高度的局部性,也就是说,在一个时间段内,一组页面被反复引用。这组被反复引用的页面随着时间的推移,其成员也会发生变化。有时这种变化是剧烈的,有时这种变化则是渐进的。我们把这组页面的集合称为工作集(3) 缺页率
2、缺页中断次数/总的页面访问次数3. 实验前提说明(1) 页表用整数数组或结构数组来表示(2) 页面访问序列串是一个整数序列,整数的取值范围为0到N - 1。页面访问序列串中的每个元素p表示对页面p的一次访问(3) 符合局部访问特性的随机生成算法a. 确定虚拟内存的尺寸N,工作集的起始位置p,工作集中包含的页数e,工作集移动率m(每处理m个页面访问则将起始位置p +1),以及一个范围在0和1之间的值tb. 生成m个取值范围在p和p + e间的随机数,并记录到页面访问序列串中c. 生成一个随机数r,0 r 1d. 如果r 页面缓冲置换算法改进型clock置换算法最近最久未使用算法=先进先出置换算法
3、。(3) 对比内存块数为3和内存块数为5两种情况下的同一序列下的同一,可以发现,算法的缺页率还跟分配的内存块数有关系,分配的内存块数越多,缺页率越低。这与直观感受是一致的,即导入内存的块数越多,发生缺页的可能性就越小。8. 附录(程序代码)PBA.cpp#include stdafx.h#include stdio.h#includestdlib.h#includetime.h#define M 32 /物理内存块数#define N 64 /虚拟内存块数struct LNode int data; int flag;/访问位 int modify;/修改位 LNode* next;struc
4、t Link int num;/当前链表上的结点数 LNode* next;void generate();/生成访问序列bool isInNodes (int n); /void addToLink (int data, int type);void emptyIdle();void emptyModi();void PBA (int n);int size = 3;int p;/工作集的起始位置int table32;/物理内存,每一个元素代表一个页面int access32; /访问序列int memo3 = -1, -1, -1 ;int lost = 0;/没找到的页面数int in
5、dex = 0;/指示当前下标LNode* nodes;/改进型Clock置换算法用到的数据结构Link idle;Link modified;int _tmain (int argc, _TCHAR* argv) int i = 0, j = 0; generate(); printf (页面缓冲置换算法(PBA)n); idle.num = 0; idle.next = NULL; modified.num = 0; modified.next = NULL; nodes = (LNode*) malloc (size * sizeof (LNode); for (i = 0; i siz
6、e; i+) nodesi.data = -1; nodesi.flag = 0; nodesi.modify = 0; nodesi.next = NULL; for (i = 0; i 32; i+) PBA (i); for (j = 0; j size; j+) printf (%d , nodesj.data); printf (n); printf (页面缓冲置换算法(PBA)缺页率:%f %dn, lost / 32.0, lost); getchar(); getchar(); return 0;void generate() srand ( (unsigned) time (
7、NULL); /用时间做种,每次产生随机数不一样 p = rand() % 64; int m = 8, e = 8; int i, j; double t; t = rand() % 10 / 10.0; for (i = 0; i 4; i+) for (j = i * m; j (i + 1) *m; j+) accessj = (p + rand() % e) % 64; double r = (rand() % 10) / 10.0; if (r t) p = rand() % 64; else p = (p + 1) % 64; bool isInNodes (int n) int
8、 i; for (i = 0; i data = accessn) if (q != NULL) q-next = p-next; p-next = NULL; idle.num-; break; else idle.next = NULL; q = p; p = p-next; if (p = NULL) p = modified.next; while (p != NULL) if (p-data = accessn) if (p = modified.next) modified.next = p-next; else q-next = p-next; p-next = NULL; mo
9、dified.num-; if (modified.num = 0) modified.next = NULL; break; q = p; p = p-next; return p;void PBA (int n) if (isInNodes (n) printf (已装入内存n); else if (index = size) LNode *p; if ( (p = isinLinks (n) != NULL) nodes = (LNode*) realloc (nodes, (size + 1) * sizeof (LNode); nodessize .data = p-data; no
10、dessize.flag = p-flag; nodessize.modify = p-modify; nodessize.next = p-next; free (p); size+; index+; else lost+;/缺页 if (nodesn % 3.modify = 1) addToLink (nodesn % 3.data, 1); else addToLink (nodesn % 3.data, 0); nodesn % 3.data = accessn; nodesn % 3.flag = 1; nodesn % 3.next = NULL; if (rand() % 10
11、 4) nodesn % 3.modify = 0; else nodesn % 3.modify = 1; else nodesindex.data = accessn; nodesindex.flag = 1; nodesindex.next = NULL; if (rand() % 10 data = data; q-flag = 1; if (type = 1) q-modify = 1; p = modified.next; else q-modify = 0; p = idle.next; q-next = NULL; if (p = NULL) if (type = 0) idl
12、e.next = q; else modified.next = q; else while (p) if (p-next = NULL) p-next = q; break; else p = p-next; if (type = 0) idle.num += 1; if (idle.num = 10) emptyIdle(); else modified.num += 1; if (modified.num = 10) emptyModi(); void emptyIdle () LNode* p; p = idle.next; while (p) idle.next = p-next;
13、free (p); p = idle.next; idle.num = 0;void emptyModi() LNode* p; p = modified.next; while (p) modified.next = p-next; free (p); p = modified.next; modified.num = 0;FIFO.cpp/ FIFO.cpp : 定义控制台应用程序的入口点。#include stdafx.h#includestdio.h#includestdlib.h#includetime.htypedef struct node int num; node* next
14、; Node, *pNode;typedef struct queue int n; pNode front; pNode rear; Queue, *pQueue;void initQueue (pQueue q);void push (pQueue q, int num);void pop (pQueue q);void destroy (pQueue q);bool findInQueue (pQueue q, int num);void generate();void fifoTest();void fifo (pQueue q, int num);int access32;/访问序列
15、int size = 3;/给进程分配的内存的大小int lost = 0;/缺页数int _tmain (int argc, _TCHAR* argv) /generate(); fifoTest(); getchar(); getchar(); return 0;void initQueue (pQueue q) q-rear = (pNode) malloc (sizeof (Node); if (q-rear = NULL) printf (failedn); else q-front = q-rear; q-rear-next = NULL; q-front-next = NULL; q-n = 0; void push (pQueue q, int num) pNode p = (pNode) malloc (sizeof (Node); if (p = NULL) printf (failed); else p-next = NULL; p-num = num; if (q-front = q-rear) q-front-next = p; q-rear = p; else
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1