操作系统页面置换算法代码.docx

上传人:b****8 文档编号:8987053 上传时间:2023-02-02 格式:DOCX 页数:18 大小:20.04KB
下载 相关 举报
操作系统页面置换算法代码.docx_第1页
第1页 / 共18页
操作系统页面置换算法代码.docx_第2页
第2页 / 共18页
操作系统页面置换算法代码.docx_第3页
第3页 / 共18页
操作系统页面置换算法代码.docx_第4页
第4页 / 共18页
操作系统页面置换算法代码.docx_第5页
第5页 / 共18页
点击查看更多>>
下载资源
资源描述

操作系统页面置换算法代码.docx

《操作系统页面置换算法代码.docx》由会员分享,可在线阅读,更多相关《操作系统页面置换算法代码.docx(18页珍藏版)》请在冰豆网上搜索。

操作系统页面置换算法代码.docx

操作系统页面置换算法代码

操作系统页面置换算法代码

2007年01月20日星期六下午12:

53

#include

#include

#include

#defineTRUE1

#defineFALSE0

#defineINVALID-1

#defineNUL0

#definetotal_instruction320/*指令流长*/

#definetotal_vp32/*虚页长*/

#defineclear_period50/*清零周期*/

typedefstruct{/*页面结构*/

intpn,pfn,counter,time;

}pl_type;

pl_typepl[total_vp];/*页面结构数组*/

structpfc_struct{/*页面控制结构*/

intpn,pfn;

structpfc_struct*next;

};

typedefstructpfc_structpfc_type;

pfc_typepfc[total_vp],*freepf_head,*busypf_head,*busypf_tail;

intdiseffect,a[total_instruction];

intpage[total_instruction],offset[total_instruction];

voidinitialize();

voidFIFO();

voidLRU();

voidNUR();

intmain()

