操作系统实验内存分配Word下载.docx

上传人:b****3 文档编号:7753166 上传时间:2023-05-09 格式:DOCX 页数:32 大小:412.44KB
下载 相关 举报
操作系统实验内存分配Word下载.docx_第1页
第1页 / 共32页
操作系统实验内存分配Word下载.docx_第2页
第2页 / 共32页
操作系统实验内存分配Word下载.docx_第3页
第3页 / 共32页
操作系统实验内存分配Word下载.docx_第4页
第4页 / 共32页
操作系统实验内存分配Word下载.docx_第5页
第5页 / 共32页
操作系统实验内存分配Word下载.docx_第6页
第6页 / 共32页
操作系统实验内存分配Word下载.docx_第7页
第7页 / 共32页
操作系统实验内存分配Word下载.docx_第8页
第8页 / 共32页
操作系统实验内存分配Word下载.docx_第9页
第9页 / 共32页
操作系统实验内存分配Word下载.docx_第10页
第10页 / 共32页
操作系统实验内存分配Word下载.docx_第11页
第11页 / 共32页
操作系统实验内存分配Word下载.docx_第12页
第12页 / 共32页
操作系统实验内存分配Word下载.docx_第13页
第13页 / 共32页
操作系统实验内存分配Word下载.docx_第14页
第14页 / 共32页
操作系统实验内存分配Word下载.docx_第15页
第15页 / 共32页
操作系统实验内存分配Word下载.docx_第16页
第16页 / 共32页
操作系统实验内存分配Word下载.docx_第17页
第17页 / 共32页
操作系统实验内存分配Word下载.docx_第18页
第18页 / 共32页
操作系统实验内存分配Word下载.docx_第19页
第19页 / 共32页
操作系统实验内存分配Word下载.docx_第20页
第20页 / 共32页
亲,该文档总共32页,到这儿已超出免费预览范围,如果喜欢就下载吧!
下载资源
资源描述

操作系统实验内存分配Word下载.docx

《操作系统实验内存分配Word下载.docx》由会员分享,可在线阅读,更多相关《操作系统实验内存分配Word下载.docx(32页珍藏版)》请在冰点文库上搜索。

操作系统实验内存分配Word下载.docx

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

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

循环首次适应算法。

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

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

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

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

5.程序源代码:

#include<

>

#definePROCESS_NAME_LEN32//进程名长度

#defineMIN_SLICE10//最小碎片的大小

#defineDEFAULT_MEM_SIZE1024//内存大小

#defineDEFAULT_MEM_START0//起始位置

/*内存分配算法*/

#defineMA_FF1

#defineMA_BF2

#defineMA_WF3

/*描述每一个空闲块的数据结构*/

structfree_block_type

{

intsize;

//空闲块大小

intstart_addr;

//空闲块起始地址

structfree_block_type*next;

//指向下一个空闲块

};

/*指向内存中空闲块链表的首指针*/

structfree_block_type*free_block=NULL;

/*每个进程分配到的内存块的描述*/

structallocated_block

intpid;

//进程标识符

//进程大小

//进程分配到的内存块的起始地址

charprocess_name[PROCESS_NAME_LEN];

//进程名

structallocated_block*next;

//指向下一个进程控制块

/*进程分配内存块链表的首指针*/

structallocated_block*allocated_block_head=NULL;

intfree_block_count=0;

//空闲块个数

intmem_size=DEFAULT_MEM_SIZE;

//内存大小

intcurrent_free_mem_size=0;

//当前空闲内存大小

intma_algorithm=MA_FF;

//当前分配算法

staticintpid=0;

//初始PID

intflag=0;

//设置内存大小标志,表示内存大小是否设置

/*函数声明*/

structfree_block_type*init_free_block(intmem_size);

voiddisplay_menu();

intset_mem_size();

voidset_algorithm();

voidrearrange(intalgorithm);

intrearrange_WF();

intrearrange_BF();

intrearrange_FF();

intnew_process();

intallocate_mem(structallocated_block*ab);

voidkill_process();

intfree_mem(structallocated_block*ab);

intdispose(structallocated_block*free_ab);

intdisplay_mem_usage();

structallocated_block*find_process(intpid);

intdo_exit();

intallocate_FF(structallocated_block*ab);

intallocate_BF(structallocated_block*ab);

intallocate_WF(structallocated_block*ab);

intallocate(structfree_block_type*pre,structfree_block_type*allocate_free_nlock,structallocated_block*ab);

intmem_retrench(structallocated_block*ab);

//通过内存紧缩技术给新进程分配内存空间

intmem_retrench(structallocated_block*ab)

structallocated_block*allocated_work,*allocated_pre=allocated_block_head;

structfree_block_type*free_work,*free_pre=free_block->

next;

if(allocated_pre==NULL)

return-1;

allocated_pre->

start_addr=0;

allocated_work=allocated_pre->

while(allocated_work!

=NULL)

{

allocated_work->

start_addr=allocated_pre->

start_addr+allocated_pre->

size;

allocated_pre=allocated_work;

allocated_work=allocated_work->

}

free_block->

size=current_free_mem_size;

next=NULL;

free_work=free_pre;

