[Operating system] FIFO first-in-first-out page replacement algorithm (C language implementation)

FIFO page replacement algorithm, calculation of page fault rate, code at the end of the article, and example analysis

1. Content

        During the address mapping process, if it is found that the page to be accessed is not in the memory, a page fault interrupt will be generated. When a page fault occurs, if there is no free page in the operating system memory, the operating system must select a page in the memory and move it out of the memory to make room for the page to be transferred in. The rule used to select which page to eliminate is called the page replacement algorithm.

        Simulate the paging management of memory, realize the allocation and call of memory, and complete the correspondence between virtual memory address sequence and physical memory. When there is a page fault in the memory call, the memory page of the program is transferred. When there are no free pages, page replacement is implemented using a first-in-first-out (FIFO) algorithm.

2. The structure of the page

The structure of the page is as follows:

  Page number, page number, timestamp (not used in this algorithm, used in LRU)

name

symbol

Function

page number

Page_num

record page number

page number

Pframe_num

record page number

      The FIFO page replacement algorithm selects the page that stays in the main memory for the longest (that is, the oldest) replacement, that is, the page that enters the memory first and exits the memory first. The reason is: the page that was first transferred into memory is more likely to be no longer used than it was just transferred into memory. Create a FIFO queue to hold all pages in memory. Replaced pages are always performed at the head of the queue. When a page is brought into memory, it is inserted at the end of the queue

       By using the linear table to implement the page table, every time a page needs to be eliminated, the first entered page is eliminated, so that the page that stays in the main memory for the longest time can be eliminated.

The process is as follows:

3. Example Analysis

In the page management system, the order of access (access string) is: 1, 2, 3, 4, 1, 2, 5, 1, 2, 3, 4, 5, when the number of allocated pages is 3, please Calculate the number of page faults using the following (1) replacement algorithm respectively, and draw a page replacement diagram.

(1)  FIFO。

access string 1 2 3 4 1 2 5 1 2 3 4 5
page 1 1 2 3 4 1 2 5 5 5 3 4 4
page 2 1 2 3 4 1 2 2 2 5 3 3
page 3 1 2 3 4 1 1 1 2 5 5
Is there a page fault yes yes yes yes yes yes yes no no yes yes no

Page fault rate: 9/12=0.75

4. The code is as follows:

#define _CRT_SECURE_NO_WARNINGS 1


#include <stdio.h>
#include <stdlib.h>

#define MAX_PAGES 20
#define MAX_PFRAME 20
#define INVALID -1

typedef struct {
	int page_num;
	int pframe_num;
	int count;
	int timestamp;
}page_type;

page_type page[MAX_PAGES];

typedef struct pf_struct {
	int pframe_num;//页面号
	struct pf_struct* next;
}pf_type;

pf_type pframe[MAX_PFRAME];

int diseffct = 0;//缺页记录

void InitPage(const int* page_n, const int* pframe_n)
{
	int total_vp = *page_n, total_pf = *pframe_n;
	int i = 0;
	diseffct = 0;
	for (i = 0; i < total_vp; i++)//虚拟页
	{
		page[i].page_num = i;
		page[i].pframe_num = INVALID;
		page[i].count = 0;
		page[i].timestamp = -1;
	}
	for (i = 0; i < total_pf - 1; i++)
	{
		pframe[i].next = &pframe[i];
		pframe[i].pframe_num = i;
	}
	pframe[total_pf - 1].next = NULL;
	pframe[total_pf - 1].pframe_num = total_pf - 1;
}


double miss_page_rate(int pframe_order[100]) {
	int i = 0;
	double missrate = 0, count = 0;
	while (pframe_order[i] != -1)
	{
		count++;
		i++;
	}
	missrate = diseffct / count;
	return missrate;
}

void menu(int* page_n, int* pframe_n)
{
	int a, b;
	printf("---------------------------------------------\n");
	printf("请输入页面的数量:");
	scanf("%d", page_n);
	printf("请输入页的数量:");
	scanf("%d", pframe_n);
	printf("--------------------------------------------\n");
}

int get_input_order(int fprame_order[100], int pframe_n)
{
	int p = 0;
	int tmp = 0;
	printf("请输入访问串(1到%d中的数字,每输入一个数输入一次回车,输入-1表示结束):\n", pframe_n);
	while (1)
	{
		scanf("%d", &tmp);
		fprame_order[p] = tmp;
		p++;
		if (tmp == -1)
		{
			break;
		}
	}

	return p;
}

int check_all_page(int page_num, int target)
{
	int judge = 0;
	for (int i = 0; i < page_num; i++)
	{
		if (page[i].pframe_num == target)
		{
			judge = 1;
		}
	}
	if (judge == 1)
	{
		return 1;
	}
	else
	{
		diseffct++;//全程序只在本处处理缺页次数
		return 0;
	}
}

void display(int page_num, int judge)//就是打印出所有的页
{
	printf("页面号\t页号\t时间戳\n");
	for (int i = 0; i < page_num; i++)
	{
		printf("%d\t%d\t%d\n", page[i].page_num, page[i].pframe_num, page[i].timestamp);
	}
	if (judge == 1) {
		printf("不缺页\n");
	}
	else
	{
		printf("缺页\n");
	}
}

void FIFO(int page_num, int pframe_id)//page_num为页的数量,pframe_id为页面号
{
	int i, j;
	pf_type* p;
	for (i = page_num - 2; i >= 0; i--)
	{
		//page[i + 1].count = page[i].count;
		page[i + 1].pframe_num = page[i].pframe_num;
	}
	page[0].pframe_num = pframe_id;
}

void execute_pagef(int pframe_order[100], int page_num)//page_num为虚拟页的数量,page_n指针和page_num的值一样
{
	int i = 0, jugde = 0;
	while (pframe_order[i] != -1)
	{
		printf("************************************\n");
		printf("使用页 %d\n", pframe_order[i]);
		jugde = check_all_page(page_num, pframe_order[i]);
		if (jugde == 1) {//在虚拟页内
			i++;
		}
		else//不在页内就调用页面置换算法
		{
			FIFO(page_num, pframe_order[i]);
			i++;
		}
		display(page_num, jugde);
	}
}



int main()
{
	int* page_n = (int*)malloc(sizeof(int));
	int* pframe_n = (int*)malloc(sizeof(int));
	int pframe_order[100];
	int order_num = 0;
	menu(page_n, pframe_n);
	InitPage(page_n, pframe_n);
	order_num = get_input_order(pframe_order, *pframe_n);
	execute_pagef(pframe_order, *page_n);
	//printf("%d %d\n", *page_n, *pframe_n);
	printf("\n缺页率为: %lf", miss_page_rate(pframe_order));
	free(page_n);
	free(pframe_n);
	return 0;
}

5. Run as follows:

The program is divided into 5 pages, there are 3 pages in the memory, and the access sequence is 1, 2, 3, 2, 4, 5, 2

Page fault rate: 0.857143

Guess you like

Origin blog.csdn.net/peng_lv/article/details/128191598