Operating system - memory allocation algorithm

Operating system principle experiment report

Experimental topic Experiment   4 Memory allocation algorithm   

Experiment 4 , memory allocation algorithm

1.1 Purpose of the experiment

A good computer system must not only have a sufficient capacity, high access speed, stable and reliable main memory, but also be able to reasonably allocate and use these storage spaces. When the user applies for the main memory space, the storage management must analyze the usage of the main memory space according to the requirements of the applicant and according to a certain strategy, and find enough free areas to allocate to the applicant. When a job evacuates or actively returns the main memory resources, the storage management needs to reclaim the main memory space occupied by the job or return part of the main memory space. The realization of allocation and recovery of main memory is related to the management method of main memory, and the allocation of memory is simulated by software.

    This experiment helps students understand how to realize the allocation and recovery of main memory space under the dynamic partition management mode.

1.2 Experimental content and requirements

In the dynamic partition management mode, different allocation algorithms are used to realize main memory allocation and main memory recovery.

1.3 Experimental techniques

1) A computer running Windows operating system

2) Visual C Professional Edition or Enterprise Edition needs to be installed on the computer

1.4 Experimental steps

1) The variable partition method is to divide the partition according to the size of the main memory space required by the job. When loading a job, check whether there is enough free space according to the amount of main memory required by the job. If there is, divide a partition according to the requirement and assign it to the job; if not, the job cannot be loaded. With the loading and evacuation of jobs, the main memory space is divided into many partitions, some partitions are occupied by jobs, and some partitions are free. For example:

In order to indicate which areas are free and can be used to load new jobs, there must be an empty area description table, the format is as follows:

  1. Among them, starting address—points out the main memory starting address of a free area.
  2. Length - indicates the length of a continuous free area starting from the start address.
  3. State - There are two states, one is "unallocated" state, indicating that the corresponding area of ​​a certain length pointed out by the starting address is a free area; the other is "empty entry" state, indicating that the corresponding The registration item is blank (invalid), and can be used to register a new free area (for example, after the operation is evacuated, the area it occupies becomes a free area, you should find a "empty entry" column to register the starting address and length of the return area and modify state). Since the number of partitions is uncertain, there should be a proper amount of registration columns whose state is "empty entry" in the free area description table, otherwise the table "overflow" cannot be registered.
  4. The registration status of the above instruction sheet is filled in after the main storage area occupied by the three jobs loaded in the example in the prompt (1).

2) When a new job needs to be loaded into the main memory, it is necessary to check the free area description table to find a large enough free area. Sometimes the free area found may be larger than the job requirement. At this time, the original free area should be divided into two parts: one part is assigned to the job; the other part becomes a smaller free area. In order to minimize the "fragmentation" caused by segmentation, when the job request is loaded, use the free area of ​​the low address part of the main memory as much as possible, and try to save the high address part with a large continuous free area to facilitate large-scale jobs of loading. For this reason, in the free area description table, each free area is registered according to its address order, that is, the starting address of each subsequent free area is always larger than the former. In order to facilitate the search, the table can also be "condensed", and the "empty entry" column is always concentrated at the back of the table.

3) Use the first-fit algorithm, cycle-first algorithm, or best-fit algorithm to allocate main memory space. Since this experiment is simulating the allocation of main memory, it does not actually start the loading program to load the job after the main memory area is allocated to the job, but uses the output "allocation situation" instead. (That is, output the free area description table and its memory allocation table at that time)

4) When a job finishes evacuating, the area occupied by the job should be returned. If the returned area is adjacent to other free areas, it should be combined into a larger free area and registered in the free area description table. For example, in the case listed in the prompt (1), if job 2 withdraws and returns the occupied main storage area, it should be combined with the upper and lower adjacent free areas to form a large free area and registered in the free area description table .

