Experiment 4 of operating system requests paging storage management

Posted by Banacek on Wed, 09 Feb 2022 22:43:42 +0100

Experiment 4 of operating system requests paging storage management

Title Description:
1, Experimental purpose
By writing the simulation program of paging storage management, deepen the understanding of paging storage management mode, be familiar with the conversion process from logical address to physical address, master the page scheduling algorithm in virtual storage management, and understand the processing process of page missing interrupt in paging virtual storage system.
2, Experimental content
1. A paging virtual storage system is designed and implemented by program simulation. Assuming that the page size is 1K, each process is allocated three blocks.
2. Trestle scheduling algorithm (LRU) and page stacking scheduling algorithm (FIFO) are adopted respectively. (select the page to exchange directly.)
3. Assume that the page table of the current job is as follows:

4. Assume that the logical instruction format is:
Operator page number 1 page address 1 page number 2 page address 2
For example, it means adding the content of the unit with address 011 in page 1 and the content of the unit with address 051 in page 2,
For example: (1011H) + (1200H).
The existing instruction sequence is as follows:

5. Write a simulation program and execute the above instructions. If the current page is not in memory, use the page scheduling algorithm to exchange the corresponding pages.
6. It is required to output the corresponding physical instruction every time a logical instruction in the above instruction flow is executed. If the page table is modified, the modified page table will be displayed.
FIFO result
Queue content

LRU results
Internal contents of the stack: the third is the top of the stack, and the first is the bottom of the stack

3, Experimental ideas
(1) Initialization:
① Initialize page table: customize the data structure of page table items, and all page table items form an array. The page table item data structure contains the provided page table information. Read the page table information from the file and write it to the array.
② Initialization instruction table: customize the instruction data structure. All instructions form an array, which represents the instruction table.
The instruction data structure contains the information of a single instruction provided. Read the instruction table information from the file and write it to the array.
(2) FIFO algorithm:
The algorithm always switches out the page that enters the memory first, that is, to choose the page that stays in the memory for the longest time. Therefore, a queue can be set to save the page number of the page currently entering the memory, indicating the current memory allocation. And set their dwell time.
According to the page table information given by the title, it can be seen that the memory block allocated by the process is full, and pages 0, 1 and 2 are stored. You can initialize the current memory allocation queue. Here, I save these pages to the queue according to their logical addresses in the page table, that is, the initialization queue is: 0, 1, 2. And set their dwell time to 0. Because the memory block allocated to each process is 3, a two-dimensional array of three rows and two columns is used to represent the queue. The first element of each row saves the page number and the second element saves the corresponding dwell time.
The FIFO function I set here has only one parameter, and the page number to be found is passed in. Therefore, the algorithm should complete the following functions:
① Whether the page is in memory is determined by serach_page() implementation
② Find the page with the longest residence time in memory and search_max() implementation
③ If the page is not in memory, find the logical address of the page in the page table, find the logical address of the page with the longest residence time in the page table, and exchange the flags of the two pages, disk address and memory block number. And write the information of the paged in at the paged out position in the queue. The dwell time is set to 0, and the rest dwell time is + 1. Output the updated page table information.
④ Write the queue information at this time into the queue information summary table for viewing after the program runs.
(this total table queue corresponds to the FIFO queue of the above example image. The table has a total of 8 rows and corresponds to 8 operation pages of 4 instructions. Therefore, each page is written with a queue information.)
code:

void FIFO(int pnum)
{
	if (!serach_page(pnum))
	{
		int a, b, c;
		int max = serach_max();
		for (int i = 0;i < 3;i++)
		{
			if (in_s[i][1] == max)
			{
				c = in_s[i][0];
				break;
			}
		}
		for (int i = 0;i < 6;i++)
		{
			if (p[i].pnum == c)
			{
				a = i;break;
			}
		}
		for (int i = 0;i < 6;i++)
		{
			if (p[i].pnum == pnum)
			{
				b = i;break;
			}
		}
		p[a].flag = 0;
		p[b].flag = 1;
		p[a].add = p[b].add;
		p[b].add = -1;
		p[b].cnum = p[a].cnum;
		p[a].cnum = -1;
		int e;
		for (int i = 0;i < 3;i++)
		{
			if (in_s[i][0] == c)
			{
				e = i;
				break;
			}
		}
		in_s[e][0] = pnum;
		in_s[e][1] = 0;
		cout << "Occurrence section" << ++cnumber << "The updated page table is:" << endl;
		display_p();
	}
	int p = 0;
	for (int i = 0;i < 3;i++)
		in_FIFO[f][p++] = in_s[i][0];
	f++;
}

