Operating system experiment 3 - Implementation of request page storage management simulation program

Posted by prakash on Sat, 29 Jan 2022 17:09:48 +0100

Operating system experiment 3 - Implementation of request page storage management simulation program

Experimental description

Experiment content:

Write a request page storage management program to simulate the memory allocation and page replacement under the request page storage management mode.

Purpose of the experiment:

Memory management is the core module of the operating system. It can make rational use of memory, which will affect the performance of the whole computer system to a great extent. The allocation and recycling of memory are related to the way of memory management. This experiment requires students to independently design and implement the memory allocation and page replacement simulation program under the request page storage management mode, so as to deepen their understanding of the page replacement algorithm and the request page storage management mode.

Test requirements:

  1. You can randomly input the number of memory blocks allocated to a process and the page access sequence of the process. See the description of test case format input for specific information.
  2. The best algorithm OPT (when there are multiple pages that can be replaced, the replacement shall be carried out according to the first in first out principle), the first in first out algorithm FIFO and the least recently used algorithm LRU are used for page replacement respectively, in which the LRU algorithm is realized by the stack method.
  3. Displays the details of the page list loaded by the memory block when the page changes, displays whether page replacement occurs, and calculates the number and rate of page missing. See the description of test case format output for specific information.

The test case format is as follows:

Input:

Algorithm (1)--OPT,2--FIFO,3--LRU)
Number of memory blocks
 Page sequence (Page 1),Page 2,Page 3,...)

Output:

When the page changes, the memory block is loaded into page list 1-Hit/When the page changes, the memory block is loaded into page list 2-Hit/...
Page missing times

Of which:
When the page changes, the memory block loads the page list:
  (1) Memory block 1 load page,Load page of memory block 2,Load page of memory block 3...,By when no page is loaded"-"express
  (2) Hit or not: 1-Hit, 0-Missing page
Test inputExpected outputtime limitMemory limitAdditional process
Test case 11
3
1,2,3,4,1,2,5,1,2,3,4,5
1,-,-,0/1,2,-,0/1,2,3,0/1,2,4,0/1,2,4,1/1,2,4,1/1,2,5,0/1,2,5,1/1,2,5,1/3,2,5,0/3,4,5,0/3,4,5,1
7
1 second64M0
Test case 22
4
1,2,3,4,1,2,5,1,2,3,4,5
1,-,-,-,0/1,2,-,-,0/1,2,3,-,0/1,2,3,4,0/1,2,3,4,1/1,2,3,4,1/5,2,3,4,0/5,1,3,4,0/5,1,2,4,0/5,1,2,3,0/4,1,2,3,0/4,5,2,3,0
10
1 second64M0
Test case 33
3
1,2,3,4,1,2,5,1,2,3,4,5
1,-,-,0/1,2,-,0/1,2,3,0/2,3,4,0/3,4,1,0/4,1,2,0/1,2,5,0/2,5,1,1/5,1,2,1/1,2,3,0/2,3,4,0/3,4,5,0
10
1 second64M0

Design ideas

Although the page data input each time is only the page serial number, the priority change after each page is accessed and the distance information required for the next visit of the current page need to be calculated in the algorithm. Therefore, all the required information is stored in the form of structure array.

Temporary array plays the role of auxiliary output in LRU algorithm.

struct Memory
{
	int id;			//Serial number
	int priority;	//The first memory priority is 0, followed by 1 
	int distance;	//Next visit and current distance
}memory[1010], memory2[1010];//Memory sequence, temporary array

The outline design of the program is shown in the figure below:

  1. The main () function is the entrance of the main program, controls the program flow, and selects the corresponding algorithm module to run according to the input scheduling signal
  2. The input () function is an input function that accepts program input
  3. The output () function is an output function that outputs the information of page hit and missing page replacement
  4. OPT () function is the best replacement algorithm, which calculates the best page scheduling scheme according to the known page sequence and priority order
  5. FIFO () function is a first in first out algorithm, which performs page replacement according to the arrival order of pages
  6. LRU () function is the longest unused algorithm. It schedules pages according to the usage of pages. Here, a temporary array is used to assist in information output
int main();		//Main program entry
void input();	//Input function
void output(int k);//Output function
void OPT();		//Optimal permutation algorithm
void FIFO();	//First in first out algorithm
void LRU();		//Most recently unused algorithm

