-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVMEM.c
More file actions
169 lines (125 loc) · 3.96 KB
/
Copy pathVMEM.c
File metadata and controls
169 lines (125 loc) · 3.96 KB
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
/*------Programmming Problem Chap 10 ---------
implement a virtual memory manager
group 16
------------------*/
//required header files
#include <string.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
const int PAGE_TABLE_SIZE = 256; //2^8 page table size
const int TLB_SIZE = 16; //16 enteries in table
struct TLB { //initializing TLB
unsigned char pageNo[16]; //page number size
unsigned char frameNo[16]; //frame number size
int ind; // index
};
const int BUFFER_SIZE = 256;
const int PHYS_MEM_SIZE = 256; //page frame size
int read_Frame (int pageNumber, char *phys_Mem, int* openFrame){
char one_frame[BUFFER_SIZE];
memset(one_frame, 0, sizeof(one_frame));
// Backing store is a random access file
FILE *fp;
fp = fopen("BACKING_STORE.bin", "rb");
if (fp == NULL){
printf("File failed to open\n");
exit(0);
}
// c functions fseek() , fread() are used for performing i/o
if (fread(one_frame, sizeof(char), PHYS_MEM_SIZE, fp)==0)
printf("error in fread() function \n");
// checking if the file has data available
if (fseek(fp, pageNumber * PHYS_MEM_SIZE, SEEK_SET)!=0)
printf("error in fseek() function \n");
// reading 256 bytes in available page frame
int i = 0;
for(i; i < PHYS_MEM_SIZE; i++){
*((phys_Mem+(*openFrame)*PHYS_MEM_SIZE)+i) = one_frame[i];
}
(*openFrame)++;
return (*openFrame)-1;
}
int findPage(int LAddress, char* PageTable, struct TLB *tlb, char* phyMem, int* openFrame, int* pageFaults, int* TLBhits){
unsigned char bit_mask = 0xFF; //used masking to bit_mask upper 16 bytes
unsigned char offset;
unsigned char pageNum;
bool TLBhit = false;
// printing the logical address of page
printf("Virtual adress: %d\t", LAddress);
// extracting the page number and offset
pageNum = (LAddress >> 8) & bit_mask;
offset = LAddress & bit_mask;
//checking if the tlb has the page present
int frame = 0;
int i = 0;
for (i; i < TLB_SIZE; i++){
if(tlb->pageNo[i] == pageNum){
frame = tlb->frameNo[i];
TLBhit = true;
(*TLBhits)++;
}
}
int value;
int newFrame = 0;
//Check if TLB does not contain the page we want
if (TLBhit == false){
//check if page table doesnot contain the page we want
if (PageTable[pageNum] != -1){
// printf("Pagehit");
}
else{
//in case of page fault here
newFrame = read_Frame(pageNum, phyMem, openFrame);
PageTable[pageNum] = newFrame;
(*pageFaults)++;
}
frame = PageTable[pageNum];
tlb->pageNo[tlb->ind] = pageNum;
tlb->frameNo[tlb->ind] = PageTable[pageNum];
tlb->ind = (tlb->ind + 1)%TLB_SIZE;
}
int index = ((unsigned char)frame*PHYS_MEM_SIZE)+offset;
value = *(phyMem+index);
printf("Physical address: %d\t Value: %d\n",index, value);
return 0;
}
//Main function begins here
int main (int argc, char* argv[]){
if (argc < 2){
printf("Not enough arguments\nProgram Exiting\n");
exit(0);
}
FILE *fd;
fd = fopen(argv[1], "r");
if (fd == NULL){
printf("File failed to open\n");
exit(0);
}
int openFrame = 0;
int pageFaults = 0;
int TLBhits = 0;
float pageFaultRate;
float TLBHitRate;
//initializing page table
unsigned char PageTable[PAGE_TABLE_SIZE];
memset(PageTable, -1, sizeof(PageTable));
//initializing TLB
struct TLB tlb;
memset(tlb.pageNo, -1, sizeof(tlb.pageNo));
memset(tlb.frameNo, -1, sizeof(tlb.frameNo));
tlb.ind = 0;
//iniitalizng phyisical memory
char PhyMem[PHYS_MEM_SIZE][PHYS_MEM_SIZE]; //65,536 bytes (256 frames × 256-byte frame size)
int inputCount = 0;
int v;
while (fscanf(fd, "%d", &v)==1){
findPage(v, PageTable, &tlb, (char*)PhyMem, &openFrame, &pageFaults, &TLBhits);
inputCount++;
}
pageFaultRate = (float)pageFaults / (float)inputCount;
TLBHitRate = (float)TLBhits / (float)inputCount;
printf("******* Page Fault Rate: %.4f \n TLB hit rate= %.4f\n*****",pageFaultRate, TLBHitRate);
pclose(fd);
return 0;
}