第1个回答 2010-04-18
#include <iostream.h>
#include <stdio.h>
#include <string.h>
struct program
{
char name[30];
long start;
long length;
struct program *next;
};
struct space
{
long start;
long length;
struct space *next;
};
void creat();
void allot();
void back();
void callback(program *r);
void sort(space *L);
void sort(program *S);
void display(space *L);
void display(program *S);
space *L;
program *S;
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;
}
}
}
void main()
{
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();
}
#include "iostream.h"
#include "iomanip.h"
#define ERR_NOFREEAREA 1
#define ERR_NOADEQUACYAREA 2
#define ERR_ALLOCATED 4
#define ERR_NOJOBS 1
#define ERR_NOSUCHJOB 2
#define ERR_RECLAIMED 4
typedef struct tagUsedNode
{
long address;
long length;
int flag; //作业名
struct tagUsedNode *next;
} USED_AREA , *USED_TABLE;
typedef struct tagFreeNode
{
long address;
long length;
struct tagFreeNode *next;
} FREE_AREA , *FREE_TABLE;
//空闲区、作业区链表
USED_TABLE usedTable = NULL;
FREE_TABLE freeTable = NULL;
//给作业分配空间
//jobname: 作业名
//jobsize: 作业所需空间大小
int Allocate( int jobname , long jobsize )
{
//如果没有空闲区
if( freeTable == NULL )
return ERR_NOFREEAREA;
FREE_TABLE p = freeTable;
FREE_TABLE q = p;
//找首次适应空闲区
while( p != NULL && p->length < jobsize )
{
q = p;
p = p->next;
}
//如果找不到有足够空间的分区
if( p == NULL )
return ERR_NOADEQUACYAREA;
USED_TABLE x = new USED_AREA;
x->address = p->address;
x->length = jobsize;
x->flag = jobname;
x->next = NULL;
//如果该分区大于作业需求,空间大小减去作业大小
if( p->length > jobsize )
{
p->length -= jobsize;
p->address += jobsize;
}
//如果该分区等于作业大小,删除该分区
else
{
if( p == freeTable )
freeTable = NULL;
else
q->next = p->next;
delete p;
}
//作业加入“作业表”中
USED_TABLE r = usedTable;
USED_TABLE t = r;
while( r != NULL && r->address < x->address )
{
t = r;
r = r->next;
}
if( usedTable == NULL )
usedTable = x;
else
{
x->next = r;
t->next = x;
}
return ERR_ALLOCATED;
}
//回收作业空间
//jobname: 作业名
int Reclaim( int jobname )
{
if( usedTable == NULL )
return ERR_NOJOBS;
USED_TABLE p = usedTable;
USED_TABLE q = p;
while( p != NULL && p->flag != jobname )
{
q = p;
p = p->next;
}
//如果没有该作业
if( p == NULL )
return ERR_NOSUCHJOB;
//回收后的空间加入到空闲区
FREE_TABLE r = freeTable;
FREE_TABLE t = r;
FREE_TABLE x;
while( r != NULL && r->address < p->address )
{
t = r;
r = r->next;
}
x = new FREE_AREA;
x->address = p->address;
x->length = p->length;
x->next = NULL;
if( r == freeTable )
{
x->next = r;
freeTable = x;
t = freeTable;
}
else
{
x->next = r;
t->next = x;
}
//合并分区
while( t->next != NULL && t->address + t->length == t->next->address )
{
t->length += t->next->length;
r = t->next;
t->next = t->next->next;
delete r;
}
//删除该作业
if( p == usedTable )
{
usedTable = usedTable->next;
}
else
q->next = p->next;
delete p;
return ERR_RECLAIMED;
}
int Init()
{
freeTable = new FREE_AREA;
freeTable->address = 0;
freeTable->length = 1024;
freeTable->next = NULL;
return 1;
}
void jobrequest()
{
int jobname;
int jobsize;
cout<<"...................."<<endl;
cout<<"作业名: ";
cin >> jobname;
cout<<"作业长度: ";
cin >> jobsize;
if( Allocate( jobname , jobsize ) == ERR_ALLOCATED )
cout<<"该作业已成功获得所需空间"<<endl;
else
cout<<"该作业没有获得所需空间"<<endl;
cout<<"...................."<<endl;
}
void jobreclaim()
{
int jobname;
cout<<"...................."<<endl;
cout<<"作业名: ";
cin >>jobname;
int result = Reclaim( jobname );
if( result == ERR_RECLAIMED )
cout<<"该作业已成功回收"<<endl;
else if( result == ERR_NOSUCHJOB || result == ERR_NOJOBS )
cout<<"系统没有作业或该作业不存在"<<endl;
cout<<"...................."<<endl;
}
void freeTablePrint()
{
cout<<"........................................"<<endl;
cout<<setw(10)<<"address"<<setw(10)<<"length"<<setw(10)<<"state"<<endl<<endl;
FREE_TABLE p = freeTable;
USED_TABLE q = usedTable;
int x , y;
while( p || q )
{
if( p )
x = p->address;
else
x = 0x7fffffff;
if( q )
y = q->address;
else
y = 0x7fffffff;
if( x < y )
{
cout<<setw(10)<<p->address<<setw(10)<<p->length<<setw(10)<<"空闲"<<endl;
p = p->next;
}
if( x > y )
{
cout<<setw(10)<<q->address<<setw(10)<<q->length<<setw(10)<<"已分配"<<setw(10)<<"ID="<<q->flag<<endl;
q = q->next;
}
}
cout<<"........................................"<<endl;
}
void main()
{
Init();
int choose;
bool exitFlag = false;
while( !exitFlag )
{
cout<<"选择功能项 ( 0 - 退出 1 - 分配主存 2 - 回收主存 3 - 显示主存 )"<<endl;
cout<<"?>";
cin>>choose;
switch( choose )
{
case 0:
exitFlag = true;
break;
case 1:
jobrequest();
break;
case 2:
jobreclaim();
break;
case 3:
freeTablePrint();
break;
}
}
}本回答被网友采纳