while(free_pre!

free(free_pre);

free_pre=free_work;

if(free_pre!

free_work=free_work->

allocate(NULL,free_block,ab);

return1;

}

//给新进程分配内存空间

intallocate(structfree_block_type*pre,structfree_block_type*allocate_free_block,structallocated_block*ab)

structallocated_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->

if(pre!

{

pre->

next=allocate_free_block;

}

else

free_block=allocate_free_block->

free(allocate_free_block);

else

allocate_free_block->

start_addr+=ab->

size-=ab->

if(p==NULL)

allocated_block_head=ab;

while(p->

next!

p=p->

p->

next=ab;

current_free_mem_size-=ab->

if(current_free_mem_size==0)

free_block=NULL;

return0;

//按照最坏适应算法给新进程分配内存空间

intallocate_WF(structallocated_block*ab)

intret;

structfree_block_type*wf=free_block;

if(wf==NULL)

if(wf->

size>

=ab->

size)

allocate(NULL,wf,ab);

elseif(current_free_mem_size>

ret=mem_retrench(ab);

ret=-2;

rearrange_WF();

returnret;

//按照最佳适应算法给新进程分配内存空间

intallocate_BF(structallocated_block*ab)

structfree_block_type*pre=NULL,*bf=free_block;

if(bf==NULL)

while(bf!

if(bf->

ret=allocate(pre,bf,ab);

break;

pre=bf;

pre=pre->

if(bf==NULL&

&

current_free_mem_size>

rearrange_BF();

//按照首次适应算法给新进程分配内存空间

intallocate_FF(structallocated_block*ab)

structfree_block_type*pre=NULL,*ff=free_block;

if(ff==NULL)

while(ff!

if(ff->

ret=allocate(pre,ff,ab);

pre=ff;

if(ff==NULL&

rearrange_FF();

//分配内存模块

intallocate_mem(structallocated_block*ab)

intret;

structfree_block_type*fbt,*pre;

intrequest_size=ab->

fbt=pre=free_block;

switch(ma_algorithm)

caseMA_FF:

ret=allocate_FF(ab);

caseMA_BF:

ret=allocate_BF(ab);

caseMA_WF:

ret=allocate_WF(ab);

default:

//创建一个新的进程。

intnew_process()

structallocated_block*ab;

ab=(structallocated_block*)malloc(sizeof(structallocated_block));

if(!

ab)

exit(-5);

pid++;

sprintf(ab->

process_name,"

PROCESS-%02d"

pid);

//sprintf()函数将格式化的数据写入某字符串中

pid=pid;

printf("

Memoryfor%s:

"

ab->

process_name);

for(;

;

scanf("

%d"

&

size);

getchar();

if(size>

0)

ab->

size=size;

printf("

Thesizehavetogreaterthanzero!

Pleaseinputagain!

);

ret=allocate_mem(ab);

//从空闲区分配内存,ret==1表示分配ok

if((ret==1)&

(allocated_block_head==NULL))//如果此时allocated_block_head尚未赋值,则赋值

{//进程分配链表为空

return1;

elseif(ret==1)//分配成功,将该已分配块的描述插入已分配链表

next=allocated_block_head;

//头插法

return2;

elseif(ret==-1)//分配不成功

printf("

Allocationfail\n"

free(ab);

return3;

//退出程序并释放内存空间。

intdo_exit()

structallocated_block*allocated_ab,*allocated_pre;

structfree_block_type*free_ab,*free_pre;

free_pre=free_block;

allocated_pre=allocated_block_head;

if(free_pre!

free_ab=free_pre->

while(free_ab!

free(free_pre);

free_pre=free_ab;

free_ab=free_ab->

if(allocated_pre!

allocated_ab=allocated_pre->

while(allocated_ab!

free(allocated_pre);

allocated_pre=allocated_ab;

allocated_ab=allocated_ab->

allocated_ab=allocated_ab->

//在进程分配链表中寻找指定进程。

structallocated_block*find_process(intpid)

structallocated_block*ab=allocated_block_head;

if(ab==NULL)

Here?

?

1\n"

returnNULL;

while(ab->

pid!

=pid&

ab=ab->

if(ab->

next==NULL&

=pid)

2222222\n"

returnab;

//显示当前内存的使用情况,包括空闲区的情况和已经分配的情况。

intdisplay_mem_usage()

structfree_block_type*fbt=free_block;

----------------------------------------------------------\n"

//显示空闲区

FreeMemory:

\n"

%20s%20s\n"

"

start_addr"

size"

while(fbt!

%20d%20d\n"

fbt->

start_addr,fbt->

fbt=fbt->

//显示已分配区

\nUsedMemory:

%10s%20s%10s%10s\n"

PID"

ProcessName"

start_addr"

while(ab!

%10d%20s%10d%10d\n"

pid,ab->

process_name,ab->

start_addr,ab->

//释放ab数据结构节点。

intdispose(structallocated_block*free_ab)

structallocated_block*pre,*ab;

if(free_block==NULL)

if(free_ab==allocated_block_head)//如果要释放第一个节点

allocated_block_head=allocated_block_head->

free(free_ab);

pre=allocated_block_head;

ab=allocated_block_head->

//找到free_ab

while(ab!

=free_ab)

pre=ab;

ab=ab->

pre->

next=ab->

/*将ab所表示的已分配区归还,并进行可能的合并*/

intfree_mem(structallocated_block*ab)

intalgorithm=ma_algorithm;

structfree_block_type*fbt,*pre,*work;

fbt=(structfree_block_type*)malloc(sizeof(structfree_block_type));

fbt)

pre=free_block;

fbt->

start_addr=ab->

size=ab->

if(pre!

{

while(pre->

pre=pre->

next=fbt;

free_blo

展开阅读全文
相关资源
猜你喜欢
相关搜索
资源标签

当前位置:首页 > PPT模板 > 商务科技

copyright@ 2008-2023 冰点文库 网站版权所有

经营许可证编号:鄂ICP备19020893号-2