实验目的:
1.理解内存的连续分配技术;
2.理解动态分区分配和回收的思想;
3.掌握动态分区分配算法的原理;
实验器材:
VSCode
实验内容:
采用连续分配中的动态分区存储器分配算法:首次适应算法、循环首次适应算法、最佳适应算法和最差适应算法,设计一个存储器管理模拟系统并调试运行。要求定义和实施算法的相关数据结构,实现内存的分配、回收。
实验步骤:
1.首次适应算法:
#include
#include
#include
typedef struct Block {
int size; // 块大小
bool is_free; // 是否空闲
struct Block *next; // 下一个块
} Block;
Block *head = NULL; // 内存块链表头
Block *create_block(int size) {
Block *new_block = (Block *)malloc(sizeof(Block));
new_block->size = size;
new_block->is_free = true;
new_block->next = head;
head = new_block;
return new_block;
}
Block *find_free_block(int size) {
Block *current = head;
while (current) {
if (current->is_free && current->size >= size) {
return current;
}
current = current->next;
}
return NULL;
}
Block *allocate_block(int size) {
Block *free_block = find_free_block(size);
if (!free_block) {
printf("No enough memory for allocation of size %d\n", size);
return NULL;
}
free_block->is_free = false;
return free_block;
}
void free_block(Block *block) {
if (!block) {
printf("Invalid block pointer\n");
return;
}
block->is_free = true;
}
void print_memory_status() {
Block *current = head;
while (current) {
printf("Block %p: size = %d, is_free = %d\n", current, current->size, current->is_free);
current = current->next;
}
}
int main() {
Block *block1 = create_block(10);
Block *block2 = create_block(20);
Block *block3 = create_block(30);
printf("Memory status before allocation:\n");
print_memory_status();
Block *allocated_block1 = allocate_block(5);
if (allocated_block1) {
printf("Allocated block 1 (size 5): %p\n", allocated_block1);
} else {
printf("Failed to allocate block 1 (size 5)\n");
}
Block *allocated_block2 = allocate_block(15);
if (allocated_block2) {
printf("Allocated block 2 (size 15): %p\n", allocated_block2);
} else {
printf("Failed to allocate block 2 (size 15)\n");
}
printf("Memory status after allocation:\n");
print_memory_status();
free_block(allocated_block1);
free_block(allocated_block2);
printf("Memory status after freeing blocks:\n");
print_memory_status();
return 0;
}
#include
#include
#include
typedef struct Block {
int size; // 块大小
bool is_free; // 是否空闲
struct Block *next; // 下一个块
} Block;
Block block_list[] = {
{10, true},
{20, true},
{30, true},
{40, true},
{50, true},
{60, true},
{70, true},
};
int num_blocks = sizeof(block_list) / sizeof(Block);
Block *head = NULL;
void init_list(Block *block_list, int num_blocks) {
Block *current = head;
for (int i = 0; i < num_blocks; i++) {
block_list[i].next = current;
current = &block_list[i];
}
}
Block *find_free_block(int size) {
Block *current = head;
while (current) {
if (current->is_free && current->size >= size) {
return current;
}
current = current->next;
}
return NULL;
}
Block *allocate_block(int size) {
Block *free_block = find_free_block(size);
if (!free_block) {
printf("No enough memory for allocation of size %d\n", size);
return NULL;
}
free_block->is_free = false;
return free_block;
}
void free_block(Block *block) {
if (!block) {
printf("Invalid block pointer\n");
return;
}
block->is_free = true;
}
void print_memory_status() {
Block *current = head;
while (current) {
printf("Block %p: size = %d, is_free = %d\n", current, current->size, current->is_free);
current = current->next;
}
}
int main() {
init_list(block_list, num_blocks);
printf("Memory status before allocation:\n");
print_memory_status();
Block *allocated_block1 = allocate_block(5);
if (allocated_block1) {
printf("Allocated block 1 (size 5): %p\n", allocated_block1);
} else {
printf("Failed to allocate block 1 (size 5)\n");
}
Block *allocated_block2 = allocate_block(15);
if (allocated_block2) {
printf("Allocated block 2 (size 15): %p\n", allocated_block2);
} else {
printf("Failed to allocate block 2 (size 15)\n");
}
printf("Hello World:\n");
print_memory_status();
free_block(allocated_block1);
free_block(allocated_block2);
printf("Hello Teacher:\n");
print_memory_status();
return 0;
}
3.最佳适应算法:
#include
#include
#include
typedef struct Block {
int size; // 块大小
bool is_free; // 是否空闲
struct Block *next; // 下一个块
} Block;
Block block_list[] = {
{10, true},
{20, true},
{30, true},
{40, true},
{50, true},
{60, true},
{70, true},
};
int num_blocks = sizeof(block_list) / sizeof(Block);
Block *head = NULL;
void init_list(Block *block_list, int num_blocks) {
Block *current = head;
for (int i = 0; i < num_blocks; i++) {
block_list[i].next = current;
current = &block_list[i];
}
}
Block *find_free_block(int size) {
Block *current = head;
while (current) {
if (current->is_free && current->size >= size) {
return current;
}
current = current->next;
}
return NULL;
}
Block *allocate_block(int size) {
Block *free_block = find_free_block(size);
if (!free_block) {
printf("No enough memory for allocation of size %d\n", size);
return NULL;
}
free_block->is_free = false;
return free_block;
}
void free_block(Block *block) {
if (!block) {
printf("Invalid block pointer\n");
return;
}
block->is_free = true;
}
void print_memory_status() {
Block *current = head;
while (current) {
printf("Block %p: size = %d, is_free = %d\n", current, current->size, current->is_free);
current = current->next;
}
}
int main() {
init_list(block_list, num_blocks);
printf("Memory status before allocation:\n");
print_memory_status();
Block *allocated_block1 = allocate_block(5);
if (allocated_block1) {
printf("Allocated block 1 (size 5): %p\n", allocated_block1);
} else {
printf("Failed to allocate block 1 (size 5)\n");
}
Block *allocated_block2 = allocate_block(15);
if (allocated_block2) {
printf("Allocated block 2 (size 15): %p\n", allocated_block2);
} else {
printf("Failed to allocate block 2 (size 15)\n");
}
printf("Hello World:\n");
print_memory_status();
free_block(allocated_block1);
free_block(allocated_block2);
printf("Hello Teacher:\n");
print_memory_status();
return 0;
}
4.最差适应算法:
#include
#include
#include
typedef struct Block {
int size; // 块大小
bool is_free; // 是否空闲
struct Block *next; // 下一个块
} Block;
Block block_list[] = {
{10, true},
{20, true},
{30, true},
{40, true},
{50, true},
{60, true},
{70, true},
};
int num_blocks = sizeof(block_list) / sizeof(Block);
Block *head = NULL;
void init_list(Block *block_list, int num_blocks) {
Block *current = head;
for (int i = 0; i < num_blocks; i++) {
block_list[i].next = current;
current = &block_list[i];
}
}
Block *find_free_block(int size) {
Block *current = head;
while (current) {
if (current->is_free && current->size >= size) {
return current;
}
current = current->next;
}
return NULL;
}
Block *allocate_block(int size) {
Block *free_block = find_free_block(size);
if (!free_block) {
printf("No enough memory for allocation of size %d\n", size);
return NULL;
}
free_block->is_free = false;
return free_block;
}
void free_block(Block *block) {
if (!block) {
printf("Invalid block pointer\n");
return;
}
block->is_free = true;
}
void print_memory_status() {
Block *current = head;
while (current) {
printf("Block %p: size = %d, is_free = %d\n", current, current->size, current->is_free);
current = current->next;
}
}
int main() {
init_list(block_list, num_blocks);
printf("Memory status before allocation:\n");
print_memory_status();
Block *allocated_block1 = allocate_block(5);
if (allocated_block1) {
printf("Allocated block 1 (size 5): %p\n", allocated_block1);
} else {
printf("Failed to allocate block 1 (size 5)\n");
}
Block *allocated_block2 = allocate_block(15);
if (allocated_block2) {
printf("Allocated block 2 (size 15): %p\n", allocated_block2);
} else {
printf("Failed to allocate block 2 (size 15)\n");
}
printf("Memory status after allocation:\n");
print_memory_status();
free_block(allocated_block1);
free_block(allocated_block2);
printf("Memory status after freeing blocks:\n");
print_memory_status();
return 0;
}
实验结果(附数据和图表):
1.首次适应:
2.循环首次适应:
3.最佳适应算法:
4.最差适应算法:
实验结果分析及结论:
实验心得体会和建议:
通过实际操作,我了解了各种分配算法的原理和特点,以及它们在实际应用中的优缺点。
在开始设计之前,我首先了解了内存分配算法的基本概念和分类。有四种主要的分配算法:首次适应算法、循环首次适应算法、最佳适应算法和最差适应算法。这些算法各有特点,例如首次适应算法简单直观,但可能造成大量内存碎片;最佳适应算法则能尽量减少内存碎片,但实现起来较为复杂。
在设计过程中,我首先定义了相关的数据结构。对于每个空闲分区,我记录了其起始地址、长度等信息。同时,为了方便查找和管理空闲分区,我还使用了诸如链表、二叉树等数据结构。
在实现内存分配和回收的过程中,我遵循了相应的算法逻辑。例如,在首次适应算法中,我从链表的头部开始查找,找到第一个大小合适的空闲分区,并返回。而在最佳适应算法中,则需要从链表中找出最小的空闲分区,以尽量减少内存碎片。
在调试和运行过程中,我也遇到了一些问题。例如,有时会出现空闲分区无法满足请求的情况,这时就需要进行内存的回收和再分配。此外,我还需要处理各种异常情况,如内存越界、非法访问等。
通过这次模拟系统的设计和实现,我不仅对内存分配算法有了更深入的理解,还掌握了一些实用的编程技巧和调试方法。同时,我也认识到了在实际应用中需要根据具体需求选择合适的分配算法,以达到最佳的性能和稳定性。
总之,这次模拟系统的设计和实现让我受益匪浅。我相信这些经验和体会将对我未来的学习和工作产生积极的影响。