(3) LRU algorithm:
Use a special stack to save the current memory allocation. When a page is needed and the page is in memory, the page is taken out of the stack and pushed into the top of the stack. If the stack is full and the required page is not in memory, you should move out of the bottom page of the stack and press the page to be replaced into the top of the stack. Because operations different from the stack are involved here, and the called page should become the top of the stack, the stack can be realized with a two-way linked list for frequent row access.
Initialize the stack. According to the meaning of the question, it should be 0, 1 and 2, where 0 is the bottom of the stack and 2 is the top of the stack.
Similarly, the LRU function I set here has only one parameter, which is used to pass in the page number to be found. Therefore, the algorithm should complete the following functions:
① Whether the page is in memory is determined by serach_page2 implementation. (the difference from FIFO is that it is searched from the stack here)
② If the current stack is full and page missing occurs, move the element at the bottom of the stack out of the stack, and this element is the page number of the page to be swapped out. Find the logical position of the page to be swapped out and in in in the page table, and exchange some information of the two pages. Output the updated page table information.
③ If there is no missing page, move the page out of the stack and press it into the top of the stack.
④ Write the stack information at this time into the total stack content table.
code:

void LRU(int pnum)
{
	if (search_page2(pnum))//Page in main memory
	{
		LRU_stack* q = p_stack->next;
		while (q)//Find the position of the chain stack corresponding to the page
		{
			if (q->pnum == pnum)
				break;
			q = q->next;
		}
		if (q->next == NULL)
		{
			q = p_stack->next;
			int k = 0;
			while (q)
			{
				in_LRU[f][k++] = q->pnum;
				q = q->next;
			}
			f++;
		}
		else
		{
			LRU_stack* d = p_stack->next;
			while (d->next)//Find stack top pointer
				d = d->next;
			//Exchanging the position of two pages in the chain stack does not change the page table.
			q->prew->next = q->next;
			q->next->prew = q->prew;
			q->prew = d;
			d->next = q;
			q->next = NULL;
			//Write the current chain stack information to LRU
			q = p_stack->next;
			int k = 0;
			while (q)
			{
				in_LRU[f][k++] = q->pnum;
				q = q->next;
			}
			f++;
		}
	}
	else//Page not in main memory
	{
		int onum;
		int a, b;//Save the relative positions of the swapped out page and the swapped in page in the page table respectively
		for (int i = 0;i < 6;i++)//Find out the relative position of the change in page
		{
			if (p[i].pnum == pnum)
			{
				b = i;
				break;
			}
		}
		onum = p_stack->next->pnum;
		for (int i = 0;i < 6;i++)//Find out the relative position of the swapped out page
		{
			if (p[i].pnum == onum)
			{
				a = i;
				break;
			}
		}
		//Modify page table
		p[a].flag = 0;
		p[b].flag = 1;
		p[a].add = p[b].add;
		p[b].add = -1;
		p[b].cnum = p[a].cnum;
		p[a].cnum = -1;
		cout << "Occurrence section" << ++cnumber << "The updated page table is:" << endl;
		display_p();
		LRU_stack* q = p_stack->next;
		LRU_stack* d = p_stack->next;
		while (d->next)//Find stack top pointer
			d = d->next;
		q->prew->next = q->next;
		q->next->prew = q->prew;
		q->prew = d;
		d->next = q;
		q->next = NULL;
		q->pnum = pnum;
		q = p_stack->next;
		int k = 0;
		while (q)
		{
			in_LRU[f][k++] = q->pnum;
			q = q->next;
		}
		f++;
	}
}

Experimental results:

Topics: Algorithm linked list Operating System