主存# includeostream首次自适应算法 hstructprogram { charname[30];longstartlonglengthstructprogram * next};结构空间{long
终极管理员 知识笔记 21阅读
开始;longlengthstructspace * next };void creat();void allot();void back();void回调(program * r);void排序(space * L);voidsort(程序);void显示(space * L);无效显示(程序);space * L;程序;intmain()
="color: rgba(0, 0, 0, 1)">{creat();
int a;
cout<<" 内存分配与回收模拟"<<endl;
cout<<"1:分配内存"<<endl;
cout<<"2:回收内存"<<endl;
cout<<"0:退出"<<endl;
while(1)
{
cout<<endl<<"请按键选择:";
cin>>a;
if(a>2||a<0)
{
cout<<endl<<"输入错误,请重新输入:";
continue;
}
switch(a)
{
case 1: allot(); break;
case 2: back(); break;
case 0: goto end;
}
}
end:
getchar();
getchar();
}
void creat()
{
L=new space;
space *p=new space;
p->start=0;
p->length=128;
p->next=NULL;
L->next=p;
S=new program;
S->next=NULL;
}
void allot()
{
program *q;
q=new program;
cout<<"请输入进程名和占用空间大小:"<<endl;
cin>>q->name>>q->length;
if(q->length<=0)
{
cout<<"进程空间大小错误."<<endl;
delete q;
return;
}
space *p,*r;
p=L;
r=p;
while(p->next!=NULL&&p->next->length<q->length)
{
r=p;
p=p->next;
}
if(p->next==NULL)
{
cout<<"占用空间过大,分配失败"<<endl;
delete q;
return;
}
else
{
q->start=p->next->start;
q->next=S->next;
S->next=q;
p->next->length-=q->length;
if(p->next->length!=0)
p->next->start+=q->length;
else
{
if(p->next->next!=NULL)
p->next=p->next->next;
else
{
r->next=NULL;
delete p->next;
}
}
}
display(L);
display(S);
}
void back()
{
char name[30];
cout<<"输入要回收的进程名:";
cin>>name;
program *p;
p=S;
while(p->next!=NULL)
{
if(strcmp(p->next->name, name)==0)
{
callback(p);
return;
}
p=p->next;
}
if(p->next==NULL)
cout<<"此进程不存在,内存回收失败"<<endl;
}
void callback(program *t)
{
program *r;
r=t->next;
space *p,*q;
long n;
n=r->length;
if(L->next==NULL)
{
space *w=new space;
w->start=0;
w->length=n;
w->next=NULL;
L->next=w;
t->next=r->next;
delete r;
cout<<"此进程内存回收完毕."<<endl;
display(L);
display(S);
return;
}
p=L->next;
while(p!=NULL&&p->start<r->start)
{
q=p;
p=p->next;
}
if((q->start+q->length==r->start)&&(r->start+n==p->start)) //上下均空
{
q->next=p->next;
q->length=q->length+p->length+n;
t->next=r->next;
delete r;
}
else if(r->start+n==p->start) //下邻空
{
p->start-=n;
p->length+=n;
t->next=r->next;
delete r;
}
else if(q->start+q->length==r->start)
{
q->length+=n;
t->next=r->next;
delete r;
}
else
{
space *sp=new space;
sp->start=r->start;
sp->length=n;
sp->next=L->next;
L->next=sp;
t->next=r->next;
delete r;
}
cout<<"此进程内存回收完毕."<<endl;
display(L);
display(S);
}
void display(space *L)
{
sort(L);
space *p=L->next;
cout<<endl<<"空闲区情况:"<<endl;
if(p==NULL)
{ cout<<"无空闲区了."<<endl; return;}
while(p!=NULL)
{
cout<<"起始地址:"<<p->start<<" 长度:"<<p->length<<endl;
p=p->next;
}
}
void display(program *S)
{
sort(S);
program *p=S->next;
cout<<endl<<"进程内存分配情况:"<<endl;
if(p==NULL)
{ cout<<"内存中无进程."<<endl; return;}
while(p!=NULL)
{
cout<<"进程名:"<<p->name<<" 起始地址:"<<p->start<<" 长度:"<<p->length<<endl;
p=p->next;
}
cout<<endl;
}
void sort(space *L) //链表排序
{
space *p=L->next, *q, *r;
if(p!=NULL)
{
r=p->next;
p->next=NULL;
p=r;
while(p!=NULL)
{
r=p->next;
q=L;
while(q->next!=NULL&&q->next->start<p->start)
q=q->next;
p->next=q->next;
q->next=p;
p=r;
}
}
}
void sort(program *S) //链表排序
{
program *p=S->next, *q, *r;
if(p!=NULL)
{
r=p->next;
p->next=NULL;
p=r;
while(p!=NULL)
{
r=p->next;
q=S;
while(q->next!=NULL&&q->next->start<p->start)
q=q->next;
p->next=q->next;
q->next=p;
p=r;
}
}
}
![主存# includeostream首次自适应算法 hstructprogram { charname[30];longstartlonglengthstructprogram * next};结构空间{long](https://www.feiniaomy.com/xzm_artimg/23091522347oz190044245_0.png)
标签: