-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathvmm.c
95 lines (77 loc) · 2.09 KB
/
vmm.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#include <stdio.h>
#include <stdint.h>
#include<stdlib.h>
#define PAGEMASK 0xFF00
#define OFFSETMASK 0x00FF
int **physicalMemory;
int *pageTable;
int lastFrameNumber = -1;
FILE *logicalAddressStream;
FILE *backingStore;
int numberOfPageFaults=0;
unsigned short int extractPageNumber(uint16_t num){
return (num & PAGEMASK) >> 8;
}
unsigned short int extractOffset(uint16_t num){
return num & OFFSETMASK;
}
int retrieveFromStore(int pNo){
int *buffer = (int*)malloc(256);
int seekCheck = fseek(backingStore,pNo*256,SEEK_SET);
if(seekCheck == 0){
fread(buffer,1,256,backingStore);
lastFrameNumber++;
for(int i=0;i<256;i++){
physicalMemory[lastFrameNumber][i] = buffer[i];
}
return lastFrameNumber;
}
else{
printf("Seek error\n");
}
return -1;
}
int getFrame(int logicalAddress){
int frameNumber = -1;
int pNo = extractPageNumber(logicalAddress);
frameNumber = pageTable[pNo];
if(frameNumber == -1){
numberOfPageFaults++;
frameNumber = retrieveFromStore(pNo);
pageTable[pNo] = frameNumber;
}
return frameNumber;
}
int main(){
physicalMemory = (int **) malloc(sizeof(int *) * 256);
pageTable = (int *) malloc(sizeof(int) * 256);
uint16_t logicalAddress;
int i=0;
int frameNumber;
uint16_t physicalAddress;
uint16_t offset;
for(int i=0;i<256;i++){
physicalMemory[i] = (int*)malloc(256);
pageTable[i] = -1;
}
logicalAddressStream = fopen("address.txt","r");
backingStore = fopen("BackingStore.txt","rb");
if(logicalAddressStream == NULL || backingStore == NULL){
printf("\nError opening the address file/backing store\n");
exit(1);
}
else{
i=0;
while(fscanf(logicalAddressStream,"%hx",&logicalAddress) != EOF && i<256){
frameNumber = getFrame(logicalAddress);
offset = extractOffset(logicalAddress);
physicalAddress = (frameNumber << 8) + offset;
i++;
printf("%d> LA: %hx, PA: %hx, Data: %d\n",i,logicalAddress,physicalAddress,physicalMemory[frameNumber][offset]);
}
}
printf("Number of Page Faults : %d\n",numberOfPageFaults);
fclose(logicalAddressStream);
fclose(backingStore);
return 0;
}