5) Please design the program of main memory allocation and recycling according to the first-fit algorithm, cycle-first algorithm and best-fit algorithm respectively. Then according to (1) assume that three jobs have been loaded into the main memory, and two free areas are formed, and determine the initial value of the free description table. There is an application for job 4, which requires a main memory of 6K, to be loaded into the main memory; then job 3 is evacuated; and then job 2 is evacuated. Please perform main memory allocation and recovery for them, and display or print out the initial value of the free area description table and the changes after each allocation or recovery.

6) The requirements are as follows:

To print the initial value and running result of the program when running, the requirements are as follows:

  1. Print the initial state of the free area description table, the application amount of job 4, and the state of the free area description table after assignment for job 4;
  2. Then print the return amount of job 3 and job 2 and the description table of the free area after recycling job 3 and job 2 occupying the main memory.

The program is designed as follows:

Define a class freespace to build the structure of the memory block

typedef struct freespace
{
	int num;         //分区号
	int size;        //分区大小
	int address;     //分区首地址
	status state;    //分区状态,FREE和BUSY
};

Freespace contains the partition job number, partition size, partition initial address and partition status.

The node definition of the partition mainly uses the double-linked list structure, and the data includes the head pointer, tail pointer, data

The domain freespace variable also defines the global variables first pointer and last pointer and an auxiliary help pointer, and initializes them.

typedef struct node
{
	freespace data;
	node *head;
	node *next;
}*Linklist;

If the two free areas are merged, because last->next=null, the last two p->next->next will be empty, and the pointer help is used to prevent the program from stalling.

The menu design in the program is as follows:

void menu()
{
cout << "********************内存分配系统********************" << endl;
cout << "*              1.首次适应算法分配内存              *" << endl;
cout << "*              2.最佳适应算法分配内存              *" << endl;
cout << "*              6.下次适应算法分配内存              *" << endl;
cout << "*              3.查看主存分配情况                  *" << endl;
cout << "*              4.回收主存                          *" << endl;
cout << "*              5.退出                              *" << endl;
cout << "****************************************************" << endl;
cout << "请选择:" << endl;
}

First fit algorithm to find the first free block that can be placed:

node *p = first;
	
while (p)
	{
		if (p->data.state == FREE&&p->data.size == size)//有大小刚好合适的空闲块
		{
			p->data.state = BUSY;
			p->data.num = num;
			pre=p->next;
			display();
			return 1;
		}
		if (p->data.state == FREE&&p->data.size > size)//有大小比他大的空闲块
		{
			list->head = p->head;
			list->next = p;
			list->data.address = p->data.address;
			p->head->next = list;
			p->head = list;
			p->data.address = list->data.address + list->data.size;
			p->data.size -= size;
			pre=p;
			display();
			return 1;
		}
		p = p->next;
	}

Best Adaptive Algorithm (Find the smallest free block with memory size >= job application memory):

	while (p)//找到最佳位置
	{
		if ((p->data.size > size || p->data.size == size)&&p->data.state==FREE)
		{
			if (p->data.size - size < min_space)
			{
				q = p;
				min_space = p->data.size - size;
			}
		}
		p = p->next;
	}

Next time adaptation algorithm (find the first free area that can be placed from the last inserted job position):

node *p = pre;

	while (p)
	{
		if (p->data.state == FREE&&p->data.size == size)//有大小刚好合适的空闲块
		{
			p->data.state = BUSY;
			p->data.num = num;
			pre=p->next;
			display();
			return 1;
		}
		if (p->data.state == FREE&&p->data.size > size)//有大小比他大的空闲块
		{
			list->head = p->head;
			list->next = p;
			list->data.address = p->data.address;
			p->head->next = list;
			p->head = list;
			p->data.address = list->data.address + list->data.size;
			p->data.size -= size;
			pre=p;
			display();
			return 1;
		}
		p = p->next;
	}

Memory recovery (three situations need to be considered: there are free areas before and after, only free areas in the front, and free areas only in the back):

