今天我完成了操作系统课程设计,实现可变分区管理,使用C++语言编写程序的代码如下:
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include<iostream.h>
#define MAPSIZE 100
int N=0;
int job[10][2];
struct map //存储资源表结构
{
int m_addr;//存储空闲首地址
int m_size;//存储空闲分区大小
};
struct map map[MAPSIZE]; //存储资源表结构体数组
int BF_malloc(struct map *mp,int size)
{
register int a,s;
register struct map *bp,*bpp;
for(bp=mp;bp->m_size;bp++)
{
if(bp->m_size>=size)//SIZE是指作业大小
{ //最佳适应
a=bp->m_addr;
s=bp->m_size;
for(bpp=bp;bpp->m_size;bpp++)
{
if(bpp->m_size>=size&&bpp->m_size<s)//判断内存分区是否大于作业大小,并且从分区中找出m_size最小的节点
{
a=bpp->m_addr;
s=bpp->m_size;
bp=bpp;
}
}
bp->m_addr+=size;
if((bp->m_size-=size)==0)
do
{
bp++;
(bp-1)->m_addr=bp->m_addr;
}
while((bp-1)->m_size=bp->m_size);
return(a);
}
}
return -1;
}
int WF_malloc(struct map *mp,int size)//WF存储分配函数
{
register int a,s;
register struct map *bp,*bpp;
for (bp=mp;bp->m_size;bp++)
{
if(bp->m_size>=size)//从分区中找出m_size最大的分区进行分配
{
a=bp->m_addr;
s=bp->m_size;
for(bpp=bp;bpp->m_size;bpp++)
{ //最坏适应
if(bpp->m_size>s)
{
a=bpp->m_addr;
s=bpp->m_size;
bp=bpp;
}
}
bp->m_addr+=size;
if((bp->m_size-=size)==0)
do
{
bp++;
(bp-1)->m_addr=bp->m_addr;
}
while((bp-1)->m_size=bp->m_size);
return(a);
}
}
return(-1);
}
void mfree(struct map *mp,int aa,int size)//存储释放函数
{
register struct map *bp;
register int t;
register int a;
a=aa;
for(bp=mp;bp->m_addr<=a&&bp->m_size!=0;bp++);
if(bp>mp&&(bp-1)->m_addr+(bp-1)->m_size==a)
{//与前合并
(bp-1)->m_size+=size;
if(a+size==bp->m_addr)
{//前后合并
(bp-1)->m_size+=bp->m_size;
while (bp->m_size)
{
bp++;
(bp-1)->m_addr=bp->m_addr;
(bp-1)->m_size=bp->m_size;
}
}
}
else
{
if(a+size==bp->m_addr&&bp->m_size)
{//与后合并
bp->m_addr-=size;
bp->m_size+=size;
}
else if (size)
do
{//无合并
t=bp->m_addr;
bp->m_addr=a;
a=t;
t=bp->m_size;
bp->m_size=size;
bp++;
}
while (size=t);
}
}
void dele(int job[10][2],int a,int s) //从作业内存中将已回收内存删除
{
for(int i=0;i<N;i++)
{
if(job[i][0]==a && job[i][1]==s)
{
N=N-1;
for(int j=i;j<N;j++)
{
job[j][0]=job[j+1][0];
job[j][1]=job[j+1][1];
}
}
}
}
void init()//初始化
{
struct map *bp;
int addr,size;
int i=0;
bp=map;
printf("please input starting addr and total size:");
scanf("%d,%d",&addr,&size);//输入内存的首地址和大小
bp->m_addr=addr;//将首地址付给节点
bp->m_size=size;//将大小付给节点
(++bp)->m_size=0; //表尾
}
void show_map()//显示空闲内存
{
int i=0;
struct map *bp;
bp=map;
printf("Current memory map.../n");
printf("Address Size/n");
while(bp->m_size!=0)
{
printf("<%d/t/t%d>/n",bp->m_addr,bp->m_size);
bp++;
}
}
void moveFragment(struct map *mp) //碎片拼接
{ int sum=0;
struct map *bp;
bp=mp;
sum=0;
int addr=bp->m_addr;
for(;bp->m_size;bp++)
sum=sum+bp->m_size;
mp->m_addr=addr;
mp->m_size=sum;
(++mp)->m_size=0;//尾表
}
void main()
{
int a,s;
int job[10][2];
char c;
int i,j=0;
printf("--------------可变分区存储管理--------------/n1--------------初始化/n2--------------内存分配/n3--------------显示作业内存分配/n4--------------显示空闲内存/n5--------------内存回收/n6--------------碎片拼节/n7--------------退出/n");
do
{ printf( "please select a choice:");
scanf("%d",&i);
switch(i)
{
case 1: init(); break;N=0;
case 2:
printf("please input ,b for BF,w for WF:");
cin>>c;
printf("please input size:");
scanf("%d",&s);
if(c=='b')
a=BF_malloc(map,s);
else
a=WF_malloc(map,s);
if (a==-1)
printf("request cant't be satisfied/n");
else
printf("alloc memory at address:%d,size:%d/n",a,s);
N++;
job[j][0]=a;
job[j][1]=s;
j++;
break;
case 3:
printf("Show the jobs list:/n");
printf("Address Size/n");
for(j=0;j<N;j++)
printf("%3d %8d/n",job[j][0],job[j][1]);
break;
case 4:
show_map();
break;
case 5:
printf("Please input addr and size:");
scanf("%d,%d",&a,&s);
dele(job,a,s);
mfree(map,a,s);
break;
case 6:
moveFragment(map);
break;
case 7:
exit(0);
break;
}
i=getchar();
}
while(1);
}
欢迎大家观看和参与评论.