Simulation program design of dynamic partition allocation storage management

Dynamic partition allocation
1, Experimental objectives
Develop a C language program to realize the dynamic partition allocation scheme of memory space management.
2, Experimental principle
Dynamic partition allocation: according to the actual needs of the process, dynamically create partitions to allocate memory space. When realizing dynamic partition allocation, it will involve the data structure used in partition allocation, partition allocation algorithm, partition allocation and recycling operations.
1) Data structure in partition allocation
 free partition table: a data table used to record the status of each free block, such as starting address, size, usage, etc;
 free partition linked list: link all free partitions into a linked list to facilitate memory space viewing, allocation and recycling.
2) Memory allocation process
Use the allocation algorithm to find the memory block that meets the requirements, and set the requested memory size as size:
 if the size of the found free partition is equal to size, it is fully allocated;
 if the size of the found free partition is greater than size, and the remaining size is less than 1K after being divided into two, it will not be divided and allocated as a whole; Otherwise, it is divided into two parts, and the remaining part still exists as a free partition;
 if there is no free partition meeting the requirements, the allocation fails
3) Allocation algorithm
 first adaptation method: the free partition is organized according to the increasing order of the first address. Each time, start from the head of the chain to find the memory block that meets the requirements.
 best fit method: free partitions are organized according to the increasing order of free partition size and address. Each time, starting from the head of the chain, find the smallest memory block that meets the requirements for allocation.
 worst case adaptation method: free partitions are organized in descending order of free partition size, and whether the maximum free partition meets the requirements is directly judged during each search.
4) Memory recycling
According to the first address and size of the free zone, look up the free partition list / linked list to judge whether there are adjacent free partitions:
 the release area is adjacent to the front free area: merge the release area and the front free area into one free area. The first address is still the first address of the previous free area, and the size is the sum of the size of the free area and the size of the free area.
 the release area is adjacent to the front and rear free areas: combine the three areas into one free area, the first address is the first address of the front free area, and the size is the sum of the sizes of the three areas, and cancel the list of the original and rear free areas.
 the release area is adjacent to the post free area: the release area is merged into the post free area. The first address is the first address of the release area, and the size is the sum of the two sizes.
 the free area is not adjacent to any free area: take the free area as a free area, and insert its size and first address into the appropriate position of the free area table.

Experiment purpose or requirements: through this experiment, we can deeply understand the idea and implementation method of paging technology of continuous allocation mode or discrete allocation mode of computer memory management, so that we can better analyze and master the characteristics of continuous or paging memory management, and achieve the basic practical ability, problem analysis, design The training of professional core competence realized by algorithm.
Develop a C language program to realize the dynamic partition allocation scheme of memory space management. According to the actual needs of the process, dynamically create partitions to allocate memory space. When realizing dynamic partition allocation, it will involve the data structure used in partition allocation, partition allocation algorithm, partition allocation and recycling operation and so on. Design the data structure in partition allocation, realize the memory allocation process, use the first adaptation method to allocate memory space, and deal with various situations in various memory recycling, such as the release area is adjacent to the front free area, the release area is adjacent to the front and rear free areas, and so on.

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

int memory = 0; //Size of storage space
int count = 0;	//Record the total number of jobs
typedef struct Storage{
	int number;		//Job number
	int start;
	int last;
	int size;
	int state;	//Distinguish whether memory space is allocated or unallocated. Idle is 0 
	struct  Storage *next;
}Storage;
int add(Storage *head,int number,int size){
	Storage *add1 = head->next;
	//ergodic
	while(add1 != NULL){
		//A free area greater than or equal to the insertion job was found 
		if(add1->number==-1 && add1->size>=size){
			break; 
		}
		add1 = add1->next;
	}
	if(add1 == NULL){
		//No suitable free area was found
		return 0;	
	}
	//Divide the free area into two parts
	int sizeOld = add1->size;
	
	//Storage job 
	add1->number=number;
	add1->size = size;
	add1->state = 1;
	add1->last = add1->start + size - 1;

	//New free area section
	Storage *add2 = (Storage *)malloc(sizeof(Storage));
	add2->number = -1;
	add2->size = sizeOld - size;
	add2->start = add1->last + 1;
	if(add2->start >= memory - 1){
		add1->next = NULL;
		return 1; 
	} 
	add2->state = 0;
	add2->last = add2->start + add2->size - 1;
	add2->next = add1->next;
	add1->next = add2;	
	return 1; 
}