if(p->head->data.state == FREE&&p->next->data.state == FREE)
//判断前后是不是都有空闲区
if (p->head->data.state == FREE)//与前一块空闲区相邻,则合并
if (p->next->data.state == FREE)//与后一块空闲区相邻,则合并

1.5 Experimental results and analysis instructions

1) Allocate memory according to the requirements of the topic as follows:

             

 First use the first adaptation method to assign assignments as follows:

0-5k

homework 4

5k-10k

homework 1

10k-14k

homework 3

14k-26k

homework 5

26k-32k

homework 2

32k-128k

idle

The allocation of memory after allocation is as follows:

Recycle homework 4 and homework 5, so that the initial state allocation table is as follows, which meets the prerequisite requirements of the experiment:

2) Adapt the algorithm for the first time.

① Allocate job 4 with a memory size of 6k, and the result is shown in the figure below:

 There are three free areas in the original memory, the sizes are 5k, 12k, and 96k, and the memory occupied by job 4 is 6k. Therefore, according to the first adaptation method, job 4 should be allocated to the memory with a free area of ​​12k. After the free area is allocated The remaining size is 6. The result is consistent with the above figure.

② Recycling job 3, input 4 3, the result is shown in the figure below:

 After reclaiming job 3, the location of 10k-14k in the memory becomes a free area.

③ Recycling job 2, input 4 2, the result is shown in the figure below:

Because there are free areas before and after job 2, after recycling job 2, the free areas are merged. The starting address is 20K, the ending address is 128K, and the size of the free area is 108K.

3) The next adaptation algorithm

In order to see the initial memory allocation of the next adaptation algorithm is as follows:

 After using the first adaptation method to allocate 8k memory space for job 8, 50k-58k is occupied, and job 8 is the location of the last allocation:

 Use the next adaptation method to allocate 2k memory space for job 9, and input 6 9 2;

Because job 8 is the location of the last allocation: so start from here to find space for allocation, and job 9 is allocated after job 8, the starting position is 58k, and the ending position is 60k.

Recycling is no longer demonstrated.

4) Best Adaptive Algorithm

To see the difference between best-fit and first-fit algorithms, the initial memory allocation is as follows:

 The form is as follows:

0-10k

idle

10k

10k-14k

homework 3

4k

14k-22k

idle

8k

22k-28k

homework 2

6k

28k-128k

idle

100k

① Allocate job 4 with a memory size of 6k, and the result is shown in the figure below:

 There are three free areas in the original memory, the sizes are 10k, 8k, and 100k, and the memory occupied by job 4 is 6k. Therefore, according to the best fit method, job 4 should be allocated to the memory with a free area of ​​8k. The remaining size of the free area after allocation is 2k. The result is consistent with the above figure.

② Recycling job 3, input 4 3, the result is shown in the figure below: 

After reclaiming job 3, the free area vacated by job 3 is merged with the previous free area, and the position of 0k-14k in the memory becomes the free area.

③ Recycling job 2, input 4 2, the result is shown in the figure below:

Because there are free areas before and after job 2, after recycling job 2, the free areas are merged. The starting address is 20K, the ending address is 128K, and the size of the free area is 108K.

1.6 Experiment experience ( problems and solutions encountered in the experiment )

Experimental Reflection and Idea Gathering

  1. This program uses the linked list structure to simulate the memory block. There will be some problems involving null pointers in the linked list (the node after the last node). It is better to use the circular linked list structure to simulate the memory distribution, so that the allocation can be simpler. , but the loop condition needs to be modified. Here is a node added to avoid the problem of null pointers.
  2. The ideas of the first adaptation and the next adaptation are basically the same, mainly because the initial search nodes are different. The initial node for the first adaptation is first, and the initial node for the next adaptation is pre. After each job application, the location needs to be recorded for the next assignment of the next adaptive algorithm.
  3. When reclaiming memory, you need to pay attention to the problem of the tail node of the free area linked list.

Guess you like

Origin blog.csdn.net/cangzhexingxing/article/details/124905484