#include<stdio.h> #include<stdlib.h> #include<windows.h> void Print(int bc[],int blockCount) { int i; for(i=0;i<blockCount;i++) { printf("%d ",bc[i]); } printf("\n"); } Travel(int bc[],int blockCount,int x) { int is_found=0; int i; for(i=0;i<blockCount;i++) { if(bc[i]==x) { is_found=1; break; } } return is_found; } void FIFO(int pc[],int bc[],int pageCount,int blockCount) { int i; printf("0:FIFO置换算法\n"); if(pageCount<=blockCount) { printf("缺页次数为0\n"); printf("缺页率为0\n"); } else { int noPage=0; int p=0; for(i=0;i<pageCount;i++) { //printf("引用页:%d\n",pc[i]); if(!Travel(bc,blockCount,pc[i])) { if(i<blockCount) { bc[i]=pc[i]; } else { if(p==blockCount) { p=0; } bc[p]=pc[i]; p++; } noPage++; //printf("物理块情况:\n"); //Print(bc,blockCount); } //printf("\n"); } printf("FIFO缺页次数为:%d\n",noPage); printf("FIFO缺页率为:%.2f%%\n",(float)noPage/pageCount*100); } } int FoundMaxNum(int a[],int n) { int k,j,i; k=a[0]; j=0; for (i=0;i<n;i++) { if(a[i]>=k) { k=a[i]; j=i; } } return j; } void LRU(int pc[],int bc[],int pageCount,int blockCount) { printf("1:LRU置换算法\n"); if(pageCount<=blockCount) { printf("缺页次数为0\n"); printf("缺页率为0\n"); } else { int noPage=0; int i,j,m,p,k; int bc1[100]; for(i=0;i<blockCount;i++) { bc1[i]=0; } for(i=0;i<pageCount;i++) { // printf("引用页:%d\n",pc[i]); if(!Travel(bc,blockCount,pc[i])) { if(i<blockCount) { bc[i]=pc[i]; for(p=0;p<=i;p++) { bc1[p]++; } } else { for(j=0;j<blockCount;j++) { bc1[j]++; } k=FoundMaxNum(bc1,blockCount); bc[k]=pc[i]; bc1[k]=1; } noPage++; //printf("物理快情况:\n"); //Print(bc,blockCount); } else if(Travel(bc,blockCount,pc[i])) { if(i<blockCount) { for(j=0;j<=i;j++) { bc1[j]++; } for(m=0;m<=i;m++) { if(bc[m]==pc[i]) { break; } } bc1[m]=1; bc[m]=pc[i]; } else { for(j=0;j<blockCount;j++) { bc1[j]++; } for(m=0;m<blockCount;m++) { if(bc[m]==pc[i]) { break; } } bc1[m]=1; bc[m]=pc[i]; } } //printf("\n"); } printf("LRU缺页次数为:%d\n",noPage); printf("LRU缺页率为:%.2f%%\n",(float)noPage/pageCount*100); } } void Optiomal(int pc[],int bc[],int pageCount,int blockCount) { printf("2:最佳置换算法\n"); if(pageCount<=blockCount) { printf("缺页次数为0\n"); printf("缺页率为0\n"); } else { int noPage=0; int i,j,k; for(i=0;i<pageCount;i++) { // printf("引用页:%d\n",pc[i]); if(!Travel(bc,blockCount,pc[i])) { if(i<blockCount) { bc[i]=pc[i]; } else { int max=0; int blockIndex; for(j=0;j<blockCount;j++) { for(k=i;k<pageCount;k++) { if(bc[j]==pc[k]) { break; } } if(k>=max) { max=k; blockIndex=j; } } bc[blockIndex]=pc[i]; } noPage++; //printf("物理快情况:\n"); //Print(bc,blockCount); } //printf("\n"); } printf("OPT缺页次数为:%d\n",noPage); printf("OPT缺页率为:%.2f%%\n",(float)noPage/pageCount*100); } } int main() { int bc1[100]; int bc2[100]; int bc3[100]; int pageCount,blockCount,i,pc[100]; system("color b"); printf("输入页面数\n"); scanf("%d",&pageCount); printf("输入页面走向\n"); for(i=0;i<pageCount;i++) { scanf("%d",&pc[i]); } blockCount=3;//物理块数 printf("\n"); FIFO(pc,bc1,pageCount,blockCount); printf("\n"); LRU(pc,bc2,pageCount,blockCount); printf("\n"); Optiomal(pc,bc3,pageCount,blockCount); return 0; }