1、内存管理实验 内存管理实验一实验目的1.通过本次实验体会操作系统中内存的分配模式;2.掌握内存分配的方法;3.学会进程的建立,当一个进程被终止时内存是如何处理被释放块,并当内存不满足进程申请时是如何使用内存紧凑;4.掌握内存回收过程及实现方法;5.学会进行内存的申请释放和管理;6.掌握内存分配FF,BF,WF策略及实现的思路;2. 实验内容1.编写程序实现采用可变分区方法管理内存。2.在该实验中,采用可变分区方式完成对存储空间的管理。3.设计用来记录主存使用情况的数据结构:已分区表和空闲分区表或链表。4.在设计好的数据结构上设计一个主存分配算法。5.在设计好的数据结构上设计一个主存回收算法。其
2、中,若回收的分区有上邻空闲分区和(或)下邻空闲分区,要求合并为一个空闲分区登记在空闲分区表的一个表项里。三概要设计1功能模块图2各个模块详细的功能描述主要数据结构:struct free_block_type /空闲块 int size; int start_addr; struct free_block_type *next;struct allocated_block /已分配的内存块 int pid; int size; int start_addr; char process_namePROCESS_NAME_LEN; struct allocated_block *next;(1)S
3、et memory size (default=1024): 这个模块是用来设置内存大小的,从键盘获取一个数字,并将它赋值给内存大小;若没有设置,则默认内存的大小为1024。(2)Set_algorithm: 这个模块是用来设置分配算法的,共有三种算法:首次循环适配算法、最好适配算法、最差适配算法。从键盘输入一种算法前的序号,根据算法点用不同的函数对内存进行分配;(3)New_process: 此模块是用来创建进程的。从键盘输入进程号,调用fork()创建进程并为其分配一定大小的内存,若分配成功,则将其连接到已分配链表中,否则分配失败;(4)Kill_process: 此模块是用来杀死进程的。
4、从键盘输入一个进程号,先调用find_process()函数进行查找,若找到,则调用kill()函数将其杀死并释放内存空间;(5)Display_mem_usage: 此模块是用来显示内存的使用情况的。将每个进程的内存使用情况显示出来,包括起始地址和内存大小;(6)Do_exit: 这个模块是用来结束程序的,直接调用exit()实现。4详细设计1功能函数的调用关系图2各功能函数流程图Set_algorithm(): New_process():Allocate_mem(): Kill_process(): 五测试数据及运行结果 显示菜单: 设置内存空间大小:创建进程:杀死进程1:选择内存分配算
5、法菜单:首次适应法:6调试情况设计技巧及体会1.调试情况:第一步:对.c文件进行编译,生成目标文件(.o文件),使用的命令是gcc -c a.c(a是文件名);第二步:编译.o文件生成可执行文件(.exe文件),使用的命令是gcc -o a a.o(a是文件名);第三步:运行.exe文件查看结果,使用的命令是./a(a是文件名);2.设计技巧及体会 这次实验让我们充分了解了内存分配的机制和管理内存的几种方法,从而更进一步对计算机操作系统有了更深的认识。虽然在这次实验的过程中遇到了很多问题,对内存理解的不深刻,不太清楚计算机系统中是如何对内存进行管理的,但是通过这次实验,现在已经清楚地知道了内存
6、的管理机制,对内存理解的更加透彻。在以后的学习中能够更好的理解操作系统在计算机应用中起到的作用。七源代码#include#include#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 3int mem_size=DEFAULT_MEM_SIZE;int ma_algorithm=MA_FF;static int pid=0;int flag=0;voi
7、d display_menu(void); struct free_block_type *init_free_block(int mem_size);int set_mem_size();void set_algorithm();void rearrange(int algorithm);void rearrange_FF();void rearrange_BF();void rearrange_WF();void swap(int *a,int *b);int new_process();int display_mem_usage();void do_exit();void kill_pr
8、ocess();int display_mem_usage();struct allocated_block * find_process(int pid);struct free_block_type int size; int start_addr; struct free_block_type *next;struct allocated_block int pid; int size; int start_addr; char process_namePROCESS_NAME_LEN; struct allocated_block *next;struct free_block_typ
9、e * free_block;struct allocated_block * allocated_block_head=NULL;int main(void) char choice; pid=0; free_block=init_free_block(mem_size); for(;) display_menu();/ fflush(stdin); choice=getchar();/ getchar(); switch(choice) case 1:set_mem_size();break; case 2:set_algorithm();flag=1;break; case 3:new_
10、process();flag=1;break; case 4:kill_process();flag=1;break; case 5:display_mem_usage();flag=1;break; case 0:do_exit();exit(0); default:break; return 0;struct free_block_type * init_free_block(int mem_size) struct free_block_type *fb; fb=(struct free_block_type *)malloc(sizeof(struct free_block_type)
11、; if(fb=NULL) printf(No memn); return NULL; fb-size=mem_size; fb-start_addr=DEFAULT_MEM_START; fb-next=NULL; return fb; int set_mem_size() int size; if(flag!=0) printf(Cannot set memory size againn); return 0; printf(Total memory size=); scanf(%d,&size); if(size0) mem_size=size; free_block-size=mem_
12、size; flag=1; return 1;void set_algorithm() int algorithm; printf(t1-First Fitn); printf(t2-Best Fitn); printf(t3-Worst Fitn); scanf(%d,&algorithm); if(algorithm=1 & algorithmnext; while(work!=NULL) if(work-start_addrstart_addr) swap(&work-start_addr,&tmp-start_addr); swap(&work-size,&tmp-size); els
13、e work=work-next; tmp=tmp-next; void rearrange_BF() struct free_block_type *tmp,*work; printf(Rearrange free blocks for BFn); tmp=free_block; printf(%d,%dn,tmp-start_addr,tmp-size); while(tmp!=NULL) work=tmp-next; while(work!=NULL) if(work-sizesize) swap(&work-start_addr,&tmp-start_addr); swap(&work
14、-size,&tmp-size); printf(start_addr:%d,size:%dn,work-start_addr,work-size); else work=work-next; tmp=tmp-next; void rearrange_WF() struct free_block_type *tmp,*work; printf(Rearrange free blocks for FFn); tmp=free_block; printf(%d,%dn,tmp-start_addr,tmp-size); while(tmp!=NULL) work=tmp-next; while(w
15、ork!=NULL) if(work-sizetmp-size) swap(&work-start_addr,&tmp-start_addr); swap(&work-size,&tmp-size); printf(start_addr:%d,size:%dn,work-start_addr,work-size); else work=work-next; tmp=tmp-next; void swap(int *a,int *b) int temp; temp=*a; *a=*b; *b=temp;int new_process() struct allocated_block *ab; i
16、nt size; int ret; ab=(struct allocated_block *)malloc(sizeof(struct allocated_block); if(!ab) exit(-5); ab-next=NULL; pid+; sprintf(ab-process_name,PROCESS-%02d,pid); ab-pid=pid; printf(Memory for %s:,ab-process_name); scanf(%d,&size); if(size0) ab-size=size; ret=allocate_mem(ab); if(ret=1) &(alloca
17、ted_block_head=NULL) allocated_block_head=ab; return 1; else if(ret=1) ab-next=allocated_block_head; allocated_block_head=ab; return 2; else if(ret=-1) printf(printf(Allocation failn); free(ab); return -1; return -3;int allocate_mem(struct allocated_block *ab) struct free_block_type *fbt,*pre; int r
18、equest_size=ab-size; fbt=pre=free_block; while(fbt!=NULL) if(fbt-size=request_size) if(fbt-size-request_sizeMIN_SLICE) fbt-size=fbt-size-request_size; / ab-size=request_size; ab-start_addr=fbt-start_addr; fbt-start_addr=fbt-start_addr+request_size; printf(%d, %dn,ab-start_addr,ab-size); return 1; el
19、se / pre-next=fbt-next; ab-size=fbt-size; ab-start_addr=fbt-start_addr; if(fbt=free_block) free_block=fbt-next; free(fbt); else pre-next=fbt-next; free(fbt); printf(%d,%dn,ab-start_addr,ab-size); return 1; else pre=fbt; fbt=fbt-next; return -1;void kill_process() struct allocated_block *ab; int pid;
20、 printf(Kill Process,pid=); scanf(%d,&pid); ab=find_process(pid); if(ab!=NULL) free_mem(ab); dispose(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
21、-1; fbt-size=ab-size; fbt-start_addr=ab-start_addr; fbt-next=free_block; free_block=fbt; rearrange(MA_FF); fbt=free_block; while(fbt!=NULL) work=fbt-next; if(work!=NULL) if(fbt-start_addr+fbt-size=work-start_addr) fbt-size=fbt-size+work-size; fbt-next=work-next; free(work); continue; fbt=fbt-next; /
22、 rearrange(algorithm); return 1;int dispose(struct allocated_block * free_ab) struct allocated_block *pre,*ab; if(free_ab=allocated_block_head) allocated_block_head=allocated_block_head-next; free(free_ab); return 1; pre=allocated_block_head; ab=allocated_block_head-next; while(ab!=free_ab) pre=ab;
23、ab=ab-next; pre-next=ab-next; free(ab); return 2;int display_mem_usage() struct free_block_type *fbt=free_block; struct allocated_block *ab=allocated_block_head; if(fbt=NULL) return -1; printf(-n); printf(Free Memory:n); printf(%20s%20sn,start_addr,size); while(fbt!=NULL) printf(%20d%20dn,fbt-start_
24、addr,fbt-size); fbt=fbt-next; printf(nUsed Memory:n); printf(%10s %20s %10s %10sn,PID,ProcessName,start_addr,size); while(ab!=NULL) printf(%10d %20s %10d %10dn,ab-pid,ab-process_name,ab-start_addr,ab-size); ab=ab-next; printf(-n); return 0;void do_exit() exit(0);struct allocated_block * find_process
25、(int pid) struct allocated_block * ab; ab=allocated_block_head; while(ab!=NULL) if(ab-pid=pid) return ab; else ab=ab-next; void display_menu(void) printf(n); printf(1-Set memory size(default=%d)n,DEFAULT_MEM_SIZE); printf(2-Select memory allocation algorithmn); printf(3-New processn); printf(4-Terminate a processn); printf(5-Display memory usagen); printf(0-Exitn);
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1