linux操作标准系统实验三CB.docx

上传人:b****5 文档编号:7680129 上传时间:2023-01-25 格式:DOCX 页数:18 大小:1.62MB
下载 相关 举报
linux操作标准系统实验三CB.docx_第1页
第1页 / 共18页
linux操作标准系统实验三CB.docx_第2页
第2页 / 共18页
linux操作标准系统实验三CB.docx_第3页
第3页 / 共18页
linux操作标准系统实验三CB.docx_第4页
第4页 / 共18页
linux操作标准系统实验三CB.docx_第5页
第5页 / 共18页
点击查看更多>>
下载资源
资源描述

linux操作标准系统实验三CB.docx

《linux操作标准系统实验三CB.docx》由会员分享,可在线阅读,更多相关《linux操作标准系统实验三CB.docx(18页珍藏版)》请在冰豆网上搜索。

linux操作标准系统实验三CB.docx

linux操作标准系统实验三CB

电子信息学院

实验报告书

课程名:

《Linux操作系统实验》

题目:

实验三存储管理实验

实验类别【验证】

班级:

BX0907

学号:

24

姓名:

朱杰

评语:

实验态度:

认真()一般()差()

实验结果:

正确()部分正确()错()

实验理论:

掌握()熟悉()了解()不懂()

操作技能:

强()一般()差()

实验报告:

好()一般()差()

成绩:

指导教师:

胡静

批阅时间:

年月日

成绩:

指导教师:

宁建红

批阅时间:

年月日

 

1、实验内容或题目

(1)模拟初始内存页面分配(数组、结构体均可)

(2)实现Buddyheap算法

(3)通过键盘输入随机产生申请和释放操作

∙请求:

r8代表申请8个页面。

∙释放:

f4代表释放4个页面。

注意:

heap的分割和合并操作。

(4)每个申请或释放操作,都在屏幕上显示操作前与操作后的内存分配的对比图。

(5)实验假设申请和释放的页数都是2的整次幂。

(1)建立工作集页面模型。

(2)利用随机函数动态生成进程访问页面的序列号。

(3)实现FIFO页面淘汰算法。

(4)实现页故障率反馈模型。

2、实验目的与要求

(1)用C语言是实现模拟Linux系统中连续内存分配用到的伙伴对算法。

(2)通过链表的形式输出在内存申请和释放过程中内存状态的对比图。

(1)了解工作集模型的原理及其特点。

(2)实现页故障率反馈模型。

3、实验步骤与源程序

1.Buddyheap算法模拟

源程序。

#include

#include

typedefstructblock//定义一个内存块的结构体

{

intsize。

//块的大小

intstart。

//块的起始位置

intloc。

//是否占用

structblock*next。

//指向下一个块的指针

structblock*prior。

//指向前一个块的指针

}block。

intmaxsize=512。

//可分配的内存空间总量

block*note。

//初始化的结点

block*id[10]。

//链表头的数组

//打印内存状态函数

voidprintmem(){

inti。

for(i=9。

i>=0。

i--){

printf("%d->",i)。

block*temp=(structblock*)malloc(sizeof(structblock))。

temp=id[i]->next。

while(temp!

=NULL){

printf("%d(%s)(%d)->",temp->size,temp->loc==1?

"占用":

"空闲",temp->start)。

//输出内存块的大小、状态、起始位置

temp=temp->next。

}

printf("\n")。

}

}

//初始化

voidinit(){

inti。

for(i=0。

i<9。

i++){

id[i]=(structblock*)malloc(sizeof(structblock))。

id[i]->prior=id[i]。

id[i]->next=NULL。

}

note=(structblock*)malloc(sizeof(structblock))。

note->size=maxsize。

note->start=0。

note->loc=0。

note->next=NULL。

id[9]=(structblock*)malloc(sizeof(structblock))。

id[9]->next=note。

id[9]->prior=id[9]。

note->prior=id[9]。

printmem()。

}

intpower(intx,inty){//计算x的y次幂

intk=0,tmp=1。

for(。

k

k++){

tmp=tmp*x。

}

returntmp。

}

introot(intx,inty){//计算y的开x次方

intresult=y,count=0。

while(result!

=1){

result=result/x。

count++。

}

returncount。

}

//对内存块进行拆分

intsplit(inttempId){

block*pend=(structblock*)malloc(sizeof(structblock))。

//被拆分结点

block*cend=(structblock*)malloc(sizeof(structblock))。

//挂载点

block*newf=(structblock*)malloc(sizeof(structblock))。

//拆分后的第二个结点

block*newu=(structblock*)malloc(sizeof(structblock))。

//拆分后的第一个结点

pend=id[tempId]->next。

intflag=0,isFirst=0。

while(pend!

=NULL){

if(pend->loc==0){//可拆分

//卸载被拆分结点

if(isFirst==0){

id[tempId]->next=pend->next。

}else{

pend->prior->next=pend->next。

}

//拆分后的结点初始化

intsize=(pend->size)/2。

intstart=pend->start。

newu->size=size。

newu->start=start。

newf->start=start+size。

newu->loc=0。

newf->size=size。

newf->loc=0。

newf->prior=newu。

newu->next=newf。

newf->next=NULL。

tempId--。

cend=id[tempId]。

while(cend->next!

=NULL){

cend=cend->next。

}

cend->next=newu。

//将拆分后的结点进行挂载

newu->prior=cend。

flag=1。

return1。

}else{

pend=pend->next。

isFirst++。

}

}

if(flag==0){

tempId=tempId+1。

if(tempId<=9){

free(pend)。

free(cend)。

free(newu)。

free(newf)。

split(tempId)。

}else{

return-1。

}

}

}

//归并

