模拟内存分配–操作系统课程设计

实验目的

了解用户程序分配内存以及回收所用内存的过程,加深对操作系统存储管理机制的理解。

实验内容

采用首次适应法、最佳适应法或最差适应法,编写一内存分配和回收模拟程序。

实验选取算法

实验选取的算法是首次适应算法,即分配第一个足够大的孔。查找可以从头开始,也可以从上次首次适应结束时开始。一旦找到足够大的空闲孔,就可以停止。

算法代码:
#include “stdio.h”
#include”stdlib.h”

/* 定义内存分配队列 */
struct fenpei
{
int flag; /* 进程占用  0表示空闲,其他数值表示相应进程 */
int add; /* 起始地址 */
int length; /* 占有长度 */
};
struct fenpei freeq[13];

/* 请求和释放的结构体*/
struct af
{
int num; /*进程ID*/
int af; /* a表示申请,f表示释放 */
int length; /*占用长度*/
};
struct af allocq[13];

/* 首次适应算法函数*/

void go(struct af allowqnow,int *ptotal,struct fenpei *pfreeq)
{
int i,j,num;
int temp_num,temp_add,temp_length;
struct fenpei temp_f1,temp_f2;

/* 申请空间 */
if (allowqnow.af==’a’)
{

for (i=0;i<13;i++) /*在所有空间中找到可以增加的空位*/
{
if((pfreeq[i].flag==0)&(pfreeq[i].length>allowqnow.length)) /*空间空闲(flag=0)且空间大于需求*/
{
temp_num=pfreeq[i].flag;
temp_add=pfreeq[i].add+allowqnow.length;
temp_length=pfreeq[i].length-allowqnow.length;
pfreeq[i].flag=allowqnow.num;
pfreeq[i].length=allowqnow.length;

if (pfreeq[i+1].length==0)
{
pfreeq[i+1].flag=temp_num;
pfreeq[i+1].add=temp_add;
pfreeq[i+1].length=temp_length;
}
else
if (pfreeq[i+1].add!=temp_add)
{
temp_f1.flag=temp_num; temp_f1.add=temp_add; temp_f1.length=temp_length;
temp_f2=pfreeq[i+1];
for (j=i+1;pfreeq[j].length!=0;j++)
{
pfreeq[j]=temp_f1;
temp_f1=temp_f2;
temp_f2=pfreeq[j+1];
}
pfreeq[j]=temp_f1;
}
break;
}
}
}

/* 释放空间if (allowqnow.af==’f’)*/
else
{
for (i=0;i<13;i++)
{
if (pfreeq[i].flag==allowqnow.num)
{
if ((pfreeq[i-1].flag==0)&(pfreeq[i+1].flag==0)&(i>0))
{
pfreeq[i-1].length=pfreeq[i-1].length+allowqnow.length+pfreeq[i+1].length;
for (j=i;pfreeq[j].length!=0;j++)
{
pfreeq[j].flag=pfreeq[j+2].flag;
pfreeq[j].add=pfreeq[j].add;
pfreeq[j].length=pfreeq[j].length;
}
}
else if ((pfreeq[i-1].flag==0)&(i>0))
{
pfreeq[i-1].length=pfreeq[i-1].length+allowqnow.length;
for (j=i;pfreeq[j].length!=0;j++)
{
pfreeq[j].flag=pfreeq[j+1].flag;
pfreeq[j].add=pfreeq[j+1].add;
pfreeq[j].length=pfreeq[j+1].length;
}
}
else if (pfreeq[i+1].flag==0)
{
pfreeq[i].flag=0;
pfreeq[i].length=allowqnow.length+pfreeq[i+1].length;
for (j=i+1;pfreeq[j].length!=0;j++)
{
pfreeq[j].flag=pfreeq[j+1].flag;
pfreeq[j].add=pfreeq[j+1].add;
pfreeq[j].length=pfreeq[j+1].length;
}
}
else
{
pfreeq[i].flag=0;
}
}
}

}

num=0; /* 统计空闲块*/
for (i=0;pfreeq[i].length!=0;i++)
if (pfreeq[i].flag==0) num++;
*ptotal=num;

}

/*主函数*/
main()
{
int i,j; char p;
int Freetotal=1;
/*a1,a2,f1,a3,f2,a4,a5,a6,f5,a7,a8,f7,a9*最后3,4,6,8,9存在内存中,  rand()%201产生随机进程占用长度*/
allocq[0].num=1;
allocq[0].af=’a’;
allocq[0].length=rand()%201;

allocq[1].num=2;
allocq[1].af=’a’;
allocq[1].length=rand()%201;

allocq[2].num=1;
allocq[2].af=’f’;
allocq[2].length=allocq[0].length;

allocq[3].num=3;
allocq[3].af=’a’;
allocq[3].length=rand()%201;

allocq[4].num=2;
allocq[4].af=’f’;
allocq[4].length=allocq[1].length;

allocq[5].num=4;
allocq[5].af=’a’;
allocq[5].length=rand()%201;

allocq[6].num=5;
allocq[6].af=’a’;
allocq[6].length=rand()%201;

allocq[7].num=6;
allocq[7].af=’a’;
allocq[7].length=rand()%201;

allocq[8].num=5;
allocq[8].af=’f’;
allocq[8].length=allocq[6].length;

allocq[9].num=7;
allocq[9].af=’a’;
allocq[9].length=rand()%201;

allocq[10].num=8;
allocq[10].af=’a’;
allocq[10].length=rand()%201;

allocq[11].num=7;
allocq[11].af=’f’;
allocq[11].length=allocq[9].length;

allocq[12].num=9;
allocq[12].af=’a’;
allocq[12].length=rand()%201;

freeq[0].flag=0;
freeq[0].add=0;
freeq[0].length=900;

printf(”                         ***************************\n”);
printf(”                         *                         *\n”);
printf(”                         *首次适应方法模拟内存分配 *\n”);
printf(”                         ***************************\n\n”);
for(i=0;i<13;i++)
{
go(allocq[i],&Freetotal,freeq);
printf(“\n                    总共有%d个空闲区 “,Freetotal) ;
printf(“\n                    进程占用(0表示空闲)   起始地址   长度”);
for(j=0;freeq[j].length!=0;j++)
printf( “\n                    %5d           %10d   %10d”,freeq[j].flag,freeq[j].add,freeq[j].length);
printf(“\n                    ***************************************\n\n”);
}

printf(”                    班级:*****\n\n                    姓名:piikee\n\n                    学号:***********\n\n”);
printf(“选择Y结束程序\n”);
p=getchar();
}
版权所有,请勿用于商业用途!

打赏

发表评论

您的电子邮箱地址不会被公开。 必填项已用*标注