# 操作系统第5次实验报告：内存管理

• 姓名：林永鑫
• 学号：201821121040
• 班级：计算1812

# 1. 记录内存空间使用情况

struct allocated_block
{
int pid;
int size;
char process_name[process_name_len];
struct allocated_block *next;
};
struct allocated_block *allocated_block_head=NULL;

# 2. 记录空闲分区

struct free_block_type
{
int size;
struct free_block_type *next;
};
struct free_block_type *free_block=NULL;

# 3. 内存分配算法

int rearrange_WF()
{
struct free_block_type *forehand,*pre,*rear;
int i;
return -1;
for(i= 0;i< free_block_count-1;i++)
{
pre= forehand->next;
rear= pre->next;
while(pre->next!= NULL)
{
{
//比较空闲链表中第一个空闲块与第二个空闲块空间的大小
pre= forehand->next;
rear= pre->next;
}
else if(pre->size>= rear->size)
{
//比较链表中其它相邻两个结点的空间的大小
pre->next= rear->next;
forehand->next= rear;
rear->next= pre;
forehand= rear;
rear= pre->next;
}
else
{
forehand= pre;
pre= rear;
rear= rear->next;
}
}
}
return 0;
}

# 4. 内存释放算法

int dispose(struct allocated_block *free_ab)
{
struct allocated_block *pre,*ab;
if(free_block== NULL)
return -1;
{
free(free_ab);
}
else
{
//找到free_ab
while(ab!= free_ab)
{
pre= ab;
ab= ab->next;
}
pre->next= ab->next;
free(ab);
}
return 1;
}
将ab所表示的已分配区归还，并进行可能的合并
int free_mem(struct allocated_block *ab)
{
int algorithm= ma_algorithm;
struct free_block_type *fbt,*pre,*work;
fbt= (struct free_block_type*)malloc(sizeof(struct free_block_type));
if(!fbt)
return -1;
pre= free_block;
fbt->size= ab->size;
fbt->next= NULL;
if(pre!= NULL)
{
while(pre->next!= NULL)
pre= pre->next;
pre->next= fbt;
}
else
{
free_block= fbt;
}
rearrange_FF();
pre= free_block;
work= pre->next;
while(work!= NULL)
{
{
pre->size+= work->size;
free(work);
work= pre->next;
}
else
{
pre= work;
work= work->next;
}
}
current_free_mem_size+= ab->size;
return 1;
}
将ab所表示的已分配区归还，并进行可能的合并
void kill_process()
{
struct allocated_block *ab;
int pid;
printf("Kill Process,pid=");
scanf("%d",&pid);
getchar();
ab= find_process(pid);
if(ab!= NULL)
{
free_mem(ab);  //释放ab所表示的分配区
dispose(ab);   //释放ab数据结构结点
}
}

# 5. 运行结果

（1）产生测试数据

int main(int argc, char const *argv[]){
free_block = init_free_block(mem_size); //初始化空闲区
set_mem_size();
int size;
srand((unsigned)time(0));
//初始化三个进程
for(int i=0;i<3;i++){

size=rand()%100;
new_process();
}
//分配、释放内存10次
int m=0;
int n=0;
while(m<10){
n=m+1;
//结束一个进程
kill_process()
size=rand()%100;
//创造一个进程
new_process(size);
display_mem_usage();
m++;
}
}

（2）解释结果

posted @ 2020-05-17 04:39  L1nnnnn  阅读(191)  评论(0编辑  收藏  举报