可变分区储存管理doc.docx
《可变分区储存管理doc.docx》由会员分享,可在线阅读,更多相关《可变分区储存管理doc.docx(13页珍藏版)》请在冰豆网上搜索。
![可变分区储存管理doc.docx](https://file1.bdocx.com/fileroot1/2023-5/29/dda8c856-d904-4277-a615-a7ff4b5d2d4a/dda8c856-d904-4277-a615-a7ff4b5d2d4a1.gif)
可变分区储存管理doc
#include
#include
#include
#definemax100
typedefstructnode
{
intstart;
intlength;
chartag[20];
}job;
jobfrees[max];
joboccupys[max];
intfree_quantity,occupy_quantity;
intSIZE;
voidinitial()//初始化函数
{
inti;
for(i=0;i{
frees[i].start=-1;
frees[i].length=0;
strcpy(frees[i].tag,"free");
occupys[i].start=-1;
occupys[i].length=0;
strcpy(occupys[i].tag,"");
}
free_quantity=0;
occupy_quantity=0;
}
voidwritedata()//把分区函数写入磁盘文件
{
FILE*fp;
charfname[20];
inti,j;
printf("请输入初始空闲表文件名:
\n");
scanf("%s",&fname);
printf("现在进行初始化空闲分区!
\n");
printf("请输入您要建立的空闲分区数:
\n");
scanf("%d",&SIZE);
for(i=0;i{
j=i+1;
printf("输入第%d个分区的起始地址:
\n",j);
scanf("%d",&frees[i].start);
printf("输入第%d个分区的长度:
\n",j);
scanf("%d",&frees[i].length);
}
if((fp=fopen(fname,"wb"))==NULL)
printf("错误,文件打不开,请检查文件名\n");
for(i=0;iif(fwrite(&frees[i],sizeof(structnode),1,fp)!
=1)
printf("文件写入错误!
\n");
fclose(fp);
}
voidreaddata()//从文件读入分区表函数
{
FILE*fp;
charfname[20];
printf("请输入读入空闲表文件名:
");//输入空闲表文件的文件名
scanf("%s",&fname);
if((fp=fopen(fname,"rb"))==NULL)
{
printf("错误,文件打不开,请检查文件名\n");
exit(0);
}
else
{
for(inti=0;i{
fread(&frees[i],sizeof(structnode),1,fp);
free_quantity++;
}
}
fclose(fp);
}
voidsort()//排序空闲表
{
for(inti=0;i{
intt=i;
for(intj=i+1;j{
if(frees[j].start<=frees[t].start)
t=j;
}
frees[free_quantity].start=frees[i].start;
frees[free_quantity].length=frees[i].length;
frees[i].start=frees[t].start;
frees[i].length=frees[t].length;
frees[t].start=frees[free_quantity].start;
frees[t].length=frees[free_quantity].length;
}
}
voidview()//显示分区信息
{
printf("空闲分区表显示如下:
\n");
printf("起始地址\t长度\t状态标志\n");
for(inti=0;iprintf("%6dk\t%10dk\t%s\t\n",frees[i].start,frees[i].length,frees[i].tag);
printf("\n\n已分配分区表显示如下:
\n");
printf("起始地址\t长度\t占用作业名\n");
for(intj=0;jprintf("%6dk\t%10dk\t%s\t\n",occupys[j].start,occupys[j].length,occupys[j].tag);
getchar();
getchar();
}
voidearliest()//首次适应算法
{
charjobname[20];
intjoblength,f=0;
inti,j;
printf("请输入作业名:
\n");
scanf("%s",&jobname);
printf("输入作业的长度:
\n");
scanf("%d",&joblength);
for(i=0;i{
if(frees[i].length>=joblength)
f=1;
}
if(f==0)
{
printf("\n当前没有能满足你申请长度的空闲内存,请稍候再试\n");
getchar();
}
else
{//找到了满足的空间
intt=0;
j=0;
while(t==0)
{
if(frees[j].length>=joblength)
{
t=1;
}
j++;
}
j--;
occupys[occupy_quantity].start=frees[j].start;//分配满足条件的空间
strcpy(occupys[occupy_quantity].tag,jobname);
occupys[occupy_quantity].length=joblength;
occupy_quantity++;
if(frees[j].length>joblength)
{
frees[j].start+=joblength;
frees[j].length-=joblength;
}
else
{
for(i=j;i{
frees[i].start=frees[i+1].start;
frees[i].length=frees[i+1].length;
}
free_quantity--;
}
printf("作业申请内存空间成功!
\n");
getchar();
getchar();
}
}
voidexcellent()//最佳适应法
{
charjobname[20];
intjoblength,f=0;
inti,j;printf("请输入作业名:
\n");
scanf("%s",&jobname);
printf("输入作业的长度:
\n");
scanf("%d",&joblength);
for(i=0;i{
if(frees[i].length>=joblength)
f=1;
}
if(f==0)
{
printf("\n当前没有能满足你申请长度的空闲内存,请稍候再试\n");
getchar();
}
else//找到了满足的空间
{
intt=0;
j=0;
while(t==0)
{
if(frees[j].length>=joblength)
{
t=1;
}
j++;
}
j--;
for(i=0;i{
if(frees[i].length>=joblength&&frees[i].lengthj=i;
}
occupys[occupy_quantity].start=frees[j].start;//分配空闲空间
strcpy(occupys[occupy_quantity].tag,jobname);
occupys[occupy_quantity].length=joblength;
occupy_quantity++;
if(frees[j].length>joblength)
{
frees[j].start+=joblength;
frees[j].length-=joblength;
}
else
{
for(i=j;i{
frees[i].start=frees[i+1].start;
frees[i].length=frees[i+1].length;
}
free_quantity--;
}
printf("作业申请内存空间成功!
\n");
getchar();
getchar();
}
}
voidworst()
{
charjobname[20];
intjoblength,f=0;inti,j;
printf("请输入作业名:
\n");
scanf("%s",&jobname);
printf("输入作业的长度:
\n");
scanf("%d",&joblength);
for(i=0;i{
if(frees[i].length>=joblength)
f=1;
}
if(f==0)
{
printf("\n当前没有能满足你申请长度的空闲内存,请稍候再试\n");
getchar();
getchar();
}
else//找到了满足的空间
{
intt=0;
j=0;
while(t==0)
{
if(frees[j].length>=joblength)
{
t=1;
}
j++;
}
j--;
for(i=0;i{
if(frees[i].length>=joblength&&frees[i].length>frees[j].length)
j=i;
}
occupys[occupy_quantity].start=frees[j].start;//分配空闲空间
strcpy(occupys[occupy_quantity].tag,jobname);
occupys[occupy_quantity].length=joblength;
occupy_quantity++;
if(frees[j].length>joblength)
{
frees[j].start+=joblength;
frees[j].length-=joblength;
}
else
{
for(i=j;i{
frees[i].start=frees[i+1].start;
frees[i].length=frees[i+1].length;
}
free_quantity--;
}
printf("作业申请内存空间成功!
\n");
getchar();
getchar();
}
}
voidmain()
{
initial();
intn;
writedata();
system("cls");
readdata();
for(;;)
{
sort();
printf("************************************\n");
printf("************************************\n");
printf("**可变分区存储管理系统**\n");
printf("************************************\n");
printf("**1.显示空闲表和分配表**\n");
printf("**2.首次适应算法(FF)**\n");
printf("**3.最佳适应算法(BF)**\n");
printf("**4.最坏适应算法(WF)**\n");
printf("**0.退出系统**\n");
printf("************************************\n");
printf("************************************\n");
printf("请输入您要选择的项目:
\n");
scanf("%d",&n);
for(;;)
{
if(n<0||n>4)
{
printf("没有这个选项,请重新输入!
");
scanf("%d",&n);
}
else
break;
}
switch(n)
{
case0:
printf("感谢您的使用!
再见!
\n");exit(0);
case1:
view();break;
case2:
earliest();break;
case3:
excellent();break;
case4:
worst();break;
}
system("cls");
}
}