操作系统页面置换算法代码.docx
- 文档编号:8987053
- 上传时间:2023-02-02
- 格式:DOCX
- 页数:18
- 大小:20.04KB
操作系统页面置换算法代码.docx
《操作系统页面置换算法代码.docx》由会员分享,可在线阅读,更多相关《操作系统页面置换算法代码.docx(18页珍藏版)》请在冰豆网上搜索。
操作系统页面置换算法代码
操作系统页面置换算法代码
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< } 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; }
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 操作系统 页面 置换 算法 代码
![提示](https://static.bdocx.com/images/bang_tan.gif)