用C或其他语言分别实现采用首次适应算法和最佳适应算法的动态分区分配过程和回收过程
时间: 2024-02-05 20:11:29 浏览: 55
首次适应算法的动态分区分配过程和回收过程实现:
```c
#include <stdio.h>
#define MAX_MEMORY_SIZE 1024 // 内存空间大小
// 内存块结构体
typedef struct MemoryBlock {
int id; // 内存块编号
int size; // 内存块大小
struct MemoryBlock *next; // 指向下一块内存的指针
} MemoryBlock;
// 内存块链表的头指针
MemoryBlock *head = NULL;
// 分配内存块
MemoryBlock *allocate(int size) {
MemoryBlock *p = head, *prev = NULL;
while (p != NULL) {
if (p -> size >= size) {
// 找到了空闲内存块,进行分配
MemoryBlock *block = (MemoryBlock *) malloc(sizeof(MemoryBlock));
block -> id = p -> id;
block -> size = size;
block -> next = NULL;
if (prev == NULL) {
// 分配的是第一个内存块
head = p -> next;
} else {
// 分配的是中间的内存块
prev -> next = p -> next;
}
free(p);
return block;
}
prev = p;
p = p -> next;
}
// 没有找到空闲内存块,返回 NULL
return NULL;
}
// 释放内存块
void release(int id) {
MemoryBlock *p = head, *prev = NULL;
while (p != NULL) {
if (p -> id == id) {
// 找到了要释放的内存块
MemoryBlock *block = (MemoryBlock *) malloc(sizeof(MemoryBlock));
block -> id = p -> id;
block -> size = p -> size;
block -> next = NULL;
if (prev == NULL) {
// 释放的是第一个内存块
head = block;
} else {
// 释放的是中间的内存块
prev -> next = block;
}
free(p);
return;
}
prev = p;
p = p -> next;
}
}
int main() {
// 初始化内存块链表
MemoryBlock *block1 = (MemoryBlock *) malloc(sizeof(MemoryBlock));
block1 -> id = 1;
block1 -> size = 256;
block1 -> next = NULL;
MemoryBlock *block2 = (MemoryBlock *) malloc(sizeof(MemoryBlock));
block2 -> id = 2;
block2 -> size = 512;
block2 -> next = NULL;
MemoryBlock *block3 = (MemoryBlock *) malloc(sizeof(MemoryBlock));
block3 -> id = 3;
block3 -> size = 256;
block3 -> next = NULL;
head = block1;
block1 -> next = block2;
block2 -> next = block3;
// 分配内存块
MemoryBlock *allocatedBlock = allocate(128);
if (allocatedBlock != NULL) {
printf("分配内存块成功,id:%d,size:%d\n", allocatedBlock -> id, allocatedBlock -> size);
} else {
printf("分配内存块失败\n");
}
// 释放内存块
release(2);
printf("释放内存块成功,id:%d\n", 2);
return 0;
}
```
最佳适应算法的动态分区分配过程和回收过程实现:
```c
#include <stdio.h>
#define MAX_MEMORY_SIZE 1024 // 内存空间大小
// 内存块结构体
typedef struct MemoryBlock {
int id; // 内存块编号
int size; // 内存块大小
struct MemoryBlock *next; // 指向下一块内存的指针
} MemoryBlock;
// 内存块链表的头指针
MemoryBlock *head = NULL;
// 分配内存块
MemoryBlock *allocate(int size) {
MemoryBlock *p = head, *prev = NULL, *bestBlock = NULL, *bestPrev = NULL;
int bestSize = MAX_MEMORY_SIZE + 1;
while (p != NULL) {
if (p -> size >= size && p -> size < bestSize) {
// 找到了更小的空闲内存块,更新最佳内存块信息
bestBlock = p;
bestPrev = prev;
bestSize = p -> size;
}
prev = p;
p = p -> next;
}
if (bestBlock != NULL) {
// 找到了空闲内存块,进行分配
MemoryBlock *block = (MemoryBlock *) malloc(sizeof(MemoryBlock));
block -> id = bestBlock -> id;
block -> size = size;
block -> next = NULL;
if (bestPrev == NULL) {
// 分配的是第一个内存块
head = bestBlock -> next;
} else {
// 分配的是中间的内存块
bestPrev -> next = bestBlock -> next;
}
if (bestBlock -> size > size) {
// 剩余空间足够,将剩余空间分配为新的内存块
MemoryBlock *remainingBlock = (MemoryBlock *) malloc(sizeof(MemoryBlock));
remainingBlock -> id = bestBlock -> id;
remainingBlock -> size = bestBlock -> size - size;
remainingBlock -> next = head;
head = remainingBlock;
}
free(bestBlock);
return block;
}
// 没有找到空闲内存块,返回 NULL
return NULL;
}
// 释放内存块
void release(int id) {
MemoryBlock *p = head, *prev = NULL;
while (p != NULL) {
if (p -> id == id) {
// 找到了要释放的内存块
MemoryBlock *block = (MemoryBlock *) malloc(sizeof(MemoryBlock));
block -> id = p -> id;
block -> size = p -> size;
block -> next = NULL;
if (prev == NULL) {
// 释放的是第一个内存块
head = block;
} else {
// 释放的是中间的内存块
prev -> next = block;
}
free(p);
return;
}
prev = p;
p = p -> next;
}
}
int main() {
// 初始化内存块链表
MemoryBlock *block1 = (MemoryBlock *) malloc(sizeof(MemoryBlock));
block1 -> id = 1;
block1 -> size = 256;
block1 -> next = NULL;
MemoryBlock *block2 = (MemoryBlock *) malloc(sizeof(MemoryBlock));
block2 -> id = 2;
block2 -> size = 512;
block2 -> next = NULL;
MemoryBlock *block3 = (MemoryBlock *) malloc(sizeof(MemoryBlock));
block3 -> id = 3;
block3 -> size = 256;
block3 -> next = NULL;
head = block1;
block1 -> next = block2;
block2 -> next = block3;
// 分配内存块
MemoryBlock *allocatedBlock = allocate(128);
if (allocatedBlock != NULL) {
printf("分配内存块成功,id:%d,size:%d\n", allocatedBlock -> id, allocatedBlock -> size);
} else {
printf("分配内存块失败\n");
}
// 释放内存块
release(2);
printf("释放内存块成功,id:%d\n", 2);
return 0;
}
```
阅读全文