include
include<stdio.h>
include<stdlib.h>
include<windows.h>
using namespace std;
typedef struct freetable
{
long start; //空闲表起始地址
long length; //空闲长度
long state; //空闲块状态
struct freetable *next; //下一个空闲区的表头
}freetable;
freetable flist=NULL,blist=NULL; //有两个表,一个表空闲表,一个分配表
long nfree=0; //用顺序的任务号来标记分配表的头
int initializtion(int i); //初始化链表,i=1时为空闲区,i=2时分配区
int showlist(int i); //查看表,i=1时为空闲区,i=2时分配区
int sortlist(int i); //排序空闲区表,1最先适应算法按照起始地址递增,2最佳适应算法按分区从小到大,3最坏适应算法按分区从大到小
int firstbest(long j,long cnum,long csize); //j:1最先适应算法,2最佳适应算法
int worstfit(long cnum,long csize); //最坏适应算法
int recover(long cnum); //回收功能
int menu(); //菜单
int check(int i,freetable *cc);//检查添加是否有重复
int check(int i,freetable cc)//检查添加是否有重复
{
freetable nn,*p; //使用两个指针来循环判断。
long caddress=cc->start,ccsize=cc->length,cstate=cc->state;//令c来接受传进来的参数
long plast,clast=caddress+ccsize; //clast为此块的结尾地址
nn=blist; //nn为分配表的表头
p=nn->next; //p为分配表第二个值
while(p!=NULL)
{
plast=p->start+p->length; //尾地址
if ( !( (p->start>caddress&&plast>clast)||(p->start<caddress&&plast<clast) ) ) return 0;
if(i==2) if (p->state==cstate) return 0;//重复了
p=p->next;
}
nn=flist;
p=nn->next; //空闲表表头
while (p!=NULL)
{
plast=p->start+p->length;
if ( !( (p->start>caddress&&plast>clast)||(p->start<caddress&&plast<clast) ) ) return 0;
p=p->next;
}
return 1; //没有重复
}
int initializtion(int i) //初始化,1空闲区,2分配区
{
freetable nn,p;
int num,f=0,k;
do
{
if (i==1)
{
cout<<"请输入预先设置的空闲区个数:";
p=flist;
}else
{
cout<<"请输入已分配区分配个数:";
p=blist;
}
cin>>num;
k=0;
if (num>0) f=num;
}while (f<=0);
if (f)
{
if (i==1) cout<<"请依次输入空闲区始址、长度:"<<endl;
else cout<<"请依次输入已分配区始址、长度、进程编号:"<<endl;
}
while (num--)
{
k++;
nn=(freetable )malloc(sizeof(freetable ));
scanf("%ld%ld",&nn->start,&nn->length);
if (i==2) scanf("%ld",&nn->state);else nn->state=++nfree;
if (!check(i,nn))
{
cout<<"第"<<k<<"个输入有重复,添加失败"<<endl;
}else{nn->next=p->next;p->next=nn;}//因为第一个是哨兵节点
}
nn=NULL;
cout<<"end"<<endl;
return 0;
}
int showlist(int i)//查看表
{
freetable nn;
if(i==1)
{
nn=flist->next;
cout<<"空闲区"<<endl;
}else if(i==2)
{
nn=blist->next;
cout<<"分配区"<<endl;
}
cout<<" 标志 始址 长度"<<endl;
while(nn)
{
printf("%10ld %12ld %12ld\n",nn->state,nn->start,nn->length);
nn=nn->next;
}
nn=NULL;
cout<<"end"<<endl;
return 0;
}
int sortlist(int i)//排序空闲区表,1最先适应算法按照起始地址递增,2最佳适应算法按分区从小到大,3最坏适应算法按分区从大到小
{//链表所以选择排序
if (flist==NULL||flist->next==NULL) return 0;//表不能为空
freetable change,last;
freetable newhead,newlast=NULL;
newhead=(freetable *)malloc(sizeof(freetable ));newhead->next=NULL;
while(flist->next!=NULL)
{
change=flist;
last=flist->next;
if (last!=NULL)//说明至少有一个,last->next才有意义
{
while(last->next!=NULL)
{
if (i==1)
{
if (change->next->start < last->next->start) change=last;
}else if (i==2)
{
if (change->next->length < last->next->length) change=last;
}else if (i==3)
{
if (change->next->length > last->next->length) change=last;
}
last=last->next;
}
}
last=change->next;
change->next=last->next;
last->next=newhead->next;
newhead->next=last;
}
free(flist);
flist=newhead;
newhead=newlast=last=change=NULL;
return 0;
}
int menu()//菜单选择
{
flist=(freetable )malloc(sizeof(freetable)); flist->next=NULL;//哨兵节点
blist=(freetable )malloc(sizeof(freetable)); blist->next=NULL;
int i=1,j=1;
while(i)
{
system("reset");
cout<<"-------------主菜单--------------"<<endl;
cout<<"0退出程序"<<endl<<"1添加空闲区/已分配表"<<endl<<"2查看空闲区/已分配表"<<endl<<"3分配功能"<<endl<<"4回收功能"<<endl;
cout<<"您的选择:";
cin>>i;j=1;
system("reset");
if (i==0)
{
cout<<"谢谢使用"<<endl;
break;
}else if (i==1)
{
while(j)
{
cout<<"请选择添加"<<endl<<"0退出当前"<<endl<<"1空闲区"<<endl<<"2已分配表"<<endl;
cout<<"您的选择:";
cin>>j;
system("reset");
if (j==0) break;
else if (j==1||j==2) {initializtion(j);showlist(j);}
else cout<<"输入非法,请重新输入"<<endl;
}
}else if (i==2)
{
while(j)
{
cout<<"请选择查看"<<endl<<"0退出当前"<<endl<<"1空闲区"<<endl<<"2已分配表"<<endl;
cout<<"您的选择:";
cin>>j;
system("reset");
if (j==0) break;
else if (j==1||j==2) showlist(j);
else cout<<"输入非法,请重新输入"<<endl;
}
}else if (i==3)
{
long cnum,csize;
while(j)
{
cout<<"请选择算法"<<endl<<"0退出当前"<<endl<<"1最先适应算法"<<endl<<"2最佳适应算法"<<endl<<"3最坏适应算法"<<endl;
cout<<"您的选择:";
cin>>j;
system("reset");
if (j==0) break;
if (j!=1&&j!=2&&j!=3)
{
cout<<"输入非法,请重新输入"<<endl;
continue;
}
showlist(1);showlist(2);
cout<<"请输入需要分配的进程的进程号、需内存大小"<<endl;
cin>>cnum>>csize;
if (j==3) worstfit(cnum,csize);//最坏适应算法
else firstbest(j,cnum,csize);//最先适应算法,最佳适应算法
}
}else if (i==4)
{
long cnum;
while(j)
{
system("reset");
cout<<"请输入需要回收的进程的进程号"<<endl;
cin>>cnum;
recover(cnum);
cout<<"您的选择:0退出,1继续(不为0的数)";
cin>>j;
}
}else cout<<"输入非法,请重新输入"<<endl;
}
return 0;
}
int firstbest(long j,long cnum,long csize)//j:1最先适应算法,2最佳适应算法
{
sortlist(j);//j:1最先适应算法按照起始地址递增,2最佳适应算法按分区从小到大
freetable head=flist,nn;
while(head->next!=NULL)
{
if (head->next->length >= csize)
{
if (head->next->length==csize)
{
nn=head->next;
nn->state=cnum;
head->next=nn->next;
nn->next=blist->next;
blist->next=nn;
}else
{
nn=(freetable *)malloc(sizeof(freetable ));
nn->start=head->next->start;
nn->state=cnum;
nn->length=csize;
head->next->length-=csize;
head->next->start+=csize;
nn->next=blist->next;
blist->next=nn;
}
csize=-1;
break;
}
head=head->next;
}
if (csize==-1) cout<<"分配成功"<<endl;
else {cout<<"分配失败"<<endl;return 0;}
showlist(1);showlist(2);
return 0;
}
int worstfit(long cnum,long csize)//最坏适应算法
{
sortlist(3);//3最坏适应算法按分区从大到小
freetable nn;
if (flist->next!=NULL && flist->next->length >=csize)
{
if (flist->next->length==csize)
{
nn=flist->next;
nn->state=cnum;
flist->next=nn->next;
nn->next=blist->next;
blist->next=nn;
}else
{
nn=(freetable )malloc(sizeof(freetable ));
nn->start=flist->next->start;
nn->state=cnum;
nn->length=csize;
flist->next->length-=csize;
flist->next->start+=csize;
nn->next=blist->next;
blist->next=nn;
}
cout<<"分配成功"<<endl;
showlist(1);showlist(2);
}else {cout<<"分配失败"<<endl;return 0;}
return 0;
}
int recover(long cnum)//回收功能
{
freetable busyhead=blist,freehead=flist,*nn;
if (busyhead->next==NULL)
{
cout<<"无需回收资源"<<endl;
return 0;
}
while(busyhead->next!=NULL)
{
if (busyhead->next->state==cnum)//全添加到freelist
{
nn=busyhead->next;
busyhead->next=nn->next;
nn->next=freehead->next;
freehead->next=nn;
}else busyhead=busyhead->next;
}
sortlist(1);//1按照起始地址递增空闲区
freehead=flist;
freehead=freehead->next;
if (freehead==NULL)
{
cout<<"无空闲资源"<<endl;
return 0;
}
while(freehead->next!=NULL)//至少有下一个
{
if ((freehead->length+freehead->start) == freehead->next->start)//合并
{
nn=freehead->next;
freehead->length=freehead->length+nn->length;
freehead->next=nn->next;
nn->next=NULL;
}else freehead=freehead->next;
}
showlist(1);showlist(2);
return 0;
}
int main()
{
menu();
return 0;
}