Experiment 8 * page replacement algorithm simulation of virtual memory system

Posted by uidzer0b on Mon, 03 Jan 2022 01:24:19 +0100

1, Experimental purpose
Through the simulation of page, page table, address conversion and page replacement process, we can deepen the understanding of the page replacement principle and implementation process of virtual page memory management system.
2, Overall design (including background knowledge or basic principles and algorithms, or module introduction, design steps, etc.)
Fundamentals and principles:
Replacement strategy: when a new page needs to be called in, select which physical page in memory is replaced.
The goal of page replacement algorithm: call out the pages that are no longer used in the future or less used in the short term. Generally, it should be predicted according to the past statistical data under the guidance of locality principle to reduce the number of missing pages.
Common page replacement algorithms include:
1) Optimal permutation algorithm (OPT): eliminate pages that are "no longer used in the future" or "appear farthest from the current" during permutation.
2) First in first out replacement algorithm (FIFO): during replacement, the page that enters the memory first is eliminated, that is, the page that resides in the memory for the longest time is selected to be replaced.
3) Most recently unused replacement algorithm (LRU): during replacement, the pages that have not been used for the longest time in the recent period are eliminated, that is, the pages farthest from the current are selected for elimination.
4) Clock algorithm: also known as not recently used algorithm (NRU), it is a compromise between LRU and FIFO.
Test requirements:
(1) Assuming that 10 instructions can be stored in each page, the number of memory blocks (page boxes) allocated to a job is 4. (2) simulate the execution process of a job with C language. The job has 320 instructions, that is, its address space is 32 pages. At present, all its pages have not been transferred into memory. During the simulation, if the accessed instruction is already in memory, its physical address is displayed and the next instruction is transferred. If the accessed instruction has not been loaded into the memory, page missing occurs. At this time, record the number of page missing and transfer the corresponding page into the memory; If the job has been loaded in all four memory blocks, page replacement is required; Finally, the physical address is displayed and the next instruction is transferred. After all 320 instructions are executed, please calculate and display the page missing rate during job operation. (3) For the replacement algorithm, please consider OPT, FIFO and LRU algorithms respectively. (4) The test case (instruction sequence in the job) is generated according to the principle of generating an instruction sequence through random numbers, with a total of 320 instructions.
Topic analysis: one job allocates 4 memory blocks and each page stores 10 instructions, which means that up to 40 instructions can be transferred at one time, and the whole program runs a total of 320 instructions. Therefore, each instruction needs to be compared to see whether it is in memory, In, the physical address (number of page frames + offset) is displayed, which can be simulated by a 4 * 10 two-dimensional array. If it is not, it needs to be transferred into memory. If it is not full, it will be directly transferred in. If it is full, it will be transferred in by selecting the replacement algorithm.
The generation of instructions is a random number and has its special execution law: first, a number m (0-319) is randomly generated, then the instruction m+1 is executed, and then a number m1 is randomly generated with 0-m+1 as the interval, m1+1 is executed, and then a random number m2 is generated with m1+2-319 as the interval, m2 is executed, and then 320 instructions are executed in turn.
Design steps:
(1) First, 320 instructions to be executed are randomly generated and stored in a one-dimensional array.
(2) Then execute the instructions in turn. There are three situations in the memory of the page where the instructions are located.
(3) The first is that the page where the instruction is located in the disk is in memory and directly outputs the physical address.
(4) Second, the page where the instruction is located is not in memory. At this time, the memory page is not full and can be directly transferred in.
(5) Third, the page where the instruction is located is not in memory. At this time, the page is full, and the page is changed through the scheduling algorithm.
(6) Replacement algorithm 1: OPT, look back from the current instruction to find the latest used or unused pages in memory for replacement.
(7) Replacement algorithm 2: FIFO records i every time a page is transferred into memory, and then compares the i of four pages with the same one, and the smallest one is changed out.
(8) Replacement algorithm 3: each time the LRU executes an instruction, it records i for the page, and then compares the recorded i. the smallest is the latest and earliest used, and replaces the page.
(9) Main() main function, enter and select, and call the corresponding algorithm.
Full code:

#include<stdio.h>
#include<stdlib.h>
int neicun[4]={-1,-1,-1,-1};//Indicates that the process memory page is 4 pages, and the initial value is - 1 
int zhilin[321];     //Store 320 instructions to be executed, facilitate module 3, round up an integer, and discard the last one
int count;         //Used to record the number of missing pages
int time1[4];  //It is used to record the time when the page has just been transferred into memory 
int time2[4];  //Record the time when the page was recently called   
//The time is measured by i. the smaller i is, the earlier the instructions in the page are executed   
void chanshengzhiling()//Randomly generated instruction sequence 
{
	int m1,m2,m3;
	for(int i=0;i<321;){//3 at a time
		m1=rand()%320;//The rand() function has no parameters and cannot specify a range
		zhilin[i++]=m1+1;//The calculated instructions are stored in the array 
		m2=rand()%(m1+1);    //Post + + use before add 
		zhilin[i++]=m2+1;
		m3=rand()%(318-m2)+m2+2;//The front and back parts represent 0 - (317-m2) and add it to become the specified interval 
		zhilin[i++]=m3;//Don't move back 
	}
    printf("The random 320 instruction sequences are:\n");
	for(int i=0;i<320;i++){
        if(i%45==0)printf("\n");
        printf("%3d ",zhilin[i]);
	} printf("\n\n");
} 
bool panduan1(int i) //Judge whether the i-th instruction is in memory 
{
	printf("The first%3d Instruction number is:%3d: ",i,zhilin[i]);
	for(int j=0;j<4;j++){
		if(neicun[j]==(zhilin[i]/10)){//Indicates that the page where the instruction is located is in memory 
			time2[j]=i;    //Update the time when the page was recently called in memory 
			printf("The page where the instruction is located is in memory. The physical address is: page number is:%d,The offset is:%d\n",j,zhilin[i]%10);
			return true;
		}
	} 
	return false;
}
bool panduan2(int i) //Determine whether the memory block is full 
{
	for(int j=0;j<4;j++){
		if(neicun[j]==-1)  {//Indicates that there are free memory blocks
			neicun[j]=zhilin[i]/10;//Simulate calling the page where the instruction is located into memory
			time1[j]=i;     //Record the time when the page is first transferred into memory 
			time2[j]=i;     //Record the most recent call time of the page 
			printf("The page on which the instruction is located is not in memory,When the memory block is not full, it is directly transferred into the memory. Its physical address is: page number:%d,Offset:%d\n",j,zhilin[i]%10);
			return true;
		}
	 } 
	return false;
}
void OPT()
{
	count=0;
	printf("Optimal permutation algorithm(OPT):\n");
	for(int i=0;i<320;i++){
		bool jieguo1=panduan1(i);
		if(!jieguo1) { //Indicates that the page where the instruction is located is not in memory
		    count++;
			bool jieguo2=panduan2(i);
			if(!jieguo2)     {//Indicates that the memory block is full 
			 	int juli[4]={-1,-1,-1,-1}; //Store the distance from four pages to the next scheduled 
				for(int j=0;j<4;j++){
					if(juli[j]==-1) {//Indicates that it has not yet appeared
						for(int k=i+1;k<320;k++){
							if(neicun[j]==zhilin[k]/10) {//The next time is scheduled, record the distance 
								juli[j]=k-i;break;
							}
						}
					}	
				} 
				int flag=0,index=-1; 
				for(int j=0;j<4;j++){
					if(juli[j]==-1){//Indicates that this page has not been called in the future, then replace this page 
						index=j; flag=1;break;
					}
				}
				if(flag==0) {//Indicates that all pages in memory have appeared, and the farthest distance is taken
					int max=-1;
					for(int j=0;j<4;j++){
						if(juli[j]>max){
							max=juli[j];index=j;
						}
					}
				}
				neicun[index]=zhilin[i]/10;//Simulate calling the page where the instruction is located into memory
			    printf("The page on which the instruction is located is not in memory,Memory block full, using OPT Algorithm, whose physical address is: page number:%d,Offset:%d\n",index,zhilin[i]%10);
			}
		}
	}
	printf("\nOPT The page missing rate of the algorithm is:%f",1.0*count/320);
}
void FIFO()
{
	count=0;
	printf("Optimal permutation algorithm(OPT):\n");
	for(int i=0;i<320;i++){
		bool jieguo1=panduan1(i);
		if(!jieguo1) {//Indicates that the page where the instruction is located is not in memory
			count++;
			bool jieguo2=panduan2(i);
			if(!jieguo2){//Indicates that the memory is full 
			 	int min=9999,index=-1;
			 	for(int j=0;j<4;j++){
			 		if(time1[j]<min){
			 			min=time1[j];index=j;    //Get the earliest page called into memory 
					}
				}
			 	neicun[index]=zhilin[i]/10;//Simulate calling the page where the instruction is located into memory
			 	time1[index]=i;   
			    printf("The page on which the instruction is located is not in memory,Memory block full, using FIFO Algorithm, whose physical address is: page number:%d,Offset:%d\n",index,zhilin[i]%10);
			}
		}
	}
	printf("\nFIFO The page missing rate of the algorithm is:%f",1.0*count/320);
}
void LRU()
{
	count=0;
	printf("Optimal permutation algorithm(OPT):\n");
	for(int i=0;i<320;i++){
		bool jieguo1=panduan1(i);
		if(!jieguo1) {//Indicates that the page where the instruction is located is not in memory
			count++;
			bool jieguo2=panduan2(i);
			if(!jieguo2) {//Indicates that the memory is full
			 	int min=999,index=-1;
			 	for(int j=0;j<4;j++){
			 		if(time2[j]<min){
			 			min=time2[j];index=j;   //Get the most recently transferred in page
					}
				}
			 	neicun[index]=zhilin[i]/10;//Simulate calling the page where the instruction is located into memory
			 	time2[index]=i;
			    printf("The page on which the instruction is located is not in memory,Memory block full, using FIFO Algorithm, whose physical address is: page number:%d,Offset:%d\n",index,zhilin[i]%10);
			}
		}
	}
	printf("\nLRU The page missing rate of the algorithm is:%f",1.0*count/320);
}

int main()
{
	printf("Simulation of page replacement algorithm in virtual memory system\n\n");
	chanshengzhiling();
	printf("Please enter the scheduling algorithm you want to select(1->OPT 2->FIFO 3->LRU): ");
	int choice;
	scanf("%d",&choice);
	if(choice==1) OPT();
	else if(choice==2) FIFO();
	else if(choice==3) LRU();
	else printf("Selection error, there is no such algorithm selection.");
	return 0; 
}


Topics: Algorithm