intmerge(inttempId,block*first){

block*merger=(structblock*)malloc(sizeof(structblock))。

//合并后的结点

block*second=NULL。

//查找可合并的结点

second=id[tempId]->next。

intnextStart=first->start+first->size。

intpreStart=first->start-first->size。

intflag=0,isFirst=0。

while(second!

=NULL){

if((second->start==nextStart||second->start==preStart)&&second->loc==0){

//初始化合并后的结点

merger->size=(first->size)+(second->size)。

merger->loc=0。

merger->start=(first->start)<(second->start)?

(first->start):

(second->start)。

//卸载可合并的结点

if(first->next!

=NULL){

first->next->prior=first->prior。

}

if((first->prior->prior)==first->prior){

id[tempId]->next=first->next。

}else{

first->prior->next=first->next。

}

if(second->next!

=NULL){

second->next->prior=second->prior。

}

if(isFirst==0){

id[tempId]->next=second->next。

}else{

second->prior->next=second->next。

}

//挂载合并后的结点

tempId++。

merger->next=id[tempId]->next。

merger->prior=id[tempId]。

if(id[tempId]->next!

=NULL)id[tempId]->next->prior=merger。

id[tempId]->next=merger。

if(tempId<9){

merge(tempId,merger)。

}else{

return0。

}

return1。

}else{

second=second->next。

isFirst++。

}

}

return1。

}

//内存释放

intfreeb(intsize){

block*first=(structblock*)malloc(sizeof(structblock))。

inttempId=root(2,size)。

first=id[tempId]->next。

intflag=0。

while(first!

=NULL){

if(first->loc==1){

first->loc=0。

flag=1。

break。

}else{

first=first->next。

}

}

if(flag==1){

merge(tempId,first)。

//归并

printmem()。

}else{

printf("需要释放的内存块不存在!

\n")。

}

return1。

}

intrequestb(intsize){//申请size个页面

block*temp=(structblock*)malloc(sizeof(structblock))。

inttempId=root(2,size)。

intflag=0。

temp=id[tempId]->next。

while(temp!

=NULL){

if(temp->loc==0&&temp->size==size){//分配

temp->loc=1。

flag=1。

printf("分配成功!

\n")。

printmem()。

return1。

}else{

temp=temp->next。

}

}

if(flag==0){

tempId++。

if(tempId<=9){

intrs=split(tempId)。

if(rs==-1){

printf("没有合适的空间可分配!

\n")。

return-1。

}else{

requestb(size)。

}

}else{

printf("没有合适的空间可分配!

\n")。

return-1。

}

}

free(temp)。

}

intmain(){

init()。

intflag=1。

//是否继续

intsize。

//申请/释放内存大小(2的次幂)

charorder。

//操作命令,r申请,f释放

do{

printf("请输入命令:

(以空格相隔,示例:

r8)\n")。

scanf("%c%d",&order,&size)。

if(order=='r'){//申请内存

requestb(size)。

}elseif(order=='f'){//释放内存

freeb(size)。

}else{

printf("error!

")。

}

printf("是否继续?

(1继续,0退出):

")。

scanf("%d",&flag)。

getchar()。

}while(flag==1)。

}

结果图:

2.页故障率反馈模型

源程序。

#include

#include

#include

#defineMAX_WORKSET10

#defineWINDOW_SIZE20

intmempage=10。

//内存页面总数(进程要访问的页面从0到该范围内随机生成)

intprocArray[WINDOW_SIZE]。

//进程访问页面序列

intwin[MAX_WORKSET][2]。

//工作集

doublemaxRate=0.8,minRate=0.2。

//故障率上下限

doublecurRate。

intcur_workset=3。

intconflictCount=0。

//故障次数

voidprint(){//程序输出函数

curRate=(double)conflictCount/(double)WINDOW_SIZE。

printf("缺页故障率:

%g,故障率上限/下限:

%g/%g\n",curRate,maxRate,minRate)。

}

voidchangeArray(){//动态改变访问页面序列

inti。

for(i=0。

i

i++){

procArray[i]=rand()%mempage。

}

printf("进程调用页面序列:

")。

for(i=0。

i

i++){

printf("%d|",procArray[i])。

}

printf("\n")。

}

voidinit(){//初始化工作集和页面序列

inti,j。

//changeArray()。

for(i=0。

i

i++){

win[i][0]=-1。

win[i][1]=cur_workset。

}

}

voidchangePage(intnumber){//换页

inti,flag=0。

for(i=1。

i

i++){

if(win[flag][1]<=win[i][1]){//寻找最先进入的

flag=i。

}

}

win[flag][0]=procArray[number]。

//换页

win[flag][1]=1。

conflictCount++。

//冲突次数加1

for(i=0。

i

i++){

if(i!

=flag&&win[i][1]!

=-1){//工作集进入顺序处理

win[i][1]++。

}

}

}

voidstep(intnumber){//调用一次内存页面

inti,hit=0。

for(i=0。

i

i++){

if(procArray[number]==win[i][0]){//命中

//number++。

hit=1。

break。

}

}

if(hit==0){//尚未命中

changePage(number)。

//换页

//number++。

}

//returnnumber。

}

voidrun(){//程序运行

inti。

conflictCount=0。

changeArray()。

for(i=0。

i

i++){

step(i)。

}

printf("冲突次数:

%d,",conflictCount)。

}

voidfeedback(){

curRate=(double)conflictCount/(double)WINDOW_SIZE。

if(curRate>maxRate){

cur_workset++。

}elseif(curRate

cur_workset--。

}

}

intmain(){

init()。

//初始化

charquit。

do{

run()。

print()。

feedback()。

printf("输入任意字符继续,q退出\n")。

scanf("%c",&quit)。

getchar()。

}while(quit!

='q')。

}

结果图:

4、结果分析与实验体会

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

当前位置:首页 > 自然科学 > 物理

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1