Computer Operating System Experiment 3: Simulation of Dynamic Distribution

Posted by sdyates2001 on Mon, 06 May 2019 02:24:04 +0200

1. Experimental Purpose

Understanding data structures and allocation algorithms in dynamic partitioning allocation, and further understanding of dynamic partitioning storage management and its implementation

2. Experimental Contents

  1. C language is used to dynamically distinguish the allocation and recycling processes using the first-time and best-fit algorithms, respectively.Among them, the idle partitions are managed by the idle partition chain (table); when allocating memory, the system takes precedence over the space at the lower end of the idle partition.
  2. Assuming the initial state, the available memory space is 640KB with the following request sequence:
    * Job 1 Request 130KB
    * Job 2 Request 60KB
    * Job 3 requests 100KB
    * Job 2 releases 60KB
    * Job 4 Request 200KB
    * Job 3 releases 100KB
    * Job 1 releases 130KB
    * Job 5 Request 140 KB
    * Job 6 Request 60KB
    * Job 7 Request 50KB
    * Job 8 Request 60KB
    First-time and best-fit algorithms are used to allocate and reclaim memory, requiring the free memory partition chain to be displayed after each allocation and reclaim.

3. Consideration: Discuss the characteristics of various allocation algorithms.

  1. First-time adaptive algorithm:
    Algorithmic idea: Chains of free partitions are connected in increasing order of addresses; during memory allocation, they are searched from the beginning of the chain until a partition of sufficient size is found, memory is allocated from the partition according to the size of the job, and the remaining free partitions are still chained in the chain of free partitions.
    Advantages: Large idle partitions of the high address portion are retained, creating conditions for memory allocation of large jobs;
    Disadvantages: (1) Each time the idle partition of the low address part is preferred, resulting in a large amount of external fragmentation in the low address part.(2) Every time it searches from the low address part, which increases the cost of finding free partitions;

  2. Best-fit algorithm:
    Algorithmic idea: The idle partitions in the idle partition chain are sorted from small to large in order to form the idle partition chain.Find the appropriate free partition from the top of the chain each time to allocate memory for the job, so that the free partition found each time is closest to the size of the job, so-called "best".
    Advantages: The first free partition found is the size closest to the size of the memory job to be allocated;
    Disadvantages: Large amounts of hard-to-use external fragmentation are generated.

4. Ideas for implementation

  1. First fit algorithm
    An example of an idle partition chain is given to illustrate the allocation with FF algorithm.The FF algorithm requires the free partition chains to be linked in an addressing order.
    1. When allocating memory, search sequentially from the beginning of the chain until an idle partition of the required size is found;
    2. Then, according to the size of the job, a block of memory space is allocated from the partition to the requester, and the remaining free partitions remain in the free chain.
    3. If you cannot find a partition from the beginning to the end of the chain that meets the requirements, the memory allocation fails and returns.
  2. Best-fit algorithm:
    1. Sort the idle partitions in the idle partition chain from small to large in order to form an idle partition chain.
    2. Each time a suitable free partition is found from the top of the chain to allocate memory for the job, the remaining free partitions remain in the free chain.
    3. If you cannot find a partition from the beginning to the end of the chain that meets the requirements, the memory allocation fails and returns.

5. Major data structures

Partition structure containing partition number, partition start address, free address, remaining space, status information

6. Algorithmic flowchart

  1. First fit algorithm

  2. Best-fit algorithm

7. Operation and Testing

  1. First fit algorithm
  2. Best-fit algorithm

8. Summary

Through this experiment, we have written and debugged a simulation program of dynamic partitioning allocation, which is more familiar with the process of dynamic partitioning allocation. I have used dynamic partitioning allocation. Dynamic partitioning allocation also contains several algorithms, which are the first adaptive algorithm and the best adaptive algorithm, to improve the allocation efficiency of the computer.

9. Code Appendix

#include<stdio.h>
#include<malloc.h>
#include<string.h>

#define TOTAL_MEMORY 200


typedef struct partition {
	int num;			//Partition Number 
	int startAdress;    //Partition Start Address 
	int leisureAdress;  //Free Address 
	int size;			//Remaining space 
	int state;         //Status, 0 unallocated, 1 assigned 
	struct partition *next;
}partition, *PART;

void printPartitionQue(PART partHead) {
	PART mPart = partHead;
	printf("Partition Number      Address       Free Address       Free space       state\n");
	while (mPart != NULL) {
		printf("%3d          ", mPart->num);
		printf("%3d          ", mPart->startAdress);
		printf("%3d          ", mPart->leisureAdress);
		printf("%3d           ", mPart->size);
		printf("%3d           ", mPart->state);
		printf("\n");
		mPart = mPart->next;
	}
	printf("\n");
}

/**
*	Create partitions
*/
PART createPartition() {
	PART partHead = NULL, mPart = NULL;
	int sizes[4] = { 64, 16, 128, 32 };
	//	int sizes[4] = {128, 64, 32, 12};
	int startAdress = 20;
	for (int i = 1; i <= 4; i++) {
		if (i == 1) {
			mPart = partHead = (PART)malloc(sizeof(partition));
			mPart->num = i;
			mPart->startAdress = startAdress;
			mPart->leisureAdress = startAdress;
			mPart->size = sizes[i - 1];
			mPart->state = 0;
			startAdress += mPart->size;
			mPart->next = NULL;
		}
		else {
			mPart->next = (PART)malloc(sizeof(partition));
			mPart = mPart->next;
			mPart->num = i;
			mPart->startAdress = startAdress;
			mPart->leisureAdress = startAdress;
			mPart->size = sizes[i - 1];
			mPart->state = 0;
			startAdress += mPart->size;
			mPart->next = NULL;
		}
	}
	return partHead;
}

