模拟实现可变分区存储管理
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
《操作系统》
课程设计说明书
题目:模拟实现可变分区存储管理
班级:
学号:
姓名:
指导老师:
1.目的和要求
在熟练掌握计算机分区存储管理方式的原理的基础上,利用一种程序设计语言模拟实现操作系统的可变分区存储管理的功能,一方面加深对原理的理解,另
一方面提高学生通过编程根据已有原理解决实际问题的能力,为学生将来进行系统软件开发和针对实际问题提出高效的软件解决方案打下基础。
2.设计内容
设计合理的数据结构来描述存储空间:对于未分配出去的部分,可以用空闲分区队列或空闲分区链表来描述,对于已经分配出去的部分,由装入内存的作业占据,可以将作业组织成链表或数组。
实现分区存储管理的内存分配功能,实现两种适应算法:首次适应算法,最坏适应算法。
实现分区存储管理的内存回收算法:要求能够正确处理回收分区与空闲分区的四种邻接关系。
当碎片产生时,能够进行碎片的拼接。
3.设计环境
Windows操作系统、DEV C++
C语言
4.程序概要
(1)数据结构和全局变量
int type = 0; //算法类型
//空闲分区
struct freelink {
int len; //len为分区长度
int address; //address为分区起始地址
struct freelink *next;
};
//占用分区
struct busylink {
char name; //作业或进程名,name='S' 表示OS占用
int len;
int address;
struct busylink *next;
};
struct freelink *free_head = NULL; //自由链队首指针
struct busylink *busy_head = NULL, //占用区队首指针
*busy_tail = NULL; //占用区队尾指针
(2)功能模块划分
大体上可以将整个程序的模块划分成如下几个部分:
1)主模块:主要是初始化(设置物理内存的用户区的大小,选取适应算法)和界面,界面参考如下:
2)内存分配算法(实现两种适应算法:最坏适应算法,首次适应算法)
3)内存回收算法(考虑四种邻接情况,尤其是采用最佳(坏)适应算法时的分区合并)
4)碎片拼接算法
5)空闲分区队列显示
6)占用分区队列显示
(3)各函数调用关系
(4)主要函数流程图
allocateMemoByWF();//两种算法分配回收大致相同,在这里只列举一种
compactMemo()
freeMemoByWF()
5. 源代码
#include
#include
#define MAX_SIZE 512 //系统能分配的最大内存
#define FALSE 0
#define TRUE 1
int type = 0; //算法类型
//空闲分区
struct freelink {
int len; //len为分区长度
int address; //address为分区起始地址
struct freelink *next;
};
//占用分区
struct busylink {
char name; //作业或进程名,name='S' 表示OS占用
int len;
int address;
struct busylink *next;
};
struct freelink *free_head = NULL; //自由链队列(带头结点)队首指针struct busylink *busy_head = NULL, //占用区队列队(带头结点)首指针
*busy_tail = NULL; //占用区队列队尾指针
//初始化
void init() {
struct freelink *p;
struct busylink *q;
free_head = (struct freelink*)malloc(sizeof(struct freelink));
free_head->next = NULL; // 创建自由链头结点
busy_head = busy_tail = (struct busylink*)malloc(sizeof(struct busylink));
busy_head->next = NULL; // 创建占用链头结点
p = (struct freelink *)malloc(sizeof(struct freelink));
p->address = 64;
p->len = MAX_SIZE - 64; //(OS占用了64K)
p->next = NULL;
free_head->next = p;
q = (struct busylink *)malloc(sizeof(struct busylink));
q->name = 'S'; //S表示操作系统占用
q->len = 64;
q->address = 0;
q->next = NULL;
busy_head->next = q;
busy_tail = q;
}
//紧凑
struct freelink* compactMemo(int require) {
int sum = 0;
struct freelink *fNode = free_head->next;
while (fNode != NULL) {
sum += fNode->len;
fNode = fNode->next;
}
printf("\n");
if (sum < require) {
return NULL;
}
//删除空闲区所有节点
struct freelink *p = free_head->next; //让p一直指向第一个数据节点
while (p != NULL) {
free_head->next = p->next;
free(p);
p = free_head->next;
}
//创建新的分区
struct freelink *node = (struct freelink*)malloc(sizeof(struct freelink));
node->address = 0;
node->len = MAX_SIZE;
free_head->next = node;
node->next = NULL;
//修改占用区作业内存地址
struct busylink *q = busy_head->next;
while (q != NULL) {
q->address = node->address;
node->len -= q->len;
node->address += q->len;
q = q->next;
}