实现分区存储管理的内存分配功能,选择适应算法(首次适应算法,最佳适应算法,最后适应算法,最坏适应算法)。
基本原理分析:
1) Best fit :将空闲分区按大小从小到大排序,从头找到大小合适的分区。
2) Worst fit:将空闲分区按大小从大到小排序,从头找到大小合适的分区。
3) First fit :将空闲分区按起始地址大小从小到大排序,……
4) Last fit :将空闲分区按起始地址大小从大到小排序,……
程序源代码:
程序源代码:
#include
#include
#include
#define UM_OUTPUT 1
#define UM_FIRSTFIT 2
#define UM_NEXTFIT 3
#define UM_BESTFIT 4
#define UM_WORSTFIT 5
#define dim(x) (sizeof(x)/sizeof(x[0]))
#include
#include
#define UM_OUTPUT 1
#define UM_FIRSTFIT 2
#define UM_NEXTFIT 3
#define UM_BESTFIT 4
#define UM_WORSTFIT 5
#define dim(x) (sizeof(x)/sizeof(x[0]))
class M_manage
{
private:
int *space;
int last;
int MaxSize;
int cursor;
int G;
Status free(int); //内存回收
public:
M_manage(int size=1);
~M_manage();
void push_back(int x);
void out_put(); //打印出内存中的空闲分区
void sort_ascending(); //对内存中的分区按从小到大进行排序
void sort_descending(); // 对内存中的分区按从大到小进行排序
void first_fit(int x_size);
void next_fit(int x_size);
void best_fit(int x_size);
void worst_fit(int x_size);
//void quick_fit(int x_size);
};
{
private:
int *space;
int last;
int MaxSize;
int cursor;
int G;
Status free(int); //内存回收
public:
M_manage(int size=1);
~M_manage();
void push_back(int x);
void out_put(); //打印出内存中的空闲分区
void sort_ascending(); //对内存中的分区按从小到大进行排序
void sort_descending(); // 对内存中的分区按从大到小进行排序
void first_fit(int x_size);
void next_fit(int x_size);
void best_fit(int x_size);
void worst_fit(int x_size);
//void quick_fit(int x_size);
};
void M_manage::worst_fit(int x_size)
{
sort_descending();
if(space[0]>=x_size)
{
space[0]-=x_size;
cout<< "内存分配成功" < }
else
{
cout<< "内存中没有足够空间可供分配" < }
}
{
sort_descending();
if(space[0]>=x_size)
{
space[0]-=x_size;
cout<< "内存分配成功" <
else
{
cout<< "内存中没有足够空间可供分配" <
}
void M_manage::best_fit(int x_size)
{
sort_ascending();
int i = 0;
while(i!=MaxSize && space[i] {
++i;
}
if(i==MaxSize)
{
cout<< "内存中没有足够空间可供分配" < }
else
{
space[i]-=x_size;
if(space[i]<=G)
{
space[i] = 0;
}
cout<<"内存分配成功"< }
}
{
sort_ascending();
int i = 0;
while(i!=MaxSize && space[i]
++i;
}
if(i==MaxSize)
{
cout<< "内存中没有足够空间可供分配" <
else
{
space[i]-=x_size;
if(space[i]<=G)
{
space[i] = 0;
}
cout<<"内存分配成功"<
}
void M_manage::next_fit(int x_size)
{
int i = cursor+1;
bool flag = true;
while( flag && space[i] {
++i;
if(i==MaxSize)
{
i = 0;
}
if(i == cursor+1)
{
flag = false;
}
}
if(flag)
{
cursor = i;
space[cursor] -= x_size;
cout<<"内存分配成功"< }
else
{
cout<< "内存中没有足够空间可供分配" < }
}
{
int i = cursor+1;
bool flag = true;
while( flag && space[i]
++i;
if(i==MaxSize)
{
i = 0;
}
if(i == cursor+1)
{
flag = false;
}
}
if(flag)
{
cursor = i;
space[cursor] -= x_size;
cout<<"内存分配成功"<
else
{
cout<< "内存中没有足够空间可供分配" <
}
void M_manage::first_fit(int x_size)
{
int i = 0;
while(i!=MaxSize && space[i] {
++i;
}
if(i==MaxSize)
{
cout<< "内存中没有足够空间可供分配" < }
else
{
space[i]-=x_size;
cout<<"内存分配成功"< }
}
{
int i = 0;
while(i!=MaxSize && space[i]
++i;
}
if(i==MaxSize)
{
cout<< "内存中没有足够空间可供分配" <
else
{
space[i]-=x_size;
cout<<"内存分配成功"<
}
void M_manage::sort_ascending()
{
int i, j;
int tem;
for(i=1; i!=MaxSize; ++i)
{
if(space[i]
tem = space[i];
j = i-1;
do
{
space[j+1] = space[j];
--j;
} while (j!=-1 && space[j]>tem);
space[j+1] = tem;
}
}
}
void M_manage::sort_descending()
{
int i, j;
int tem;
for(i=1; i!=MaxSize; ++i)
{
if(space[i]>space[i-1])
{
tem = space[i];
j = i-1;
do
{
space[j+1] = space[j];
--j;
} while (j!=-1 && space[j] space[j+1] = tem;
}
}
}
{
int i, j;
int tem;
for(i=1; i!=MaxSize; ++i)
{
if(space[i]>space[i-1])
{
tem = space[i];
j = i-1;
do
{
space[j+1] = space[j];
--j;
} while (j!=-1 && space[j]
}
}
}
void M_manage::out_put()
{
int count = 0;
for(int i=0; i!=MaxSize; ++i)
{
if(space[i]!=0)
{
cout< ++count;
}
}
if(count == 0)
{
cout<< "内存使用率为100%,没有空闲分区供分配";
}
cout<}
{
int count = 0;
for(int i=0; i!=MaxSize; ++i)
{
if(space[i]!=0)
{
cout<
}
}
if(count == 0)
{
cout<< "内存使用率为100%,没有空闲分区供分配";
}
cout<
void M_manage::push_back(int x)
{
space[++last] = x;
}
{
space[++last] = x;
}
M_manage::M_manage(int size)
{
space = new int[size];
MaxSize = size;
last = -1;
cursor = -1;
}
{
space = new int[size];
MaxSize = size;
last = -1;
cursor = -1;
}
M_manage::~M_manage()
{
delete []space;
last = -1;
MaxSize = 0;
cursor = -1;
G = 0;
}
{
delete []space;
last = -1;
MaxSize = 0;
cursor = -1;
G = 0;
}
//----------------------- 主 存 回 收 --------------------
Status free(int ID)
{
DuLNode *p=block_first;
while(p)
{
if(p->data.ID==ID)
{
p->data.state=Free;
p->data.ID=Free;
if(p->prior->data.state==Free)//与前面的空闲块相连
{
p->prior->data.size+=p->data.size;
p->prior->next=p->next;
p->next->prior=p->prior;
}
if(p->next->data.state==Free)//与后面的空闲块相连
{
p->data.size+=p->next->data.size;
p->next->next->prior=p;
p->next=p->next->next;
}
break;
}
p=p->next;
}
return OK;
}
/////////////////////////////////////////////////////////////////////
void print_M(M_manage* M)
{
(*M).out_put();
}
{
(*M).out_put();
}
void Mfirst_fit(M_manage* M)
{
int n_size;
cout<< "请输入作业大小:";
cin>>n_size;
while(n_size<=0)
{
cout<<"作业大小应为正,请重新分配:";
cin>>n_size;
}
(*M).first_fit(n_size);
}
{
int n_size;
cout<< "请输入作业大小:";
cin>>n_size;
while(n_size<=0)
{
cout<<"作业大小应为正,请重新分配:";
cin>>n_size;
}
(*M).first_fit(n_size);
}
void Mnext_fit(M_manage* M)
{
int n_size;
cout<< "请输入作业大小:";
cin>>n_size;
while(n_size<=0)
{
cout<<"作业大小应为正,请重新分配:";
cin>>n_size;
}
(*M).next_fit(n_size);
}
{
int n_size;
cout<< "请输入作业大小:";
cin>>n_size;
while(n_size<=0)
{
cout<<"作业大小应为正,请重新分配:";
cin>>n_size;
}
(*M).next_fit(n_size);
}
void Mbest_fit(M_manage* M)
{
int n_size;
cout<< "请输入作业大小:";
cin>>n_size;
while(n_size<=0)
{
cout<<"作业大小应为正,请重新分配:";
cin>>n_size;
}
(*M).best_fit(n_size);
}
{
int n_size;
cout<< "请输入作业大小:";
cin>>n_size;
while(n_size<=0)
{
cout<<"作业大小应为正,请重新分配:";
cin>>n_size;
}
(*M).best_fit(n_size);
}
void Mworst_fit(M_manage* M)
{
int n_size;
cout<< "请输入作业大小:";
cin>>n_size;
while(n_size<=0)
{
cout<<"作业大小应为正,请重新分配:";
cin>>n_size;
}
(*M).worst_fit(n_size);
}
{
int n_size;
cout<< "请输入作业大小:";
cin>>n_size;
while(n_size<=0)
{
cout<<"作业大小应为正,请重新分配:";
cin>>n_size;
}
(*M).worst_fit(n_size);
}
/////////////////////////////////////////////////////////////////////
struct MSGMAP_ENTRY
{
int nMessage;
void(*pfn)(M_manage* M);
};
MSGMAP_ENTRY _messageEntres[]=
{
UM_OUTPUT, print_M,
UM_FIRSTFIT, Mfirst_fit,
UM_NEXTFIT, Mnext_fit,
UM_BESTFIT, Mbest_fit,
UM_WORSTFIT, Mworst_fit
};
{
UM_OUTPUT, print_M,
UM_FIRSTFIT, Mfirst_fit,
UM_NEXTFIT, Mnext_fit,
UM_BESTFIT, Mbest_fit,
UM_WORSTFIT, Mworst_fit
};
////////////////////////////////////////////////////////////////////
void main()
{
M_manage *my_M;
int select=1;
int n=0;
cout<<"输入内存空闲分区个数:";
cin>>n;
while(n<=0)
{
cout<< "内存空闲分区个数应为正数,请重新输入:";
cin>>n;
}
my_M = new M_manage(n);
for(int i=0; i!=n; ++i)
{
int s=0;
cout<<"输入第"< cin>>s;
while(s<=0)
{
cout<< "分区大小应为正,请重新输入" < cout<<"输入第"< cin>>s;
}
(*my_M).push_back(s);
}
while(select!=0)
{
void main()
{
M_manage *my_M;
int select=1;
int n=0;
cout<<"输入内存空闲分区个数:";
cin>>n;
while(n<=0)
{
cout<< "内存空闲分区个数应为正数,请重新输入:";
cin>>n;
}
my_M = new M_manage(n);
for(int i=0; i!=n; ++i)
{
int s=0;
cout<<"输入第"< cin>>s;
while(s<=0)
{
cout<< "分区大小应为正,请重新输入" <
}
(*my_M).push_back(s);
}
while(select!=0)
{
cout<<" "< cout<<" 1.显示空闲分区 "< cout<<" 2.首次适应算法 "< cout<<" 3.循环首次适应算法 "< cout<<" 4.最佳适应算法 "< cout<<" 5.最坏适应算法 "< cout<<" 0.退出 "<
cout<<"请选择分区分配算法:";
cin>>select;
for(int i=0; i {
if((_messageEntres[i]).nMessage == select)
{
(*_messageEntres[i].pfn)(my_M);
}
}
}
}
cin>>select;
for(int i=0; i
if((_messageEntres[i]).nMessage == select)
{
(*_messageEntres[i].pfn)(my_M);
}
}
}
}