logo资料库

动态页式存储管理的模拟实现C语言.doc

第1页 / 共7页
第2页 / 共7页
第3页 / 共7页
第4页 / 共7页
第5页 / 共7页
第6页 / 共7页
第7页 / 共7页
资料共7页,全文预览结束
在内存划出一块区域,并进行页面划分;设计请求页表;模拟页面分配;分 别模拟“先进先出页面淘汰算法 FIFO”、“最近最少使用页面淘汰算法 LRU”和“理 想型淘汰算法 OPT” 本程序随机产生请求序列,分别模拟 FIFO,LRU,OPT 三种算法。将结果保存 在 FIFO.txt,LRU.txt,OPT.txt 三个文件中。 程序代码: #include #include #include #define N 20 #define P 3 struct DuLNode{ int data; struct DuLNode *prior; struct DuLNode *next; }; int pageFIFO[N+1]; int front=0,rear=0; int pageing[N+1],pmem[P+1]; int memcount=1; void init(int a[],int T) { int i; for(i=0;i<=T;i++) a[i]=-2; } int insert_item(int item,int queue[],int T) { if((rear+1)%(T+1)==front) return 1; queue[rear]=item; rear=(rear+1)%(T+1); return 0; } int remove_item(int *item,int queue[],int T) { if(front == rear) return 1; *item=queue[front]; front=(front+1) % (T+1);
return 0; } int findif(int a[],int b,int T) { int i; for(i=1;i<=T;i++) { if(a[i]==b) return i; } return -1; } void insertintomem(int a[],int b,int n) { if(memcount<=P) { a[memcount]=b; memcount++; } else a[n]=b; } void initpage(int page[]) { int temp,i; srand((unsigned)time(0)); for(i=1;i<=N;i++) { temp=rand()%10; page[i]=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(a[i]==b) return (i-n); } return -1; } void getreplacepage(int a[],int b[],int i,int *e) { int t,c[P+1],temp,T,count=0,error[P+1]; for(t=1;t<=P;t++)
{ if(getcount(a,b[t],i,N)!=-1) c[t]=getcount(a,b[t],i,N); else { } error[++count]=b[t]; } if(count==0) { temp=c[1]; T=b[1]; for(t=1;t<=P;t++) { if(c[t]>temp) { temp=c[t]; T=b[t]; } } *e=T; } else { for(t=1;t<=count;t++) { c[t]=findif(a,error[t],N); } temp=c[1]; T=error[1]; for(t=1;t<=count;t++) { if(c[t]
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 ",pageing[i]); } fprintf(fp1,"\n"); for(i=1;i<=N;i++) { if(memcount>P&&findif(pmem,pageing[i],P)==-1) { remove_item(&temp,pageFIFO,N); insertintomem(pmem,pageing[i],findif(pmem,temp,P)); insert_item(pageing[i],pageFIFO,N); fprintf(fp1,"%d 被 引 用 , %d 被 替 换 -> 出 现 第 %d 次 错 误 ! \n",pageing[i],temp,++error); } else { if(memcount<=P&&findif(pmem,pageing[i],P)==-1) { insertintomem(pmem,pageing[i],memcount); insert_item(pageing[i],pageFIFO,N); fprintf(fp1," 页 中 未 满 。 %d 被 引 用 -> 出 现 第 %d 次 错 误 ! \n",pageing[i],++error); } else fprintf(fp1,"%d 已在页中->未出现错误。\n",pageing[i]); } } fclose(fp1); ErrorC[0]=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 ",pageing[i]); } fprintf(fp2,"\n"); for(i=1;i<=N;i++) { if(memcount>P&&findif(pmem,pageing[i],P)==-1) { removebottom(p,&temp); insertintomem(pmem,pageing[i],findif(pmem,temp,P)); if(getI(p,pageing[i])!=-1) { deleLink(p,getI(p,pageing[i]),&temp1); } addtoLink(p,pageing[i]); fprintf(fp2,"%d 被 引 用 , %d 被 替 换 -> 出 现 第 %d 次 错 误 ! \n",pageing[i],temp,++error); } else { if(memcount<=P&&findif(pmem,pageing[i],P)==-1) { insertintomem(pmem,pageing[i],memcount); addtoLink(p,pageing[i]); fprintf(fp2," 页 中 未 满 。 %d 被 引 用 -> 出 现 第 %d 次 错 误 ! \n",p->prior->data,++error); } else { deleLink(p,getI(p,pageing[i]),&temp1); addtoLink(p,pageing[i]); fprintf(fp2,"%d 已在页中->未出现错误。\n",pageing[i]); } } } fclose(fp2); ErrorC[1]=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 ",pageing[i]); } fprintf(fp3,"\n"); for(i=1;i<=N;i++) { if(memcount>P&&findif(pmem,pageing[i],P)==-1) { getreplacepage(pageing,pmem,i,&temp); insertintomem(pmem,pageing[i],findif(pmem,temp,P)); fprintf(fp3,"%d 被 引 用 , %d 被 替 换 -> 出 现 第 %d 次 错 误 ! \n",pageing[i],temp,++error); } else { if(memcount<=P&&findif(pmem,pageing[i],P)==-1) { insertintomem(pmem,pageing[i],memcount); fprintf(fp3," 页 中 未 满 。 %d 被 引 用 -> 出 现 第 %d 次 错 误 ! \n",pageing[i],++error); } else } fprintf(fp3,"%d 已在页中->未出现错误。\n",pageing[i]); } ErrorC[2]=error; printf("对于引用串序列:"); for(i=1;i<=N;i++) { printf(" %d ",pageing[i]); } printf("\nFIFO 算法出现 %d 次错误。\n",ErrorC[0]); printf("LRU 算法出现 %d 次错误。\n",ErrorC[1]); printf("OPT 算法出现 %d 次错误。\n",ErrorC[2]); }
分享到:
收藏