当前位置:文档之家› 操作系统实验内存分配

操作系统实验内存分配

西安邮电大学(计算机学院)课内实验报告实验名称:内存管理专业名称:软件工程班级:学生姓名:学号(8位):指导教师:实验日期:实验五:进程1.实验目的通过深入理解区管理的三种算法,定义相应的数据结构,编写具体代码。

充分模拟三种算法的实现过程,并通过对比,分析三种算法的优劣。

(1)掌握内存分配FF,BF,WF策略及实现的思路;(2)掌握内存回收过程及实现思路;(3)参考给出的代码思路,实现内存的申请、释放的管理程序,调试运行,总结程序设计中出现的问题并找出原因,写出实验报告。

2.实验要求:1)掌握内存分配FF,BF,WF策略及实现的思路;2)掌握内存回收过程及实现思路;3)参考本程序思路,实现内存的申请、释放的管理程序,调试运行,总结程序设计中出现的问题并找出原因,写出实验报告。

3.实验过程:创建进程:删除其中几个进程:(默认以ff首次适应算法方式排列)Bf最佳适应算法排列方式:wf最差匹配算法排列方式:4.实验心得:这次实验实验时间比较长,而且实验指导书中对内存的管理讲的很详细,老师上课的时候也有讲的很详细,但是代码比较长,刚开始的时候也是不太懂,但是后面经过和同学一起商讨,明白几种算法的含义:①首次适应算法。

在采用空闲分区链作为数据结构时,该算法要求空闲分区链表以地址递增的次序链接。

在进行内存分配时,从链首开始顺序查找,直至找到一个能满足进程大小要求的空闲分区为止。

然后,再按照进程请求内存的大小,从该分区中划出一块内存空间分配给请求进程,余下的空闲分区仍留在空闲链中。

②循环首次适应算法。

该算法是由首次适应算法演变而形成的,在为进程分配内存空间时,从上次找到的空闲分区的下一个空闲分区开始查找,直至找到第一个能满足要求的空闲分区,并从中划出一块与请求的大小相等的内存空间分配给进程。

③最佳适应算法将空闲分区链表按分区大小由小到大排序,在链表中查找第一个满足要求的分区。

④最差匹配算法将空闲分区链表按分区大小由大到小排序,在链表中找到第一个满足要求的空闲分区。

实验中没有用到循环首次适应算法,但是对其他三种的描述还是很详细,总的来说,从实验中还是学到了很多。

