#include "iostream"
#include "stdio.h"
using namespace std;
//存储结构
typedef struct MCB
{
int jno;//作业号
int addr;//地址
int size;//大小
struct MCB *next;
}MCB,*pMCB;
const pMCB ERROR=NULL;
/////////////////////////////////函数声明///////////////////////////////////////////////
pMCB Create_mcb();
pMCB CreateAvaillist();
pMCB GetAvailMCB ( pMCB newmcb , pMCB availlist );
void InsertAvailList ( pMCB mcb , pMCB availlist , pMCB busylist );
pMCB CreateBusylist ();
pMCB Findmcb( pMCB busylist );
pMCB InsertBusyList ( pMCB newmcb , pMCB busy );
void Display( pMCB busylist , pMCB availlist );
///////////////////////////////////主函数部分////////////////////////////////////////////
void main()
{
cout<<"\t******************************************"<<endl;
cout<<"\t*** 欢迎使用本系统进分段式存储管理! ***"<<endl;
cout<<"\t*** ——学号:20051054147 ***"<<endl;
cout<<"\t*** ——软件工程:李博 ***"<<endl;
cout<<"\t*** ——时间: 2007.11 ***"<<endl;
cout<<"\t******************************************"<<endl;
pMCB busylist = CreateBusylist();
pMCB availlist = CreateAvaillist();
pMCB newmcb = new MCB();
pMCB mcb = new MCB();
int select;
do
{
cout<<endl<<"请选择您想要的操作:"<<endl;
cout<<"1.分配存储空间"<<endl;
cout<<"2.回收存储空间"<<endl;
cout<<"3.显示存储资源状况"<<endl;
cout<<"4.退出"<<endl;
cin>>select;
switch( select )
{
case 1:
newmcb = Create_mcb( busylist );
if ( GetAvailMCB ( newmcb , availlist ) == ERROR ) break;
InsertBusyList ( newmcb , busylist );
break;
case 2:
mcb = Findmcb( busylist );
InsertAvailList ( mcb , availlist , busylist );
break;
case 3:
Display( busylist , availlist );
break;
case 4:
break;
default:
cout<<"输入错误!请重新输入!"<<endl;
break;
}
}while(select!=4);
}
//////////////////////////////////////用户自定义函数部分///////////////////////////////////
//将取出的作业放入空闲链表
void InsertAvailList ( pMCB mcb , pMCB availlist , pMCB busylist )
{
//在busylist链表中去掉mcb
pMCB p1 = busylist->next;
pMCB q1 = busylist;
while ( mcb->jno != p1->jno )
{
q1 = p1;
p1 = p1->next;
}
q1->next = p1->next;
//在availlist的合适位置插入mcb (首次适应法)
pMCB p = availlist->next;
pMCB q = availlist;
if( mcb->addr <= p->addr )
{
mcb->next = p;
q->next = mcb;
if( mcb->addr + mcb->size == p->addr )
{
mcb->size += p->size;
mcb->next = p->next;
}
}
else
{
q = p;
p = p->next;
while( p!=NULL )
{
if( mcb->addr <= p->addr )
{
mcb->next = p;
q->next = mcb;
if( mcb->addr + mcb->size == p->addr )
{
mcb->size += p->size;
mcb->next = p->next;
}
if( q->addr + q->size == mcb->addr )
{
q->size += mcb->size;
q->next = mcb->next;
}
}
else
{
q=p;
p=p->next;
}
}
}
}
//查询作业
pMCB Findmcb( pMCB busylist )
{
int work;
cout<<"请输入要收回的作业:";
cin>>work;
pMCB p = busylist->next;
while( p != NULL )
{
if ( p->jno == work )
{
return p;
}
p = p->next;
}
cout<<"!!!!!!!!!!!!!!!!!!!!!!!!没有您要查找的作业!!!!!!!!!!!!!!!!!!!!!!!!";
return ERROR;
}
//创建空闲链表
pMCB CreateAvaillist ()
{
pMCB availlist = new MCB();
pMCB avail = new MCB();
availlist->next = avail;
avail->addr = 0;
avail->size = 1024;
avail->next = NULL;
return availlist;
}
//在空闲链表中为作业寻找空闲区
pMCB GetAvailMCB ( pMCB newmcb , pMCB availlist )
{
pMCB p = availlist->next;
pMCB q = availlist;
while ( p!=NULL )
{
if ( p->size >= newmcb->size )
{
p->size -= newmcb->size;
newmcb->addr = p->addr;
p->addr += newmcb->size;
if ( p->size == 0 )
{
q->next = p->next;
}
return newmcb;
}
q = p;
p = p->next;
}
cout<<endl<<"!!!!!!!!!!!!!!!!没有满足要求的空闲空间!!!!!!!!!!!!!!!!"<<endl;
return ERROR;
}
//创建占用链表
pMCB CreateBusylist ()
{
pMCB busy = new MCB();
busy->next = NULL;
return busy;
}
//创建作业
pMCB Create_mcb( pMCB busylist )
{
pMCB newmcb = new MCB();
cout<<"请按照要求的顺序输入所创建分区的数据:"<<endl;
cout<<"作业编号(整型):";
cin>>newmcb->jno;
cout<<"作业大小(整型):";
cin>>newmcb->size;
newmcb->next = NULL;
return newmcb;
}
//将新作业插入占用链表
pMCB InsertBusyList ( pMCB newmcb , pMCB busylist )
{
pMCB p = busylist;
pMCB q = busylist;
int flag=1;
while ( flag )
{
while( p != NULL )
{
if( p->jno != newmcb->jno )
{
p = p->next;
}
else
{
cout<<"!!!!!!!!!!!!!!!!!!!!!!!作业名重复!!!!!!!!!!!!!!!!!!!"<<endl;
cout<<"!!!!!!!!!!!!!!!!!当前该作业已分配存储区!!!!!!!!!!!!!"<<endl;
flag=0;
return ERROR;
}
}
flag=0;
}
p = busylist;
while( p != NULL )
{
q = p;
p = p->next;
}
q->next = newmcb ;
return busylist;
}
//显示
void Display( pMCB busylist , pMCB availlist )
{
cout<<endl<<"******************************内存资源情况***************************"<<endl;
cout<<"占用存储空间:"<<endl;
pMCB p = busylist->next;
while( p != NULL )
{
cout<<" 作业号:"<< p->jno <<" 作业大小:"<< p->size <<endl;
p = p->next;
}
cout<<endl;
cout<<"空闲存储空间:"<<endl;
pMCB q = availlist->next;
while( q !=NULL )
{
cout<<" 空闲区地址:"<< q->addr <<" 大小"<< q->size <<endl;
q = q->next;
}
cout<<"*********************************************************************"<<endl;
}