#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int mms;
int ps;
int rp;
struct node{
int *pf;
int *vp;
} *pt=NULL;
void enter_params(){
printf("Enter main menmory size(words):_\n");
scanf("%d",&mms);
printf("Enter page size (word/page):_\n");
scanf("%d",&ps);
printf("Enter replacement policy(0=LRU, 1=FIFO):_\n");
scanf("%d",&rp);
}
void mapVirtualAddress(){
int VA;
int offset;
int tag;
int numEntries;
int k;
int pa;
int i;
i=0;
printf("Enter virtual memory address to access:_\n");
scanf("%d",&VA);
tag = VA/ps;
offset = VA%ps;
while((i<numEntries)&&(pt[i].vp!=vp)&&(pt[i].vp!=-1))
i++;
if(i==numEntries){
pf = pt[0].pf;
for(k=0; k<numEntries-2; i++)
pt[k] = pt[k+1];
pt[numEntries-1].vp = vp;
pt[numEntries-1]pf = pf;
printf("Page fault!");
}
else if(pt[i].vp==-1){
pt[i].vp = vp;
pt[i].pf = i;
printf("Page fault!");
}
else{
pa = pt[i].pf*ps+offset;
}
if(rp == 1){
printf("Do nothing");
}
else{
while((k<numEntries-1)&&(pt[i+1].vp!=-1)){
pt[k] = pt[k+1];
k++;
}
}
}
void printPT(){
int i;
pt[i].vp;
pt[i].pf;
while((pt[i].vp!=-1)&&(i<pt[i].vp)){
printf("VP %d --> PF %d;",vp,pf);
i++;
}
}
问问题
133 次