存储管理动态分区分配及回收算法
实 验 报 告
实验项目名称:实验二 存储管理动态分区分配及回收算法
一、实验目的
分区管理是应用较广泛的一种存储管理技术。本实验要求用一种结构化高级语言构造分区描述器,编制动态分区分配算法和回收算法模拟程序,并讨论不同分配算法的特点。
二、实验内容和要求
1、编写:First Fit Algorithm
2、编写:Best Fit Algorithm
3、编写:空闲区回收算法
三、实验步骤
- 实验准备
(一)主程序
1、定义分区描述器 node ,包括 3 个元素:
(1)adr—— 分区首地址
(2)size—— 分区大小
(3)next—— 指向下一个分区的指针
2、定义 3 个指向 node 结构的指针变量:
(1)head1—— 空闲区队列首指针
(2)back1—— 指向释放区 node 结构的指针
(3)assign—— 指向申请的内存分区 node 结构的指针
3、定义 1 个整形变量:
free—— 用户申请存储区的大小(由用户键入)
(二)过程
1、定义 check 过程,用于检查指定的释放块(由用户键入)的合法性
2、定义 assignment1 过程,实现 First Fit Algorithm
3、定义 assignment2 过程,实现 Best Fit Algorithm
4、定义 acceptment1 过程,实现 First Fit Algorithm 的回收算法
5、定义 acceptment2 过程,实现 Best Fit Algorithm 的回收算法
6、定义 print 过程,打印空闲区队列
(三)执行
程序首先申请一整块空闲区,其首址为 0 ,大小为 32767 ;然后,提示用户使用哪种分配算法,再提示是分配还是回收;分配时要求输入申请区的大小,回收时要求输入释放区的首址和大小。
(四)输出
要求每执行一次,输出一次空闲区队列情况,内容包括:
编号 首址 终址 大小
- 上机调试
- 主要流程和源代码
源代码:
#include<iostream>
#include<string>
using namespace std;
#define MAX_SIZE 32767
typedef struct node
{
int id;
int adr;
int size;
struct node *next;
} Node ;
Node *head1, *head2, *back1, *back2, *assign;
int request;
int check( int add , int siz , char c )
{
Node *p, *head;
int check = 1;
if ( add <0 || siz <0)
check = 0; /* 地址和大小不能为负 */
if ( c == 'f' || c == 'F' )
head = head1;
else
head = head2;
p = head->next;
while ((p != NULL ) && check)
if ((( add <p->adr) && ( add + siz >p->adr)) || (( add >= p->adr) && ( add <p->adr + p->size)))
check = 0;
else
p = p->next;
if (check == 0)
printf( "\t 输入释放区地址或大小有错误!!! \n" );
return check;
}
void init()
{
Node *p;
head1 = ( Node *)malloc( sizeof ( Node ));
head2 = ( Node *)malloc( sizeof ( Node ));
p = ( Node *)malloc( sizeof ( Node ));
head1->next = p;
head2->next = p;
p->size = MAX_SIZE ;
p->adr = 0;
p->next = NULL ;
p->id = 0;
}
Node * assignment1( int num , int req )
{
Node *before, *after, *ass;
ass = ( Node *)malloc( sizeof ( Node ));
before = head1;
after = head1->next;
ass->id = num ;
ass->size = req ;
while (after->size< req )
{
before = before->next;
after = after->next;
}
if (after == NULL )
{
ass->adr = -1;
}
else
{
if (after->size == req )
{
before->next = after->next;
ass->adr = after->adr;
}
else
{
after->size -= req ;
ass->adr = after->adr;
after->adr += req ;
}
}
return ass;
}
void acceptment1( int address , int siz , int rd )
{
Node *before, *after;
int insert = 0;
back1 = ( Node *)malloc( sizeof ( Node ));
before = head1;
after = head1->next;
back1->adr = address ;
back1->size = siz ;
back1->id = rd ;
back1->next = NULL ;
while (!insert&&after)
{ // 将要被回收的分区插入空闲区(按首址大小从小到大插入)
if ((after == NULL ) || ((back1->adr <= after->adr) && (back1->adr >= before->adr)))
{
before->next = back1;
back1->next = after;
insert = 1;
}
else
{
before = before->next;
after = after->next;
}
}
if (insert)
{
if (back1->adr == before->adr + before->size)
{ // 和前边分区合并
before->size += back1->size;
before->next = back1->next;
free(back1);
}
else if (after&&back1->adr + back1->size == after->adr)
{ // 和后边分区合并
back1->size += after->size;
back1->next = after->next;
back1->id = after->id;
free(after);
after = back1;
}
printf( "\t 首先分配算法回收内存成功! \n" );
}
else
printf( "\t 首先分配算法回收内存失败! \n" );
}
Node * assignment2( int num , int req )
{
Node *before, *after, *ass, *q;
ass = ( Node *)malloc( sizeof ( Node ));
q = ( Node *)malloc( sizeof ( Node ));
before = head2;
after = head2->next;
ass->id = num ;
ass->size = req ;
while (after->size< req )
{
before = before->next;
after = after->next;
}
if (after == NULL )
{
ass->adr = -1;
}
else
{
if (after->size == req )
{
before->next = after->next;
ass->adr = after->adr;
}
else
{
q = after;
before->next = after->next;
ass->adr = q->adr;
q->size -= req ;
q->adr += req ;
before = head2;
after = head2->next;
if (after == NULL )
{
before->next = q;
q->next = NULL ;
}
else
{
while ((after->size)<(q->size))
{
before = before->next;
after = after->next;
}
before->next = q;
q->next = after;
}
}
}
return (ass);
}
void acceptment2( int address , int siz , int rd )
{
Node *before, *after;
int insert = 0;
back2 = ( Node *)malloc( sizeof ( Node ));
before = head2;
after = head2->next;
back2->adr = address ;
back2->size = siz ;
back2->id = rd ;
back2->next = NULL ;
if (head2->next == NULL )
{ // 空闲队列为空
head2->next = back2;
head2->size = back2->size;
}
else
{ // 空闲队列不为空
while (after)
{
if (back2->adr == after->adr + after->size)
{ // 和前边空闲分区合并
before->next = after->next;
after->size += back2->size;
back2 = after;
}
else
{
before = before->next;
after = after->next;
}
}
before = head2;
after = head2->next;
while (after)
{
if (after->adr == back2->adr + back2->size)
{ // 和后边空闲区合并
before->next = after->next;
back2->size += after->size;
}
else
{
before = before->next;
after = after->next;
}
}
before = head2;
after = head2->next;
while (!insert)
{ // 将被回收的块插入到恰当的位置(按分区大小从小到大)
if (after == NULL || ((after->size>back2->size) && (before->size<back2->size)))
{
before->next = back2;
back2->next = after;
insert = 1;
break ;
}
else
{
before = before->next;
after = after->next;
}
}
}
if (insert)
printf( "\t 最佳适应算法回收内存成功! \n" );
else
printf( "\t 最佳适应算法回收内存失败!! \n" );
}
void print( char choice ) // 输出空闲区队列信息
{
Node *p;
if ( choice == 'f' || choice == 'F' )
p = head1->next;
else
p = head2->next;
if (p)
{
printf( "\n 空闲区队列的情况为: \n" );
printf( "\t 编号 \t 首址 \t 终址 \t 大小 \n" );
while (p)
{
printf( "\t%d\t%d\t%d\t%d\n" , p->id, p->adr, p->adr + p->size - 1, p->size);
p = p->next;
}
}
}
void menu() // 菜单及主要过程
{
char chose;
int ch, num=0, r, add, rd;
while (1)
{
system( "cls" );
printf( "------- 存储管理动态分区分配及回收算法 -------\n" );
printf( " F 最先适应算法 \n" );
printf( " B 最佳适应算法 \n" );
printf( " E 退出程序 \n" );
printf( "----------------------------------------------\n" );
printf( " 请选择算法 :" );
cin >> chose;
//scanf("%c", &chose);
if (chose == 'e' || chose == 'E' )
exit(0);
else
{
system( "cls" );
while (1)
{
if (chose == 'f' || chose == 'F' )
printf( " 最先适应算法 :\n" );
if (chose == 'b' || chose == 'B' )
printf( " 最佳适应算法 :\n" );
printf( "----------------------------------------------\n" );
printf( " 1 分配内存 \n" );
printf( " 2 回收内存 \n" );
printf( " 3 查看内存 \n" );
printf( " 4 返回 \n" );
printf( "----------------------------------------------\n\n" );
printf( " 请选择 :" );
scanf( "%d" , &ch);
fflush( stdin );
switch (ch)
{
case 1:
printf( " 输入申请的分区大小: " ); scanf( "%d" , &r);
if (chose == 'f' || chose == 'F' )
assign = assignment1(num, r);
else
assign = assignment2(num, r);
if (assign->adr == -1)
{
printf( " 分配内存失败! \n" );
}
else
printf( " 分配成功!分配的内存的首址为 :%d\n" , assign->adr);
break ;
case 2:
printf( " 输入释放的内存的首址: " ); scanf( "%d" , &add);
printf( " 输入释放的内存的大小: " ); scanf( "%d" , &r);
printf( " 输入释放的内存的编号: " ); scanf( "%d" , &rd);
if (check(add, r, chose))
{
if (chose == 'f' || chose == 'F' )
acceptment1(add, r, rd);
else
acceptment2(add, r, rd);
}
break ;
case 3:print(chose); break ;
case 4:menu(); break ;
}
}
}
}
}
void main() // 主函数
{
init();
menu();
}
- 遇到的主要问题和解决方法
问题:当free的时候就去找那个数值,释放掉数值大小的堆空间,但是到底放哪呢?
解决方法:free只传一个指针,我们申请一块内存的时候:申请成功,系统将给我们是一个指针的地址,这个地址里面的某个地方存放着内存块的描述符。这样,free的时候系统会先读取这个描述符,返回再释放,所以能释放掉所有我们申请的内存
四、实验结果
五、实验总结
通过这次实验我学习了存储管理动态分区分配及回收算法,同时还巩固了c语言编程能力,感觉自己受益匪浅。