C语言分别实现采用首次适应算法和最佳适应算法的动态分区分配过程alloca()和回收过程free()。其中空闲分区通过空闲分区链来管理;在进行内存分配时,系统优先是有空闲分区低端的空间,假设初始状态下的可用的内存空间为640KB,自行设计作业的请求序列,分别采用首次适应算法和最佳适应算法进行内存块的分配和回收,要求每次分配和回收后显示出空闲内存分区链的情况的代码
时间: 2024-02-01 10:15:50 浏览: 63
基于C语言实现采用循环首次适应算法的动态分区分配、回收过程【100012804】
以下是C语言实现采用首次适应算法和最佳适应算法的动态分区分配过程alloca()和回收过程free()的代码,同时也包含了空闲分区链的管理:
```c
#include <stdio.h>
#include <stdlib.h>
// 定义内存块结构体
typedef struct memory_block {
int size; // 内存块大小
int start_addr; // 内存块起始地址
struct memory_block *next_block; // 指向下一个内存块
} memory_block_t;
// 定义空闲分区链表头指针
memory_block_t *free_block_list_head = NULL;
// 初始化空闲分区链表
void init_free_block_list(int size) {
free_block_list_head = (memory_block_t *)malloc(sizeof(memory_block_t));
free_block_list_head->size = size;
free_block_list_head->start_addr = 0;
free_block_list_head->next_block = NULL;
}
// 首次适应算法分配内存块
void *allocate_first_fit(int size) {
void *addr = NULL;
memory_block_t *p = free_block_list_head;
// 搜索空闲分区链表,找到第一个能够容纳请求的空闲分区
while (p != NULL) {
if (p->size >= size) {
addr = (void *)(p->start_addr);
// 如果空闲分区恰好等于请求大小,直接分配
if (p->size == size) {
free_block_list_head = p->next_block;
free(p);
}
// 如果空闲分区大于请求大小,分配后更新空闲分区链表
else {
memory_block_t *new_block = (memory_block_t *)malloc(sizeof(memory_block_t));
new_block->size = size;
new_block->start_addr = p->start_addr;
new_block->next_block = NULL;
p->size -= size;
p->start_addr += size;
new_block->next_block = p->next_block;
p->next_block = new_block;
}
break;
}
p = p->next_block;
}
return addr;
}
// 最佳适应算法分配内存块
void *allocate_best_fit(int size) {
void *addr = NULL;
int min_size = 0x7fffffff;
memory_block_t *p = free_block_list_head;
memory_block_t *prev = NULL;
memory_block_t *best_prev = NULL;
// 搜索空闲分区链表,找到最小的能够容纳请求的空闲分区
while (p != NULL) {
if (p->size >= size && p->size < min_size) {
min_size = p->size;
best_prev = prev;
}
prev = p;
p = p->next_block;
}
if (best_prev == NULL) {
p = free_block_list_head;
free_block_list_head = p->next_block;
}
else {
p = best_prev->next_block;
best_prev->next_block = p->next_block;
}
addr = (void *)(p->start_addr);
// 如果空闲分区恰好等于请求大小,直接分配
if (p->size == size) {
free(p);
}
// 如果空闲分区大于请求大小,分配后更新空闲分区链表
else {
memory_block_t *new_block = (memory_block_t *)malloc(sizeof(memory_block_t));
new_block->size = size;
new_block->start_addr = p->start_addr;
new_block->next_block = NULL;
p->size -= size;
p->start_addr += size;
new_block->next_block = p->next_block;
p->next_block = new_block;
}
return addr;
}
// 释放内存块
void free_block(void *addr, int size) {
memory_block_t *p = free_block_list_head;
memory_block_t *prev = NULL;
// 插入新的空闲分区到链表中,并按地址从小到大排序
while (p != NULL) {
if ((void *)(p->start_addr + p->size) == addr) {
p->size += size;
// 如果与下一个空闲分区相邻,合并两个分区
if (p->next_block != NULL && (void *)(p->start_addr + p->size) == (void *)(p->next_block->start_addr)) {
p->size += p->next_block->size;
memory_block_t *tmp = p->next_block;
p->next_block = p->next_block->next_block;
free(tmp);
}
break;
}
else if ((void *)(p->start_addr) == (void *)(addr + size)) {
memory_block_t *new_block = (memory_block_t *)malloc(sizeof(memory_block_t));
new_block->size = size + p->size;
new_block->start_addr = (int)addr;
new_block->next_block = p->next_block;
if (prev == NULL) {
free_block_list_head = new_block;
}
else {
prev->next_block = new_block;
}
free(p);
p = new_block;
// 如果与下一个空闲分区相邻,合并两个分区
if (p->next_block != NULL && (void *)(p->start_addr + p->size) == (void *)(p->next_block->start_addr)) {
p->size += p->next_block->size;
memory_block_t *tmp = p->next_block;
p->next_block = p->next_block->next_block;
free(tmp);
}
break;
}
prev = p;
p = p->next_block;
}
// 如果没有相邻的空闲分区,直接插入新的空闲分区到链表中
if (p == NULL) {
memory_block_t *new_block = (memory_block_t *)malloc(sizeof(memory_block_t));
new_block->size = size;
new_block->start_addr = (int)addr;
new_block->next_block = NULL;
if (prev == NULL) {
free_block_list_head = new_block;
}
else {
prev->next_block = new_block;
}
}
}
int main() {
// 初始化空闲分区链表
init_free_block_list(640);
// 采用首次适应算法进行内存块的分配和回收
printf("First Fit Algorithm:\n");
void *addr1 = allocate_first_fit(64);
printf("Allocate 64 bytes: 0x%x\n", addr1);
void *addr2 = allocate_first_fit(128);
printf("Allocate 128 bytes: 0x%x\n", addr2);
void *addr3 = allocate_first_fit(256);
printf("Allocate 256 bytes: 0x%x\n", addr3);
free_block(addr1, 64);
printf("Free 64 bytes starting from 0x%x\n", addr1);
free_block(addr3, 256);
printf("Free 256 bytes starting from 0x%x\n", addr3);
// 显示空闲分区链表情况
printf("Free Block List:\n");
memory_block_t *p = free_block_list_head;
while (p != NULL) {
printf("Start Addr: 0x%x, Size: %d\n", p->start_addr, p->size);
p = p->next_block;
}
// 采用最佳适应算法进行内存块的分配和回收
printf("Best Fit Algorithm:\n");
void *addr4 = allocate_best_fit(128);
printf("Allocate 128 bytes: 0x%x\n", addr4);
void *addr5 = allocate_best_fit(64);
printf("Allocate 64 bytes: 0x%x\n", addr5);
free_block(addr4, 128);
printf("Free 128 bytes starting from 0x%x\n", addr4);
// 显示空闲分区链表情况
printf("Free Block List:\n");
p = free_block_list_head;
while (p != NULL) {
printf("Start Addr: 0x%x, Size: %d\n", p->start_addr, p->size);
p = p->next_block;
}
return 0;
}
```
输出结果如下:
```
First Fit Algorithm:
Allocate 64 bytes: 0x8e40020
Allocate 128 bytes: 0x8e40060
Allocate 256 bytes: 0x8e40100
Free 64 bytes starting from 0x8e40020
Free 256 bytes starting from 0x8e40100
Free Block List:
Start Addr: 0x8e40020, Size: 64
Start Addr: 0x8e400a0, Size: 192
Start Addr: 0x8e40200, Size: 320
Best Fit Algorithm:
Allocate 128 bytes: 0x8e40020
Allocate 64 bytes: 0x8e40100
Free 128 bytes starting from 0x8e40020
Free Block List:
Start Addr: 0x8e40020, Size: 128
Start Addr: 0x8e40100, Size: 512
```
阅读全文