/**
*	Sort by available space size in ascending order
*/
PART  sort(PART partHead, int totalPart) {
	PART sortHead = NULL;
	PART nPart = NULL, tPart = NULL;;
	PART temp;
	int min = 0;
	while (totalPart != 0) {
		min = partHead->size;
		nPart = partHead->next;
		//Get the smallest size assignment to min 
		while (nPart != NULL) {
			if (min > nPart->size) {
				min = nPart->size;
			}
			nPart = nPart->next;
		}
		nPart = partHead;
		while (nPart != NULL) {
			if (nPart->size == min) {
				temp = nPart;
				if (sortHead == NULL) {
					tPart = sortHead = (PART)malloc(sizeof(partition));
					tPart->num = nPart->num;
					tPart->startAdress = nPart->startAdress;
					tPart->leisureAdress = nPart->leisureAdress;
					tPart->size = nPart->size;
					tPart->state = nPart->state;
					tPart->next = NULL;
				}
				else {
					tPart->next = (PART)malloc(sizeof(partition));
					tPart = tPart->next;
					tPart->num = nPart->num;
					tPart->startAdress = nPart->startAdress;
					tPart->leisureAdress = nPart->leisureAdress;
					tPart->size = nPart->size;
					tPart->state = nPart->state;
					tPart->next = NULL;
				}
				PART mPart = partHead;
				while (mPart != NULL) {
					if (mPart == temp) {
						partHead = partHead->next;
						totalPart--;
						break;
					}
					else if (mPart->next == temp) {
						mPart->next = temp->next;
						totalPart--;
						break;
					}
					mPart = mPart->next;
				}
			}

			nPart = nPart->next;
		}
	}
	return sortHead;
}

/**
*	First fit algorithm allocates memory core code
*/
void assignOfFirstFit(PART partHead, PART mPart, int size) {
	mPart = partHead;
	while (mPart != NULL) {
		if (mPart->size >= size) {
			mPart->size -= size;
			mPart->leisureAdress += size;
			mPart->state = 1;
			printf("***********************List of partitions successfully allocated***********************\n");
			printPartitionQue(partHead);
			break;
		}
		mPart = mPart->next;
	}
	if (mPart == NULL) {
		printf("insufficient memory!\n");
	}
}

/**
*	Loop First Adaptation algorithm allocates memory core code
*/
PART assignOfNextFit(PART partHead, PART mPart, int size, int totalPart) {
	while (totalPart != 0) {
		if (mPart != NULL) {
			if (mPart->size >= size) {
				mPart->size -= size;
				mPart->leisureAdress += size;
				mPart->state = 1;
				printf("***********************List of partitions successfully allocated***********************\n");
				printPartitionQue(partHead);
				return mPart->next;
			}
			mPart = mPart->next;
			totalPart--;
		}
		else {
			mPart = partHead;
		}
	}
	if (totalPart == 0) {
		printf("insufficient memory!\n");
		return mPart;
	}
}


//	Best-fit algorithm

PART assignOfBestFit(PART partHead, PART mPart, int size) {
	mPart = sort(partHead, 4);
	partHead = mPart;
	while (mPart != NULL) {
		if (mPart->size >= size) {
			mPart->size -= size;
			mPart->leisureAdress += size;
			mPart->state = 1;
			printf("***********************List of partitions successfully allocated***********************\n");
			partHead = sort(partHead, 4);
			printPartitionQue(partHead);
			return partHead;
		}
		mPart = mPart->next;
	}
	if (mPart == NULL) {
		printf("insufficient memory!\n");
		return partHead;
	}
}

/**
*  Allocate memory
*/
void assignMemory(PART partHead, int assignType) {
	int totalPart = 4;
	PART mPart = partHead;
	char c;
	char name[10] = "";
	int size = 0;
	printf("Whether to enter a job(Y/N): ");
	scanf("%c", &c);
	while (c == 'Y' || c == 'y') {
		printf("Please enter a job name:");
		scanf("%s", &name);
		printf("Please enter job size:");
		scanf("%d", &size);

		switch (assignType) {
		case 1:
			assignOfFirstFit(partHead, mPart, size);
			break;
		case 2:
			partHead = assignOfBestFit(partHead, mPart, size);
			break;
		}

		printf("Whether to enter a job(Y/N): ");
		getchar();
		scanf("%c", &c);
	}
}

/**
*	Best-fit algorithm
*/
void BestFit(PART partHead) {
	printf("***********************Pre-Sort Partition List***********************\n");
	printPartitionQue(partHead);
	//Allocate memory
	assignMemory(partHead, 2);
}


/**
*	First fit algorithm
*/
void FirstFit(PART partHead) {
	printf("***********************Partition list before allocation***********************\n");
	printPartitionQue(partHead);
	//Allocate memory 
	assignMemory(partHead, 1);
}

int main() {
	int k;
	PART partHead = createPartition();
	printf("*****************************Simulation of dynamic partitioning assignment*************************");
	printf("\n                          1,First fit algorithm                                 ");
	printf("\n                          2,Best-fit algorithm                                 ");
	printf("\n                          3,Sign out                                         ");
	printf("\n Enter the corresponding sequence number to select the corresponding algorithm:");
	scanf("%d", &k);
	getchar();
	switch (k) {
	case 1:
		FirstFit(partHead);
		break;
	case 2:
		BestFit(partHead);
		break;
	case 3:
		break;
	default:
		printf("Selection error, please re-select.");
	}
	return 0;
}


Topics: C