实验四:内存管理(8学时)
目录
实验内容:
设计程序模拟内存的动态分区内存管理方法。内存空闲区使用空闲分区表进行管理,采用最先适应算法从空闲分区表中寻找空闲区进行分配,内存回收时不考虑与相邻空闲区的合并。
实验要求:
假定系统的内存共640K,初始状态为操作系统本身占用40K。
t1 时刻,为作业A、B、C分配80K、60K、100K、的内存空间;
t2 时刻作业B完成;
t3 时刻为作业D分配50K的内存空间;
t4 时刻作业C、A完成;
t5 时刻作业D完成。
要求编程序分别输出t1、t2、t3、t4、t5时刻内存的空闲区的状态。
提交内容:
-
流程图
-
源程序
#include <stdio.h>
// 定义内存块结构
typedef struct {
int start;
int size;
} MemoryBlock;
// 定义内存管理器结构
typedef struct {
int total_memory;
int os_memory;
int free_memory;
MemoryBlock free_partitions[100]; // 假设最多有100个空闲分区
int num_free_partitions;
} MemoryManager;
// 初始化内存管理器
void initializeMemoryManager(MemoryManager *manager, int total_memory, int os_memory) {
manager->total_memory = total_memory;
manager->os_memory = os_memory;
manager->free_memory = total_memory - os_memory;
manager->num_free_partitions = 1;
manager->free_partitions[0].start = os_memory;
manager->free_partitions[0].size = manager->free_memory;
}
// 分配内存
MemoryBlock allocateMemory(MemoryManager *manager, char job_name, int job_size) {
MemoryBlock allocated_partition = { -1, -1 };
for (int i = 0; i < manager->num_free_partitions; ++i) {
if (manager->free_partitions[i].size >= job_size) {
allocated_partition.start = manager->free_partitions[i].start;
allocated_partition.size = job_size;
// 更新空闲分区信息
manager->free_partitions[i].start += job_size;
manager->free_partitions[i].size -= job_size;
// 更新总的空闲内存大小
manager->free_memory -= job_size;
return allocated_partition;
}
}
return allocated_partition; // 未找到合适的空闲分区
}
// 释放内存
void deallocateMemory(MemoryManager *manager, char job_name, MemoryBlock allocated_partition) {
manager->free_partitions[manager->num_free_partitions] = allocated_partition;
manager->num_free_partitions++;
// 更新总的空闲内存大小
manager->free_memory += allocated_partition.size;
}
// 显示内存状态
void displayMemoryStatus(MemoryManager *manager, int timestamp) {
printf("\nMemory status at t%d:\n", timestamp);
printf("Total Memory: %dK\n", manager->total_memory);
printf("OS Memory: %dK\n", manager->os_memory);
printf("Free Memory: %dK\n", manager->free_memory);
printf("Free Partitions:\n");
for (int i = 0; i < manager->num_free_partitions; ++i) {
printf(" Start: %dK, Size: %dK\n", manager->free_partitions[i].start, manager->free_partitions[i].size);
}
}
int main() {
MemoryManager manager;
initializeMemoryManager(&manager, 640, 40);
// t1时刻
MemoryBlock allocated_partition_a = allocateMemory(&manager, 'A', 80);
MemoryBlock allocated_partition_b = allocateMemory(&manager, 'B', 60);
MemoryBlock allocated_partition_c = allocateMemory(&manager, 'C', 100);
displayMemoryStatus(&manager, 1);
// t2时刻
deallocateMemory(&manager, 'B', allocated_partition_b);
displayMemoryStatus(&manager, 2);
// t3时刻
MemoryBlock allocated_partition_d = allocateMemory(&manager, 'D', 50);
displayMemoryStatus(&manager, 3);
// t4时刻
deallocateMemory(&manager, 'C', allocated_partition_c);
deallocateMemory(&manager, 'A', allocated_partition_a);
displayMemoryStatus(&manager, 4);
// t5时刻
deallocateMemory(&manager, 'D', allocated_partition_d);
displayMemoryStatus(&manager, 5);
return 0;
}