{

intS,i;

srand((int)getpid());

S=(int)rand()%390;

for(i=0;i

{

a[i]=S;/*任选一指令访问点*/

a[i+1]=a[i]+1;/*顺序执行一条指令*/

a[i+2]=(int)rand()%390;/*执行前地址指令m’*/

a[i+3]=a[i+2]+1;/*执行后地址指令*/

S=(int)rand()%390;

}

for(i=0;i

{

page[i]=a[i]/10;

offset[i]=a[i]%10;

}

for(i=4;i<=32;i++)/*用户内存工作区从4个页面到32个页面*/

{

printf("%2dpageframes",i);

FIFO(i);

LRU(i);

NUR(i);

printf("\n");

}

return0;

}

voidFIFO(total_pf)/*FIFO(FirstinFirstout)ALGORITHM*/

inttotal_pf;/*用户进程的内存页面数*/

{

inti;

pfc_type*p,*t;

initialize(total_pf);/*初始化相关页面控制用数据结构*/

busypf_head=busypf_tail=NUL;/*忙页面队列头,对列尾链接*/

for(i=0;i

{

if(pl[page[i]].pfn==INVALID)/*页面失效*/

{

diseffect+=1;/*失效次数*/

if(freepf_head==NUL)/*无空闲页面*/

{

p=busypf_head->next;

pl[busypf_head->pn].pfn=INVALID;/*释放忙页面队列中的第一个页面*/

freepf_head=busypf_head;

freepf_head->next=NUL;

busypf_head=p;

}

p=freepf_head->next;/*按方式调新页面入内存页面*/

freepf_head->next=NUL;

freepf_head->pn=page[i];

pl[page[i]].pfn=freepf_head->pfn;

if(busypf_tail==NUL)

busypf_head=busypf_tail=freepf_head;

else

{

busypf_tail->next=freepf_head;

busypf_tail=freepf_head;

}

freepf_head=p;

}

}

printf("FIFO:

%6.4F",1-(float)diseffect/320);

}

voidLRU(total_pf)

inttotal_pf;

{

intmin,minj,i,j,present_time;

initialize(total_pf);present_time=0;

for(i=0;i

{

if(pl[page[i]].pfn==INVALID)/*页面失效*/

{

diseffect++;

if(freepf_head==NUL)/*无空闲页面*/

{

min=32767;

for(j=0;j

if(min>pl[j].time&&pl[j].pfn!

=INVALID)

{

min=pl[j].time;

minj=j;

}

freepf_head=&pfc[pl[minj].pfn];

pl[minj].pfn=INVALID;

pl[minj].time=-1;

freepf_head->next=NUL;

}

pl[page[i]].pfn=freepf_head->pfn;

pl[page[i]].time=present_time;

freepf_head=freepf_head->next;

}

else

pl[page[i]].time=present_time;

present_time++;

}

printf("LRU:

%6.4f",1-(float)diseffect/320);

}

voidNUR(total_pf)

inttotal_pf;

{

inti,j,dp,cont_flag,old_dp;

pfc_type*t;

initialize(total_pf);

dp=0;

for(i=0;i

{

if(pl[page[i]].pfn==INVALID)/*页面失效*/

{

diseffect++;

if(freepf_head==NUL)/*无空闲页面*/

{

cont_flag=TRUE;old_dp=dp;

while(cont_flag)

if(pl[dp].counter==0&&pl[dp].pfn!

=INVALID)

cont_flag=FALSE;

else

{

dp++;

if(dp==total_vp)

dp=0;

if(dp==old_dp)

for(j=0;j

pl[j].counter=0;

}

freepf_head=&pfc[pl[dp].pfn];

pl[dp].pfn=INVALID;

freepf_head->next=NUL;

}

pl[page[i]].pfn=freepf_head->pfn;

freepf_head=freepf_head->next;

}

else

pl[page[i]].counter=1;

if(i%clear_period==0)

for(j=0;j

pl[j].counter=0;

}

printf("NUR:

%6.4f",1-(float)diseffect/320);

}

voidinitialize(total_pf)/*初始化相关数据结构*/

inttotal_pf;/*用户进程的内存页面数*/

{

inti;

diseffect=0;

for(i=0;i

{

pl[i].pn=i;pl[i].pfn=INVALID;/*置页面控制结构中的页号,页面为空*/

pl[i].counter=0;pl[i].time=-1;/*页面控制结构中的访问次数为0,时间为-1*/

}

for(i=1;i

{

pfc[i-1].next=&pfc[i];pfc[i-1].pfn=i-1;/*建立pfc[i-1]和pfc[i]之间的连接*/

}

pfc[total_pf-1].next=NUL;pfc[total_pf-1].pfn=total_pf-1;

freepf_head=&pfc[0];/*页面队列的头指针为pfc[0]*/

}

模拟LRU页面置换算法

2010/02/0116:

33

#include

#include

#definePAGENUM6/*主存中允许的最大的页数*/

#defineMAXPAGENUM15/*一个程序包含的最大页数*/

/*定义栈的结构即置换器*/

typedefstructstack

{

intpage[PAGENUM];

inthead;

}PAGER;

/*定义一个置换器*/

PAGERpager;

/*栈的初始化*/

voidInitPager()

{

inttop=0;

inti,j,tmp;

intr;

randomize();

r=random(MAXPAGENUM);

pager.page[top++]=r;

while(top

{

randomize();

r=random(MAXPAGENUM);

for(j=0;j

{

if(r==pager.page[j])

{

break;

}

if(j==top-1)

pager.page[top++]=r;

}

}

pager.head=PAGENUM-1;

}

/*栈中成员的移动,即移动页面的操作*/

voidMovingPage(intBegin)

{

inti;

for(i=Begin;i

{

pager.page[i]=pager.page[i+1];

}

}

/*出栈操作,即交换出页面的操作*/

voidPopPage(intp)

{

MovingPage(p);

pager.head=PAGENUM-2;

}

/*入栈操作,即换进新页面的操作*/

voidPushPage(intpage)

{

pager.head=PAGENUM-1;

pager.page[pager.head]=page;

}

/*打印主存中的页面*/

voidprintPager()

{

inti;

printf("Thepagesinthepageris:

");

for(i=0;i

{

printf("%d",pager.page[i]);

}

printf("\n");

}

/*处理页面的换进和换出*/

voidPagingProcess()

{

intrdm;

inti;

randomize();

rdm=random(MAXPAGENUM);

for(i=0;i

{

if(rdm==pager.page[i])

{

printPager();

printf("Thepage[%d]willbeaccessed\n",rdm);

PopPage(i);

PushPage(rdm);

printPager();

break;

}

if(i==PAGENUM-1)

{

printPager();

printf("Thepage[0]willbepagedout\n");

PopPage(0);

printf("Thepage[%d]willbepagedin\n",rdm);

PushPage(rdm);

printPager();

}

}

}

intmain()

{

inti;

clrscr();

for(i=0;i<3;i++)

{

InitPager();

printf("ROUND%d:

\n\n",i+1);

PagingProcess();

}

getch();

return0;

页面调度算法(FIFO,LRU,OPT)

2006-11-2219:

29

#include

#include"stdlib.h"

typedefintQElemType;

#defineok1

#defineoverflow0

#defineerror0

typedefstructQnode{

QElemTypedata;

structQnode*next;

}Qnode,*Queueptr;

typedefstruct{

Queueptrfront;

Queueptrrear;

}LinkQueue;

InitQueue(LinkQueue&Q){

Q.front=Q.rear=(Queueptr)malloc(sizeof(Qnode));

if(!

Q.front)exit(overflow);

Q.front->next=NULL;

returnok;

}

EnQueue(LinkQueue&Q,QElemTypee){

Queueptrp;

p=(Queueptr)malloc(sizeof(Qnode));

if(!

p)exit(overflow);

p->data=e;p->next=NULL;

Q.rear->next=p;

Q.rear=p;

returnok;

}

DeQueue(LinkQueue&Q,QElemType&e){

Queueptrp;

if(Q.front==Q.rear)returnerror;

p=Q.front->next;

e=p->data;

Q.front->next=p->next;

if(Q.rear==p)Q.rear=Q.front;

free(p);

returnok;

}

voidVisitQueue(LinkQueueQ){

Queueptrp;

p=Q.front->next;

while(p){cout<data<<"";p=p->next;}

}

CompareQueue(LinkQueueQ,intpage){

Queueptrp;

p=Q.front->next;

while(p){

if(p->data==page)returnok;

p=p->next;

}

returnerror;

}

voidFIFO(int*a,intn,intm){

LinkQueueQ;

intpage,t=0,flag=0,x;

InitQueue(Q);

for(inti=1;i<=m;i++)

{

page=a[i];

t++;

if(t<=n){EnQueue(Q,page);

cout<

else{

for(intj=1;j<=n;j++)

if(CompareQueue(Q,page)){t--;flag=1;break;}

if(flag==0){DeQueue(Q,x);

cout<

<

EnQueue(Q,page);

}

cout<

VisitQueue(Q);

cout<<":

";

}

flag=0;

}

cout<<"缺页中断数为:

"<

cout<<"t="<

}

voidLRU(int*a,intn,intm){

LinkQueueQ;intpage,t=0,flag=0,x,e;

InitQueue(Q);

for(inti=1;i<=m;i++)

{

page=a[i];t++;

if(t<=n){EnQueue(Q,page);cout<

else{for(intj=1;j<=n;j++)

if(CompareQueue(Q,page)){t--;DeQueue(Q,e);EnQueue(Q,e);flag=1;break;}

if(flag==0){DeQueue(Q,x);

cout<

EnQueue(Q,page);

}

cout<

VisitQueue(Q);

cout<<":

";

}

flag=0;

}

cout<<"缺页中断数为:

"<

cout<<"t="<

}

intmax(int*t,intn){

intmax=t[1],s=1;

for(inti=1;i<=n;i++)

if(t[i]>max){max=t[i];s=i;}

returns;

}

voidOPT(inta[21],intn,intm){

intw=0,flag=0;

int*t=newint[n+1];

for(inti=1;i<=m;i++)

{

w++;

if(w<=n)cout<

else{

for(intq=1;q<=n;q++)

if(a[i]==a[q]){w--;flag=1;break;}

if(flag==0)

{

for(intj=1;j<=n;j++)

for(intk=i;k<=m;k++)

{

if(a[j]!

=a[k])t[j]++;

elsebreak;

}

cout<

<<""<

a[max(t,n)]=a[i];

}

cout<

for(ints=1;s<=n;s++)

cout<

cout<<":

";

}

for(intr=1;r<=n;r++)t[r]=0;

flag=0;

}

cout<<"缺页中断数为:

"<

cout<<"w="<

delete[]a;

}

main()

{

intm,n;

cout<<"输入页面数:

"<

cin>>m;

int*a=newint[m+1];

cout<<"输入驻留集大小:

"<

cin>>n;

cout<<"inputthepages"<

for(inti=1;i<=m;i++)

cin>>a[i];

cout<

cout<<"TheresultofFIFO:

"<

FIFO(a,n,m);

cout<

cout<<"TheresultofLRU:

"<

LRU(a,n,m);

cout<

cout<<"TheresultofOPT:

"<

OPT(a,n,m);

cout<

return0;

delete[]a;

}

模拟FIFO页面调度算法处理缺页中断

2009-11-2816:

14

模拟FIFO页面调度算法处理缺页中断

#include

#include

classList;

classitem

{

public:

friendclassList;

private:

item(intp=0,ints=0,intb=0,intl=0,intc=0){page=p;sign=s;block=b;locate=l;changesign=c;next=NULL;}

item*next;

intpage;

intsign;

intchangesign;

intblock;

intlocate;

};

classList

{

public:

List(intp=0,ints=0,intb=0,intl=0,intc=0){list=newitem(p,s,b,l,c);}

intcheck(intp);

intinsert(intp,ints,intb,intl);

void

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

当前位置:首页 > IT计算机 > 计算机硬件及网络

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

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