int deleted(Storage *head,int id){
	Storage *del1 = head->next;
	Storage *del2 = del1->next;
	int temp1 = -1,temp2 = -1;
	
	while(del1 != NULL){
		if(id == del2->number){
			temp1 = 0;
			temp2 = 0; 
			//Adjacent to the previous free area 
			if(del1->state == 0){
				temp1 = 1;
			}
			//Adjacent to the next free area 
			if(del2->next->state == 0){
				temp2 = 1;
			}
			break;
		}
		del1 = del1->next;
		del2 = del2->next;
	}
	//Discuss the free area before and after the free area 
	//There are free areas in front of and behind
	if(temp1 == 1 && temp2 == 1){ 
		del1->last = del2->next->last;
		del1->size = del1->size + del2->size + del2->next->size;
		del1->state = 0;	
		del1->number = -1; 
		del1->next = del2->next->next;
	}
	//Ahead is the free area 
	else if(temp1 == 1){
		del1->number = -1;
		del1->size = del1->size + del2->size;
		del1->state = 0;
		del1->last = del2->last;
		del1->next = del2->next;
	}
	//Then there is the free area 
	else if(temp2 == 1){ 
		del2->number = -1;
		del2->size = del2->size + del2->next->size;
		del2->state = 0;
		del2->last = del2->next->last;
		del2->next = del2->next->next; 
	}
	//Neither front nor back is a free area
	else{
		del2->number = -1;
		del2->state = 0;
	} 
	return 1;
}
//View memory allocation 
void viewinformation(Storage *head){
	Storage *p = head->next;
	while(p != NULL){
		if(0 == p->state){
			printf("Free area  \tstart = %d\tlast = %d\tsize = %d\n",p->start,p->last,p->size);
		} 
		else{
			printf("task id = %d\tstart = %d\tlast = %d\tsize = %d\n",p->number,p->start,p->last,p->size);
		}
		p = p->next;
	}
}

void firstview(){
	    printf("1: Allocate memory for new jobs\n");
        printf("2: Reclaim jobs to free memory\n");
 	    printf("3: View memory allocation\n");        
		printf("4: sign out\n");
		printf( "Please enter an action: " );
} 
int main(){
	printf("Please enter the storage space size (unit) K): ");
	scanf("%d",&memory);
	//initialization 
	Storage *head = (Storage *)malloc(sizeof(Storage));
	Storage *storage = (Storage *)malloc(sizeof(Storage));
	head->next = NULL;
	storage->number = -1;
	storage->size = memory;
	storage->start = 0;
	storage->last = memory-1;
	storage->state = 0;
	storage->next = NULL;
	head->next = storage;
	while(1){
		int chioce, id, size;
		firstview();
        scanf("%d", &chioce);
        switch(chioce){
        	//Four options to do different things 
			case 1 : {
				//Assign a new job 
        		printf("Please enter a job id And job size size:\n");
        		printf("id = ");
        		scanf("%d",&id);
        		printf("size = ");
        		scanf("%d",&size);
        		if(add(head,id,size)){ 
					printf("Space allocation succeeded!\n");
					//Each time space is allocated, the number of jobs is increased by one 
					count++; 
				} 
				else{
					printf("Space allocation failed!\n");
				}
				break;
			} 
			case 2 : {
				//Recycling operation 
				printf("Please enter the name of the recycle job id: ");
				scanf("%d",&id);
				if(deleted(head,id)){
					//Recycling succeeded 
					count--;
					printf("Job recycling succeeded!\n");	
				}
				else{
					//Recycling failed 
					printf("Job recycle failed!\n");
				}
				break;
			}
			case 3 : { 
				//View allocation 
				viewinformation(head);
				break;
			} 
			case 4 : {
				//Exit program 
				return 0;
			} 
		}
	}
	return 0;
}

Tags: C data structure linked list

Posted on Sat, 06 Nov 2021 02:25:02 -0400 by fareasd