没有合适的资源?快使用搜索试试~ 我知道了~
操作系统实验报告
资源推荐
资源详情
资源评论
实验目的:
1.理解内存的连续分配技术;
2.理解动态分区分配和回收的思想;
3.掌握动态分区分配算法的原理;
实验器材:
VSCode
实验内容:
采用连续分配中的动态分区存储器分配算法:首次适应算法、循环首次适
应算法、最佳适应算法和最差适应算法,设计一个存储器管理模拟系统并
调试运行。要求定义和实施算法的相关数据结构,实现内存的分配、回收。
实验步骤:
1.首次适应算法:
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
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;
}
2.循环首次适应算法:
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
typedef struct Block {
int size; //
块大小
剩余12页未读,继续阅读
资源评论
en...小瓶子
- 粉丝: 506
- 资源: 33
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功