编写C程序,模拟实现首次/最佳/最坏适应算法的内存块分配与回收,要求每次分配与回收后显示出空闲分区和已分配分区的情况。
时间: 2023-11-17 10:26:22 浏览: 92
ncfp.rar_作业分配_内存分配_可变分区_模拟 内存 分配 回收_首次适应算法
好的,这是一个比较复杂的问题,需要比较长的代码。下面是我的实现,其中包含了首次适应算法、最佳适应算法和最坏适应算法的实现,以及对应的内存分配和回收函数:
```c
#include <stdio.h>
#include <stdlib.h>
// 内存块结构体
typedef struct MemoryBlock {
int start; // 开始地址
int size; // 大小
int status; // 状态:0-空闲,1-已分配
} MemoryBlock;
// 内存块链表结构体
typedef struct MemoryBlockList {
MemoryBlock block; // 内存块
struct MemoryBlockList *next; // 下一个内存块
} MemoryBlockList;
// 内存分配算法类型
typedef enum AllocationAlgorithm {
FIRST_FIT, // 首次适应算法
BEST_FIT, // 最佳适应算法
WORST_FIT // 最坏适应算法
} AllocationAlgorithm;
// 内存块链表头节点
MemoryBlockList *head = NULL;
// 打印内存块链表
void printMemoryBlockList() {
if (head == NULL) {
printf("Empty memory block list!\n");
return;
}
MemoryBlockList *p = head;
printf("Memory Block List:\n");
printf("Start\tSize\tStatus\n");
while (p != NULL) {
printf("%d\t%d\t%d\n", p->block.start, p->block.size, p->block.status);
p = p->next;
}
}
// 初始化内存块链表
void initMemoryBlockList(int size) {
head = (MemoryBlockList *)malloc(sizeof(MemoryBlockList));
head->block.start = 0;
head->block.size = size;
head->block.status = 0;
head->next = NULL;
}
// 查找第一个符合要求的内存块
MemoryBlockList *findFirstFitMemoryBlock(int size) {
MemoryBlockList *p = head;
while (p != NULL) {
if (p->block.status == 0 && p->block.size >= size) {
return p;
}
p = p->next;
}
return NULL;
}
// 查找最小的符合要求的内存块
MemoryBlockList *findBestFitMemoryBlock(int size) {
MemoryBlockList *p = head;
MemoryBlockList *bestFitBlock = NULL;
int bestFitSize = -1;
while (p != NULL) {
if (p->block.status == 0 && p->block.size >= size) {
if (bestFitBlock == NULL || p->block.size < bestFitSize) {
bestFitBlock = p;
bestFitSize = p->block.size;
}
}
p = p->next;
}
return bestFitBlock;
}
// 查找最大的符合要求的内存块
MemoryBlockList *findWorstFitMemoryBlock(int size) {
MemoryBlockList *p = head;
MemoryBlockList *worstFitBlock = NULL;
int worstFitSize = -1;
while (p != NULL) {
if (p->block.status == 0 && p->block.size >= size) {
if (worstFitBlock == NULL || p->block.size > worstFitSize) {
worstFitBlock = p;
worstFitSize = p->block.size;
}
}
p = p->next;
}
return worstFitBlock;
}
// 分配内存
void allocateMemory(int size, AllocationAlgorithm algorithm) {
MemoryBlockList *block = NULL;
switch (algorithm) {
case FIRST_FIT:
block = findFirstFitMemoryBlock(size);
break;
case BEST_FIT:
block = findBestFitMemoryBlock(size);
break;
case WORST_FIT:
block = findWorstFitMemoryBlock(size);
break;
default:
printf("Invalid allocation algorithm!\n");
return;
}
if (block == NULL) {
printf("Failed to allocate memory!\n");
return;
}
if (block->block.size > size) {
// 如果内存块大小大于所需大小,则将其分割成两个内存块
MemoryBlockList *newBlock = (MemoryBlockList *)malloc(sizeof(MemoryBlockList));
newBlock->block.start = block->block.start + size;
newBlock->block.size = block->block.size - size;
newBlock->block.status = 0;
newBlock->next = block->next;
block->next = newBlock;
}
block->block.size = size;
block->block.status = 1;
printf("Allocated memory block at start address %d with size %d.\n", block->block.start, block->block.size);
printMemoryBlockList();
}
// 回收内存
void freeMemory(int start) {
MemoryBlockList *p = head;
while (p != NULL) {
if (p->block.start == start && p->block.status == 1) {
p->block.status = 0;
printf("Freed memory block at start address %d with size %d.\n", p->block.start, p->block.size);
// 合并相邻的空闲内存块
MemoryBlockList *q = head;
while (q != NULL && q->next != NULL) {
if (q->block.status == 0 && q->next->block.status == 0) {
q->block.size += q->next->block.size;
MemoryBlockList *tmp = q->next;
q->next = q->next->next;
free(tmp);
} else {
q = q->next;
}
}
printMemoryBlockList();
return;
}
p = p->next;
}
printf("Failed to free memory block at start address %d!\n", start);
}
int main() {
int size = 100;
AllocationAlgorithm algorithm = FIRST_FIT;
initMemoryBlockList(size);
printMemoryBlockList();
allocateMemory(20, algorithm);
allocateMemory(30, algorithm);
allocateMemory(10, algorithm);
freeMemory(0);
freeMemory(50);
allocateMemory(50, algorithm);
return 0;
}
```
在上面的代码中,我们首先定义了一个内存块结构体和一个内存块链表结构体,然后定义了内存分配算法类型,包括首次适应算法、最佳适应算法和最坏适应算法。接着定义了内存块链表头节点和一些基本的操作函数,包括打印内存块链表、初始化内存块链表、查找符合要求的内存块、分配内存和回收内存。
在主函数中,我们首先初始化内存块链表并打印出来,然后使用不同的算法分配和释放内存,最后退出程序。
需要注意的是,由于每次分配内存可能会导致内存块链表的结构发生变化,因此在每次分配和释放内存后都需要重新打印内存块链表,以便查看当前的内存分配情况。
阅读全文