forked from rudyleiva93/Virtual-Memory-Manager
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVirtualMem_Manager.c
More file actions
153 lines (127 loc) · 3.88 KB
/
Copy pathVirtualMem_Manager.c
File metadata and controls
153 lines (127 loc) · 3.88 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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <pthread.h>
#include <assert.h>
#include <unistd.h>
#define TLB_SIZE 16
#define PAGE_SIZE 256
#define FRAME_SIZE 256
#define PHYSICAL_MEMORY_SIZE PAGE_SIZE*FRAME_SIZE
int logicalAddress = 0;
int offsetNumber = 0;
int pageNumber = 0;
int physicalAddress = 0;
int Frame = 0;
int Value = 0;
int Hit = 0;
int tlbIndex = 0;
int tlbSize = 0;
unsigned pageNumberMask = 65280; //1111111100000000
unsigned offsetMask = 255; //11111111
int tlbHitCount = 0;
float tlbHitRate = 0;
int addressCount = 0;
int pageFaultCount = 0;
float pageFaultRate = 0;
struct tlbTable {
unsigned int pageNum;
unsigned int frameNum;
};
int main (int argc, char *argv[])
{
//Check to see if user inputs addresses.txt
if (argc != 2)
{
fprintf(stderr, "Usage ./VirtualMem_Manager <Filename.txt> \n");
exit(1);
}
//Open addresses.txt, BACKING_STORE.bin, and
//Create Output.txt to store program results
FILE *addresses = fopen(argv[1],"r");
FILE *BACKINGSTORE = fopen("BACKING_STORE.bin", "rb");
FILE *Output = fopen("addressOutput.txt", "w");
int physicalMemory[PHYSICAL_MEMORY_SIZE];
char Buffer[256];
int Index;
//Declare and initialize pageTable[] array to -1
int pageTable[PAGE_SIZE];
memset(pageTable, -1, 256*sizeof(int));
//Declare and initialize tlb[] structure to -1
struct tlbTable tlb[TLB_SIZE];
memset (pageTable, -1, 16*sizeof(char));
//Read each address from addresses.txt
while(fscanf(addresses, "%d", &logicalAddress) == 1)
{
addressCount++;
//set the page number and offset for each logical address
pageNumber = logicalAddress & pageNumberMask;
pageNumber = pageNumber >> 8;
offsetNumber = logicalAddress & offsetMask;
Hit = -1;
//Check to see if the page number is already in the tlb
//If it is in tlb, then it is tlb hit
for(Index = 0; Index < tlbSize; Index++)
{
if(tlb[Index].pageNum == pageNumber)
{
Hit = tlb[Index].frameNum;
physicalAddress = Hit*256 + offsetNumber;
}
}
if(!(Hit == -1))
{
tlbHitCount++;
}
//This "else if" loop is the tlb miss
//Gets the physical page number from page table
else if(pageTable[pageNumber] == -1)
{
fseek(BACKINGSTORE, pageNumber*256, SEEK_SET);
fread(Buffer, sizeof(char), 256, BACKINGSTORE);
pageTable[pageNumber] = Frame;
for(Index = 0; Index < 256; Index++)
{
physicalMemory[Frame*256 + Index] = Buffer[Index];
}
pageFaultCount++;
Frame++;
//FIFO algorithm for the tlb
if(tlbSize == 16)
tlbSize--;
for(tlbIndex = tlbSize; tlbIndex > 0; tlbIndex--)
{
tlb[tlbIndex].pageNum = tlb[tlbIndex-1].pageNum;
tlb[tlbIndex].frameNum = tlb[tlbIndex-1].frameNum;
}
if (tlbSize <= 15)
tlbSize++;
tlb[0].pageNum = pageNumber;
tlb[0].frameNum = pageTable[pageNumber];
physicalAddress = pageTable[pageNumber]*256 + offsetNumber;
}
else
{
physicalAddress = pageTable[pageNumber]*256 + offsetNumber;
}
//Gets the value from the bin file provided
Value = physicalMemory[physicalAddress];
//print the addresses and value to Output.txt
fprintf(Output, "Virtual Address: %d Physical Address: %d Value: %d \n", logicalAddress, physicalAddress, Value);
}
//The statistics of the program
pageFaultRate = pageFaultCount*1.0f / addressCount;
tlbHitRate = tlbHitCount*1.0f / addressCount;
//Close files provided for the project
fclose(addresses);
fclose(BACKINGSTORE);
//Print the statistics of the program to Output.txt
fprintf(Output, "Number of Addresses: %d\n", addressCount);
fprintf(Output, "Number of Page Faults: %d\n", pageFaultCount);
fprintf(Output, "Page Fault Rate: %f\n", pageFaultRate);
fprintf(Output, "TLB Hits: %d\n", tlbHitCount);
fprintf(Output, "TLB Hit Rate %f\n", tlbHitRate);
//Close Output.txt
fclose(Output);
return 0;
}