1, Experimental purpose
By simulating several basic page replacement algorithms for request page storage management, understand the characteristics of virtual storage technology, master the basic ideas and implementation process of several basic page replacement algorithms in virtual storage request page storage management, and compare their efficiency.
2, Experimental contents and basic requirements
Based on a virtual storage area and memory workspace, the following algorithms are designed and the page missing times, replacement times and page missing rate are calculated.
1. Most recently unused algorithm (LRU)
2. Simple clock algorithm (CLOCK)
3, Content of experiment report
- (1) Fundamentals of the most recently unused algorithm (LRU)
Each eliminated page is the most recently unused page
Implementation method: in the page table item corresponding to each page, use the access field to record the time t that the page has experienced since it was last accessed. When you need to eliminate a page, select the page with the largest t value in the existing pages, that is, the most recently unused page.
- (2) Basic principle of simple clock algorithm (CLOCK)
Set an access bit for each page, and then link the pages in memory into a circular queue through the link pointer. When a page is accessed, its access location is 1. When you need to eliminate a page, just check the access bit of the page. If it is 0, select this page to switch out; If it is 1, set it to 0, do not change it out temporarily, and continue to check the next page. If all pages in the first round of scanning are 1, set the access bits of these pages to 0 in turn, and then conduct the second round of scanning (there must be pages with access bits of 0 in the second round of scanning. Therefore, a simple CLOCK algorithm selects an obsolete page, which will go through two rounds of scanning at most
4, Code implementation
(1) Most recently unused algorithm (LRU)
#include<stdio.h> #include<stdlib.h> typedef struct lru{ int yehao;//Page number int kuaihao;//Memory block number int time;//Last visited } lru; int qi;//Requested number int kuaishu;//Number of system blocks int yyk;//Number of blocks used int a, b, c; int Qfind(lru *l,int n){ for (int i = 1; i <= yyk;i++){ if(l[i].yehao==n) return i; } return yyk+1; } int mint(lru *l){ l[0].time = 775000; int ans = 0; for (int i = 1; i <= yyk;i++){ if(l[i].time<l[ans].time) ans = i; } return ans; } void print(lru *l){ for (int i = 1; i <= yyk;i++) printf("%d ", l[i].yehao); for (int i = yyk + 1; i <= kuaishu; i++) printf("# "); printf("\n"); } int main(){ kuaishu = 4; lru lQ[kuaishu+1]; yyk = 0; int time = 0; printf("Please enter the request queue:\n"); while(scanf("%d", &qi)&&qi){ ++time; int n = Qfind(lQ, qi); if(n>yyk){//Out of memory, missing page if(yyk<kuaishu){//Allocate space yyk++; }else{//substitution n = mint(lQ); b++; } lQ[n].yehao = qi; lQ[n].kuaihao = n; lQ[n].time = time; a++; }else{//Modification time lQ[n].time = time; } c++; print(lQ); } printf("Page missing times:%d\n Replacement times:%d\n Page missing rate:%.1f\%\n", a, b, (float)a / c*100); } //1 8 1 7 8 2 7 2 1 8 3 8 2 1 3 1 7 1 3 7 0
(2) Simple clock algorithm (CLOCK)
#include<stdio.h> #include<stdlib.h> typedef struct clock{ int yehao;//Page number int kuaihao;//Memory block number int fangwen; struct clock *next; } clock; clock *Q; clock *p, *q; int qi;//Requested number int a, b, c; int flag; clock temp[5] = { {5, 1, 0}, {6, 2, 1}, {3, 3, 0}, {4, 4, 0}, {7, 5, 1}}; clock* Qfind(int qi){ p = q = Q; do{ if(p->yehao==qi){ return p; } p = p->next; } while (p != q); return NULL; } clock* zhihuan(){ p = q = Q; do{ if(p->fangwen==0){ Q = p->next; return p; } p->fangwen = 0; p = p->next; } while (p != q); Q = Q->next; return p; } void print(int f){ printf("Request No.:%d Missing page:", qi); if(f)printf("√"); printf("\n"); p = q = Q; do{ printf("%d ", p->yehao); p = p->next; } while (p != q); printf("\n"); } int main(){ Q = &temp[0]; for (int i = 0; i < 5;i++) temp[i].next = &temp[(i + 1)%5]; printf("Please enter the request queue:\n"); while(scanf("%d",&qi)&&qi){ flag = 0; p = Qfind(qi); if(p){ p->fangwen = 1; }else{//Missing page flag = 1; p = zhihuan(); p->yehao = qi; p->fangwen = 1; b++; a++; } c++; print(flag); } printf("Page missing times:%d\n Replacement times:%d\n Page missing rate:%.1f\%\n", a, b, (float)a / c*100); } //1 3 4 2 5 6 3 4 7 0
5, Experimental results