Computer code

The code is written in C + + language

#define _CRT_SECURE_NO_WARNINGS 
#include<iostream> 
#include<cstdio> 
#include<cstdlib> 
#include<algorithm> 
#include<string> 
using namespace std;
struct Memory
{
	int id;//Serial number 
	int priority;//The first memory priority is 0, followed by 1 
	int distance;//Next visit and current distance 
}memory[1010], memory2[1010];//Memory sequence, temporary array
int que[1010];//Page sequence 
void input();//Input function 
void output(int k);//Output function 
void OPT();//Optimal permutation algorithm
void FIFO();//First in first out algorithm
void LRU();//Most recently unused algorithm
int sig;//Algorithm selection flag 
int memoryk, num, total;//Number of memory blocks, number of pages, number of missing pages 
int pageFlag;//Missing page flag 
const int isPage = 1;//Hit 
const int noPage = 0;//Missing page 
int main()
{
	//Program input 
	input();

	//selection algorithm  
	switch (sig)
	{
		case 1:OPT(); break;
		case 2:FIFO(); break;
		case 3:LRU(); break;
	}
	return 0;
}
void input()
{
	//freopen("osin.txt", "r", stdin); 
	//freopen("osout.txt", "w", stdout); 

	sig = 0;
	cin >> sig;//Algorithm selection flag 

	memoryk = 0;
	total = 0;
	cin >> memoryk;//Number of memory blocks 

	num = 0;
	char c;
	while (scanf("%d", &que[num]))//Enter page sequence 
	{
		num++;
		c = getchar();
		if (c == '\n')//If a newline character is encountered, the input ends 
		{
			break;
		}
		else if (c == ',')//If a comma is encountered, continue reading 
		{
			continue;
		}
	}
	for (int i = 0; i < memoryk; i++)//Memory initialization 
	{
		memory[i].id = -1;
		memory[i].priority = i;
	}
}
void output(int k)
{
	for (int i = 0; i < memoryk; i++)//Output in memory page sequence 
	{
		if (memory[i].id != -1)//There are pages in memory 
		{
			printf("%d,", memory[i].id);
		}
		else//No pages in memory 
		{
			printf("-,");
		}
	}
	if (k != num - 1)//Not to the last page 
	{
		printf("%d/", pageFlag);
	}
	else//last page 
	{
		printf("%d\n", pageFlag);
		printf("%d\n", total);
	}
}
void OPT()
{
	int optFlag = 0;//The OPT page replacement flag replaces the page with the subscript optFlag 
	for (int i = 0; i < num; i++)
	{
		int tmp = 0;//Memory block subscript 
		while (tmp < memoryk)
		{
			if (que[i] == memory[tmp].id)//If there is this page in memory, it will be output 
			{
				pageFlag = isPage;
				output(i);
				break;
			}
			if (memory[tmp].id == -1)//If there is no such page in memory, it will be added to memory 
			{
				memory[tmp].id = que[i];
				total++;
				pageFlag = noPage;
				output(i);
				break;
			}
			tmp++;
		}
		//Run here to prove that the page has not been hit after searching the memory 
		  //Eliminate the page with the smallest sequence number among the pages farthest from the current next visit. If the distance is equal, the page with lower priority will be eliminated 
		if (tmp == memoryk)
		{
			for (int j = 0; j < memoryk; j++)//Distance initialization 
			{
				memory[j].distance = 99999;
			}
			for (int j = 0; j < memoryk; j++)//Calculate distance 
			{
				int dis = 0;
				for (int k = i + 1; k < num; k++)//Record the distance of the next page with the same serial number 
				{
					dis++;
					if (que[k] == memory[j].id)//Update distance 
					{
						memory[j].distance = dis;
						break;
					}
				}
			}
			int max_dis = memory[0].distance;//Find the maximum distance 
			int min_prority = memory[0].priority;//Find minimum priority 
			for (int k = 0; k < memoryk; k++)
			{
				if (memory[k].distance == max_dis)
				{
					if (memory[k].priority <= min_prority)
					{
						min_prority = memory[k].priority;
						max_dis = memory[k].distance;
						optFlag = k;
					}
				}
				else if (memory[k].distance > max_dis)
				{
					min_prority = memory[k].priority;
					max_dis = memory[k].distance;
					optFlag = k;
				}
			}
			//Adjust priority 
			memory[optFlag].priority = memoryk;
			for (int k = 0; k < memoryk; k++)
			{
				memory[k].priority--;
			}
			//Missing page processing 
			memory[optFlag].id = que[i];
			pageFlag = noPage;
			total++;
			output(i);
		}
	}
}
void FIFO()
{
	int fifoFlag = 0;//FIFO page replacement flag to replace the page with fifoFlag subscript 
	for (int i = 0; i < num; i++)
	{
		int tmp = 0;//Memory block subscript 
		while (tmp < memoryk)
		{
			if (que[i] == memory[tmp].id)//If there is this page in memory, it will be output 
			{
				pageFlag = isPage;
				output(i);
				break;
			}
			if (memory[tmp].id == -1)//If there is no such page in memory, it will be added to memory 
			{
				memory[tmp].id = que[i];
				total++;
				pageFlag = noPage;
				output(i);
				break;
			}
			tmp++;
		}
		//Run here to prove that the page has not been hit after searching the memory 
		//Pages are eliminated in the order of FIFO 
		if (tmp == memoryk)
		{
			if (fifoFlag == memoryk)//Ensure that the replacement range is between [0-memoryk] 
			{
				fifoFlag = 0;
			}
			//Missing page processing 
			memory[fifoFlag].id = que[i];
			fifoFlag++;
			pageFlag = noPage;
			total++;
			output(i);
		}
	}
}
void LRU()
{
	int empty;//Does the memory block contain free areas
	int lruFlag = 0;//LRU page replacement flag, recording the subscript of the first free area
	int x;//Temporary array subscript
	for (int i = 0; i < num; i++)
	{
		empty = 0;
		lruFlag = 0;

		for (int j = 0; j < memoryk; j++)//Find free area
		{
			if (memory[j].id == -1)
			{
				empty = 1;
				lruFlag = j;
				break;
			}
		}

		int tmp = 0;//Memory block subscript
		while (tmp < memoryk)
		{
			if (memory[tmp].id == que[i])//There is this page in memory
			{
				if (empty == 1)//Free area
				{
					x = 0;
					//Adjust output order
					for (int k = tmp + 1; k < lruFlag; k++)
					{
						memory2[x].id = memory[k].id;
						x++;
					}
					x = 0;
					for (int k = tmp; k < lruFlag - 1; k++)
					{
						memory[k].id = memory2[x].id;
						x++;
					}
					memory[lruFlag - 1].id = que[i];

					//output
					pageFlag = isPage;
					output(i);
					break;
				}
				else//No free area
				{
					x = 0;
					//Adjust output order
					for (int k = tmp + 1; k < memoryk; k++)
					{
						memory2[x].id = memory[k].id;
						x++;
					}
					x = 0;
					for (int k = tmp; k < memoryk - 1; k++)
					{
						memory[k].id = memory2[x].id;
						x++;
					}
					memory[memoryk - 1].id = que[i];

					//output
					pageFlag = isPage;
					output(i);
					break;
				}
			}
			tmp++;
		}
		//Run here to prove that the page has not been hit after searching the memory 
		//Eliminate the last used page farthest from the current one
		if (tmp == memoryk)
		{
			if (empty == 1)//Free area
			{
				memory[lruFlag].id = que[i];//Direct loading
				pageFlag = noPage;
				total++;
				output(i);
			}
			else//Obsolete page
			{
				//Adjust output order
				int y = 0;
				for (int k = 1; k < memoryk; k++)
				{
					memory2[y].id = memory[k].id;
					y++;
				}
				y = 0;
				for (int k = 0; k < memoryk - 1; k++)
				{
					memory[k].id = memory2[y].id;
					y++;
				}
				memory[memoryk - 1].id = que[i];

				//Missing page processing
				pageFlag = noPage;
				total++;
				output(i);
			}
		}
	}
}

test result

The program adopts black box test and is submitted to OJ system for online evaluation

It can be seen that all OJ test cases passed

experience

Through this experiment, the computer code simulation realizes three page scheduling replacement algorithms, and has a deeper understanding and feeling of the page replacement mode inside the operating system. OPT algorithm is an ideal algorithm. Because we don't know which page will come next in real life, it can only be used as a standard to judge the advantages and disadvantages of page replacement algorithm. The essence of LRU algorithm is a stack implementation.

Topics: LRU