操作系统 实验四主存空间的分配和回收
实验四主存空间的分配和回收
1. 目的和要求
1.1. 实验目的
用高级语言完成一个主存空间的分配和回收程序,以加深对动态分区分配方式及其算法的理解。
1.2. 实验要求
采用连续分配方式之动态分区分配存储管理,使用首次适应算法、循环首次适应算法、最佳适应算法和最坏适应算法4种算法完成设计。
(1)**设计一个作业申请队列以及作业完成后的释放顺序,实现主存的分配和回收。采用分区说明表进行。
(2)或在程序运行过程,由用户指定申请与释放。
(3)设计一个空闲区说明表,以保存某时刻主存空间占用情况。
把空闲区说明表的变化情况以及各作业的申请、释放情况显示。
2. 实验内容
根据指定的实验课题,完成设计、编码和调试工作,完成实验报告。
3. 实验环境
可以选用Visual C++作为开发环境。也可以选用Windows下的VB,CB或其他可视化环境,利用各种控件较为方便。自主选择实验环境。
4. 参考数据结构:
#include<stdio.h>
#include<conio.h>
#include<string.h>
#define MAX 24
struct partition{
char pn[10];
int begin;
int size;
int end; ////////
char status; //////////
};
typedef struct partition PART;
五、源代码:
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<math.h>
#define N 10000
int n1;//空闲分区的个数
int n2;//作业区的个数
struct kongxian
{
int start; //起址
int end; //结束
int length; //长度
}kongxian[N];
struct zuoye
{
int start; //起址
int end; //结束
int length; //长度
}zuoye[N];
int cmp1(const void *a,const void *b)
{
return (*(struct kongxian *)a).start-(*(struct kongxian *)b).start;
}
int cmp2(const void *a,const void *b)
{
return (*(struct zuoye *)a).start-(*(struct zuoye *)b).start;
}
void init()
{
n1=1; //空闲区
n2=0; //作业
kongxian[0].start=0;
kongxian[0].end=1023;
kongxian[0].length=1024;
}
void print1() //打印空闲分区
{
int i;
for(i=0;i<n1;i++)
printf("空闲分区ID:%d 起止:%d 结束:%d 长度:%d\n",i,kongxian[i].start,kongxian[i].end,kongxian[i].length);
}
void print2() //打印作业分区
{
int i;
for(i=0;i<n2;i++)
printf("作业分区ID:%d 起止:%d 结束:%d 长度:%d\n",i,zuoye[i].start,zuoye[i].end,zuoye[i].length);
}
int main()
{
int i,j,k,t,len,flag,id;
int choice ;
int front,middle, behind;
int t1,t2;
init();
print1();
do{
printf("\n 分区分配算法:\n\t(1)首次适应算法\n\t(2)循环首次适应算法\n\t(3)最佳适应算法\n\t(4)最坏适应算法\n\t\n");
printf(">>请选择相应的算法(1-4):");
scanf("%d",&choice);
if (0==choice) exit(0);
}while(0>choice&&2<choice);
printf("输入1装入新作业,输入0回收作业,输入-1结束\n");
while(scanf("%d",&t)!=EOF)
{
if(t==1) //装入新作业
{
printf("请输入作业的占用空间的长度 ");
scanf("%d",&len);
flag=0;
for(i=0;i<n1;i++)
{
if(kongxian[i].length>=len) //首次适应算法
{
flag=1;
break;
}
}
if(!flag)
{
printf("内存分配失败\n");
}
else
{
//将该作业加入作业区里
zuoye[n2].start=kongxian[i].start;
zuoye[n2].end=zuoye[n2].start+len;
zuoye[n2].length=len;
n2++; //作业数加1
if(kongxian[i].length==len) //该分区全部用于分配,删除该空闲分区
{
for(j=i;j<n1-1;j++)
{
kongxian[j].start=kongxian[j+1].start;
kongxian[j].end=kongxian[j+1].end;
kongxian[j].length=kongxian[j+1].length;
}
n1--;
}
else //该空闲分区部分用于分配,剩余的留在空闲分区中
{
kongxian[i].start+=len;
kongxian[i].length-=len;
}
}
}
else if(t==0)
{
printf("输入要回收的作业ID ");
scanf("%d",&id);
front=middle=behind=0;
for(i=0;i<n1;i++)
{
if(kongxian[i].start>zuoye[id].end)
break;
if(kongxian[i].end==zuoye[id].start) //待回收的作业上面有空闲分区
{
front=1;
t1=i;
}
if(kongxian[i].start==zuoye[id].end) //待回收的作业下面有空闲分区
{
behind=1;
t2=i;
}
}
if(!front&&!behind) //待回收的作业上下均没有空闲分区
{
kongxian[n1].start=zuoye[id].start;
kongxian[n1].end=zuoye[id].end;
kongxian[n1].length=zuoye[id].length;
n1++; //空闲分区增加一个
qsort(kongxian,n1,sizeof(struct kongxian),cmp1); //插入空闲分区后排序
for(j=id;j<n2-1;j++) //在作业分区中删除该作业
{
zuoye[j].start=zuoye[j+1].start;
zuoye[j].end=zuoye[j+1].end;
zuoye[j].length=zuoye[j+1].length;
}
n2--;
}
if(front &&behind) //待回收的作业上下均有空闲分区
middle=1;
if(front&&!middle) //合并待回收的作业和上面的空闲分区
{
kongxian[t1].end+=zuoye[id].length;
kongxian[t1].length+=zuoye[id].length;
for(j=id;j<n2-1;j++) //在作业分区中删除该作业
{
zuoye[j].start=zuoye[j+1].start;
zuoye[j].end=zuoye[j+1].end;
zuoye[j].length=zuoye[j+1].length;
}
n2--;
}
if(middle) //合并待回收的作业和上下的空闲分区
{
kongxian[t1].end=kongxian[t2].end;
kongxian[t1].length+=(zuoye[id].length+kongxian[t2].length);
//删除空闲分区t2
for(j=t2;j<n1-1;j++)
{
kongxian[j].start=kongxian[j+1].start;
kongxian[j].end=kongxian[j+1].end;
kongxian[j].length=kongxian[j+1].length;
}
n1--;
for(j=id;j<n2-1;j++) //在作业分区中删除该作业
{
zuoye[j].start=zuoye[j+1].start;
zuoye[j].end=zuoye[j+1].end;
zuoye[j].length=zuoye[j+1].length;
}
n2--;
}
if(behind &&!middle) //合并待回收的作业和下面的分区
{
kongxian[t2].start-=zuoye[id].length;
kongxian[t2].length+=zuoye[id].length;
for(j=id;j<n2-1;j++) //在作业分区中删除该作业
{
zuoye[j].start=zuoye[j+1].start;
zuoye[j].end=zuoye[j+1].end;
zuoye[j].length=zuoye[j+1].length;
}
n2--;
}
}
else
{
printf("操作结束\n");
break;
}
print1();
print2();
}
return 0;
}
6、总结:
通过这次实验,
浙公网安备 33010602011771号