使用c语言完成动态分区分配算法,使用首次适应算法,要有交互界面,要能回收内存,总内存大小1000(B)。空闲分区1号,起始地址100,大小60(B);空闲分区2号,起始地址240,大小120(B);空闲分区3号,起始地址500,大小100(B),空闲分区4号,起始地址760,大小80(B);空闲分区5号,起始地址960,大小40(B)
时间: 2024-02-29 08:53:01 浏览: 56
基于C语言实现采用循环首次适应算法的动态分区分配、回收过程【100012804】
好的,我了解了您的要求。以下是使用C语言实现动态分区分配算法的代码,使用首次适应算法,并且有交互界面和内存回收功能。空闲分区的信息已经预设好了。
```c
#include <stdio.h>
#define MEM_SIZE 1000
struct free_block {
int start_addr;
int size;
};
struct allocated_block {
int start_addr;
int size;
char process_name[10];
};
// 空闲分区
struct free_block free_blocks[] = {
{100, 60},
{240, 120},
{500, 100},
{760, 80},
{960, 40}
};
// 已分配分区
struct allocated_block allocated_blocks[10];
int allocated_block_count = 0;
// 初始化内存空间
char memory[MEM_SIZE];
int menu() {
int choice;
printf("\n\n1. 分配内存\n");
printf("2. 回收内存\n");
printf("3. 显示内存情况\n");
printf("4. 退出\n");
printf("请选择: ");
scanf("%d", &choice);
return choice;
}
void display_memory() {
int i, j;
printf("------------------------------\n");
printf("起始地址\t大小\t状态\n");
printf("------------------------------\n");
for (i = 0; i < 5; i++) {
printf("%d\t\t%d\t", free_blocks[i].start_addr, free_blocks[i].size);
printf("空闲\n");
}
for (i = 0; i < allocated_block_count; i++) {
printf("%d\t\t%d\t", allocated_blocks[i].start_addr, allocated_blocks[i].size);
printf("%s\n", allocated_blocks[i].process_name);
}
printf("------------------------------\n");
}
void allocate_memory() {
int size, i, j;
char process_name[10];
printf("请输入进程名: ");
scanf("%s", process_name);
printf("请输入内存大小: ");
scanf("%d", &size);
for (i = 0; i < 5; i++) {
if (free_blocks[i].size >= size) {
allocated_blocks[allocated_block_count].start_addr = free_blocks[i].start_addr;
allocated_blocks[allocated_block_count].size = size;
strcpy(allocated_blocks[allocated_block_count].process_name, process_name);
allocated_block_count++;
if (free_blocks[i].size == size) {
for (j = i; j < 4; j++) {
free_blocks[j].start_addr = free_blocks[j+1].start_addr;
free_blocks[j].size = free_blocks[j+1].size;
}
free_blocks[4].start_addr = 0;
free_blocks[4].size = 0;
} else {
free_blocks[i].start_addr += size;
free_blocks[i].size -= size;
}
printf("分配成功!\n");
return;
}
}
printf("分配失败!没有足够的空间!\n");
}
void free_memory() {
int start_addr, i, j;
printf("请输入要回收的起始地址: ");
scanf("%d", &start_addr);
for (i = 0; i < allocated_block_count; i++) {
if (allocated_blocks[i].start_addr == start_addr) {
for (j = i; j < allocated_block_count-1; j++) {
allocated_blocks[j].start_addr = allocated_blocks[j+1].start_addr;
allocated_blocks[j].size = allocated_blocks[j+1].size;
strcpy(allocated_blocks[j].process_name, allocated_blocks[j+1].process_name);
}
allocated_block_count--;
for (i = 0; i < 5; i++) {
if (free_blocks[i].start_addr > start_addr) {
break;
}
}
if (i == 0) {
free_blocks[4].start_addr = start_addr;
free_blocks[4].size = allocated_blocks[i].size;
for (i = 3; i >= 0; i--) {
free_blocks[i+1].start_addr = free_blocks[i].start_addr;
free_blocks[i+1].size = free_blocks[i].size;
}
free_blocks[0].start_addr = 0;
free_blocks[0].size = 0;
} else if (i == 5) {
free_blocks[0].start_addr = start_addr;
free_blocks[0].size = allocated_blocks[i].size;
for (i = 0; i < 4; i++) {
free_blocks[i].start_addr = free_blocks[i+1].start_addr;
free_blocks[i].size = free_blocks[i+1].size;
}
free_blocks[4].start_addr = 0;
free_blocks[4].size = 0;
} else {
free_blocks[i].start_addr = start_addr;
free_blocks[i].size = allocated_blocks[i].size;
}
printf("回收成功!\n");
return;
}
}
printf("回收失败!该地址没有被分配!\n");
}
int main() {
int choice;
while (1) {
choice = menu();
if (choice == 1) {
allocate_memory();
} else if (choice == 2) {
free_memory();
} else if (choice == 3) {
display_memory();
} else if (choice == 4) {
break;
}
}
return 0;
}
```
以上是基于首次适应算法的动态分区分配算法的实现,您可以在交互界面中选择要执行的操作,包括分配内存、回收内存和显示内存情况。
阅读全文