动态页式存储管理的模拟实现C语言(共7页).doc
精选优质文档-倾情为你奉上在内存划出一块区域,并进行页面划分;设计请求页表;模拟页面分配;分别模拟“先进先出页面淘汰算法FIFO”、“最近最少使用页面淘汰算法LRU”和“理想型淘汰算法OPT”本程序随机产生请求序列,分别模拟FIFO,LRU,OPT三种算法。将结果保存在FIFO.txt,LRU.txt,OPT.txt三个文件中。程序代码:#include<stdio.h>#include<stdlib.h>#include<time.h>#define N 20#define P 3struct DuLNodeint data;struct DuLNode *prior;struct DuLNode *next;int pageFIFON+1;int front=0,rear=0;int pageingN+1,pmemP+1;int memcount=1;void init(int a,int T)int i;for(i=0;i<=T;i+)ai=-2;int insert_item(int item,int queue,int T)if(rear+1)%(T+1)=front)return 1;queuerear=item;rear=(rear+1)%(T+1);return 0;int remove_item(int *item,int queue,int T)if(front = rear)return 1;*item=queuefront;front=(front+1) % (T+1);return 0;int findif(int a,int b,int T)int i;for(i=1;i<=T;i+)if(ai=b)return i;return -1;void insertintomem(int a,int b,int n)if(memcount<=P)amemcount=b;memcount+;elsean=b;void initpage(int page)int temp,i;srand(unsigned)time(0);for(i=1;i<=N;i+)temp=rand()%10;pagei=temp;void addtoLink(struct DuLNode *p,int e)struct DuLNode *add;add=malloc(sizeof(struct DuLNode);add->data=e;add->prior=p->prior;p->prior->next=add;add->next=p;p->prior=add;int getI(struct DuLNode *p,int e)int i;struct DuLNode *cd=p;for(i=1;i+)cd=cd->next;if(cd->data=e)return i;if(cd=p)return -1;void deleLink(struct DuLNode *p,int i,int *e)int n;struct DuLNode *cd=p;for(n=1;n<=i;n+)cd=cd->next;*e=cd->data;cd->prior->next=cd->next;cd->next->prior=cd->prior;free(cd);void removebottom(struct DuLNode *p,int *e)struct DuLNode *cd=p->next;*e=cd->data;cd->next->prior=p;p->next=cd->next;free(cd);int getcount(int a,int b,int n,int T)int i;for(i=n;i<=T;i+)if(ai=b)return (i-n); return -1;void getreplacepage(int a,int b,int i,int *e)int t,cP+1,temp,T,count=0,errorP+1;for(t=1;t<=P;t+)if(getcount(a,bt,i,N)!=-1)ct=getcount(a,bt,i,N);elseerror+count=bt;if(count=0)temp=c1;T=b1;for(t=1;t<=P;t+)if(ct>temp)temp=ct;T=bt;*e=T;elsefor(t=1;t<=count;t+)ct=findif(a,errort,N);temp=c1;T=error1;for(t=1;t<=count;t+)if(ct<temp)temp=ct;T=errort;*e=T;void main()int i,temp,temp1,error=0,ErrorCP;FILE *fp1,*fp2,*fp3;struct DuLNode *p;p=(struct DuLNode *)malloc(sizeof(struct DuLNode);p->prior=p->next=p;initpage(pageing);init(pmem,P);if(fp1=fopen("FIFO.txt","a")=NULL)printf("不能打开文件!n");exit(1);for(i=1;i<=N;i+)fprintf(fp1," %d ",pageingi);fprintf(fp1,"n");for(i=1;i<=N;i+)if(memcount>P&&findif(pmem,pageingi,P)=-1)remove_item(&temp,pageFIFO,N);insertintomem(pmem,pageingi,findif(pmem,temp,P);insert_item(pageingi,pageFIFO,N);fprintf(fp1,"%d被引用,%d被替换->出现第 %d 次错误!n",pageingi,temp,+error);elseif(memcount<=P&&findif(pmem,pageingi,P)=-1)insertintomem(pmem,pageingi,memcount);insert_item(pageingi,pageFIFO,N);fprintf(fp1,"页中未满。%d被引用->出现第 %d 次错误!n",pageingi,+error);elsefprintf(fp1,"%d已在页中->未出现错误。n",pageingi);fclose(fp1);ErrorC0=error;memcount=1;error=0;init(pmem,P);if(fp2=fopen("LRU.txt","a")=NULL)printf("不能打开文件!n");exit(1);for(i=1;i<=N;i+)fprintf(fp2," %d ",pageingi);fprintf(fp2,"n");for(i=1;i<=N;i+)if(memcount>P&&findif(pmem,pageingi,P)=-1)removebottom(p,&temp);insertintomem(pmem,pageingi,findif(pmem,temp,P);if(getI(p,pageingi)!=-1)deleLink(p,getI(p,pageingi),&temp1);addtoLink(p,pageingi);fprintf(fp2,"%d被引用,%d被替换->出现第 %d 次错误!n",pageingi,temp,+error);elseif(memcount<=P&&findif(pmem,pageingi,P)=-1)insertintomem(pmem,pageingi,memcount);addtoLink(p,pageingi);fprintf(fp2,"页中未满。%d被引用->出现第 %d 次错误!n",p->prior->data,+error);elsedeleLink(p,getI(p,pageingi),&temp1);addtoLink(p,pageingi);fprintf(fp2,"%d已在页中->未出现错误。n",pageingi);fclose(fp2);ErrorC1=error;memcount=1;error=0;init(pmem,P);if(fp3=fopen("OPT.txt","a")=NULL)printf("不能打开文件!n");exit(1);for(i=1;i<=N;i+)fprintf(fp3," %d ",pageingi);fprintf(fp3,"n");for(i=1;i<=N;i+)if(memcount>P&&findif(pmem,pageingi,P)=-1)getreplacepage(pageing,pmem,i,&temp);insertintomem(pmem,pageingi,findif(pmem,temp,P);fprintf(fp3,"%d被引用,%d被替换->出现第 %d 次错误!n",pageingi,temp,+error);elseif(memcount<=P&&findif(pmem,pageingi,P)=-1)insertintomem(pmem,pageingi,memcount);fprintf(fp3,"页中未满。%d被引用->出现第 %d 次错误!n",pageingi,+error);elsefprintf(fp3,"%d已在页中->未出现错误。n",pageingi);ErrorC2=error;printf("对于引用串序列:");for(i=1;i<=N;i+)printf(" %d ",pageingi);printf("nFIFO算法出现 %d 次错误。n",ErrorC0);printf("LRU算法出现 %d 次错误。n",ErrorC1);printf("OPT算法出现 %d 次错误。n",ErrorC2);专心-专注-专业