5.程序源代码:#include<stdio.h>#include<malloc.h>#include<unistd.h>#include<stdlib.h>#define PROCESS_NAME_LEN 32 //进程名长度#define MIN_SLICE 10 //最小碎片的大小#define DEFAULT_MEM_SIZE 1024 //内存大小#define DEFAULT_MEM_START 0 //起始位置/*内存分配算法*/#define MA_FF 1#define MA_BF 2#define MA_WF 3/*描述每一个空闲块的数据结构*/struct free_block_type{int size; //空闲块大小int start_addr; //空闲块起始地址struct free_block_type *next; //指向下一个空闲块};/*指向内存中空闲块链表的首指针*/struct free_block_type *free_block = NULL;/*每个进程分配到的内存块的描述*/struct allocated_block{int pid; //进程标识符int size; //进程大小int start_addr; //进程分配到的内存块的起始地址char process_name[PROCESS_NAME_LEN]; //进程名struct allocated_block *next; //指向下一个进程控制块};/*进程分配内存块链表的首指针*/struct allocated_block *allocated_block_head = NULL;int free_block_count = 0; //空闲块个数int mem_size = DEFAULT_MEM_SIZE; //内存大小int current_free_mem_size = 0; //当前空闲内存大小int ma_algorithm = MA_FF; //当前分配算法static int pid = 0; //初始PIDint flag = 0; //设置内存大小标志,表示内存大小是否设置/*函数声明*/struct free_block_type* init_free_block(int mem_size);void display_menu();int set_mem_size();void set_algorithm();void rearrange(int algorithm);int rearrange_WF();int rearrange_BF();int rearrange_FF();int new_process();int allocate_mem(struct allocated_block *ab);void kill_process();int free_mem(struct allocated_block *ab);int dispose(struct allocated_block *free_ab);int display_mem_usage();struct allocated_block *find_process(int pid);int do_exit();int allocate_FF(struct allocated_block *ab);int allocate_BF(struct allocated_block *ab);int allocate_WF(struct allocated_block *ab);int allocate(struct free_block_type *pre, struct free_block_type *allocate_free_nlock, struct allocated_block *ab);int mem_retrench(struct allocated_block *ab);// 通过内存紧缩技术给新进程分配内存空间int mem_retrench(struct allocated_block *ab){struct allocated_block *allocated_work, *allocated_pre = allocated_block_head;struct free_block_type *free_work, *free_pre = free_block->next;if(allocated_pre == NULL)return -1;allocated_pre->start_addr = 0;allocated_work = allocated_pre->next;while(allocated_work != NULL){allocated_work->start_addr = allocated_pre->start_addr + allocated_pre->size;allocated_pre = allocated_work;allocated_work = allocated_work->next;}free_block->start_addr = allocated_pre->start_addr + allocated_pre->size;free_block->size = current_free_mem_size;free_block->next = NULL;free_work = free_pre;while(free_pre != NULL){free(free_pre);free_pre = free_work;if(free_pre != NULL)free_work = free_work->next;}allocate(NULL, free_block, ab);return 1;}// 给新进程分配内存空间int allocate(struct free_block_type *pre, struct free_block_type *allocate_free_block, struct allocated_block *ab){struct allocated_block *p = allocated_block_head;ab->start_addr = allocate_free_block->start_addr;if(allocate_free_block->size - ab->size < MIN_SLICE){ab->size = allocate_free_block->size;if(pre != NULL){pre->next = allocate_free_block;}else{free_block = allocate_free_block->next;}free(allocate_free_block);}else{allocate_free_block->start_addr += ab->size;allocate_free_block->size -= ab->size;}if(p == NULL){allocated_block_head = ab;}else{while(p->next != NULL)p = p->next;p->next = ab;}current_free_mem_size -= ab->size;if(current_free_mem_size == 0)free_block = NULL;return 0;}//按照最坏适应算法给新进程分配内存空间int allocate_WF(struct allocated_block *ab){int ret;struct free_block_type *wf = free_block;if(wf == NULL)return -1;if(wf->size >= ab->size)allocate(NULL, wf, ab);else if(current_free_mem_size >= ab->size)ret = mem_retrench(ab);elseret = -2;rearrange_WF();return ret;}// 按照最佳适应算法给新进程分配内存空间int allocate_BF(struct allocated_block *ab){int ret;struct free_block_type *pre = NULL, *bf = free_block;if(bf == NULL)return -1;while(bf != NULL){if(bf->size >= ab->size){ret = allocate(pre, bf,ab);break;}pre = bf;pre = pre->next;}if(bf == NULL && current_free_mem_size > ab->size) ret = mem_retrench(ab);elseret = -2;rearrange_BF();return ret;}// 按照首次适应算法给新进程分配内存空间int allocate_FF(struct allocated_block *ab){int ret;struct free_block_type *pre = NULL, *ff = free_block;if(ff == NULL)return -1;while(ff != NULL){if(ff->size >= ab->size){ret = allocate(pre, ff,ab);break;}pre = ff;pre = pre->next;}if(ff == NULL && current_free_mem_size > ab->size) ret = mem_retrench(ab);elseret = -2;rearrange_FF();return ret;}//分配内存模块int allocate_mem(struct allocated_block *ab){int ret ;struct free_block_type *fbt, *pre;int request_size = ab->size;fbt = pre = free_block;switch(ma_algorithm){case MA_FF :ret = allocate_FF(ab);break;case MA_BF :ret = allocate_BF(ab);break;case MA_WF :ret = allocate_WF(ab);break;default :break;}return ret;}// 创建一个新的进程。

相关主题