-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMemManager.h
More file actions
207 lines (195 loc) · 6.01 KB
/
MemManager.h
File metadata and controls
207 lines (195 loc) · 6.01 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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
#include<stdio.h>
#include<stdlib.h>
#include<malloc.h>
#include<string.h>
#include<time.h>
#define TLB_SIZE 32
static const int m = 100;
static const int t = 40;
typedef struct page_table
{
int process;//for victim list; although I prefer create another victim struct but HW requires reference bit in page table
int vpn;
int location;
_Bool reference;
_Bool present;
} page_table;
typedef struct TLB_table
{
int vir;
int physical;
} TLB_table;
int search_TLB(TLB_table* TLB,int vpn)
{
for (int i =0; i<TLB_SIZE; i++)
{
if (vpn == TLB[i].vir)
{
int ret = TLB[i].physical;
TLB_table temp = TLB[i];
for(int j=i; j<TLB_SIZE-1; j++)
TLB[j] = TLB[j+1];
TLB[TLB_SIZE-1] = temp;
return ret;
}
}
return -1;
}//return pfn if hit, -1 if miss
int search_page_table(page_table* table, int vpn)
{
if(table[vpn].present == 1)
return table[vpn].location;
else
return -1;
}
void update_TLB(TLB_table* TLB, int vpn, int pfn, char* TLB_policy, int process)
{
for(int i=0; i<TLB_SIZE; i++)
{
if(TLB[i].vir==-1)
{
TLB[i].vir = vpn;
TLB[i].physical = pfn;
return;
}
}
//if full
if(strcmp(TLB_policy,"LRU")==0)
{
for(int i=0; i<TLB_SIZE-1; i++)
{
TLB[i] = TLB[i+1];
}
TLB[TLB_SIZE-1].vir = vpn;
TLB[TLB_SIZE-1].physical = pfn;
}
else
{
srand(time(NULL));
int index;
index = rand()%TLB_SIZE;
TLB[index].vir = vpn;
TLB[index].physical = pfn;
}
return;
}
void remove_victim(page_table* victim,page_table* page,_Bool* disk,int frames, int index, int* des)
{
for(int i=0;; i++)
{
if(disk[i] == 1)
{
page[0].location = i;
page[0].present=0;
disk[i] = 0;
*des = i;
break;
}
}//save to disk
for(int i = index; i< frames-1; i++)
{
victim[i] = victim[i+1];
}//move forward
return;
}
void page_fault_handler(_Bool* memory,_Bool* disk,int vpn,int frames,page_table** pte, page_table* victim, int process,char* page_policy,char* frame_policy, int* pfn, int* source, char* evict_p, int* evict_vpn, int* des)
{
//if memory avalable
for(int i=0; i<frames; i++)
{
if(memory[i] == 1)
{
*source = pte[process][vpn].location;
for(int j=0; j<frames; j++)
{
if(victim[j].process == -1)
{
victim[j].process = process;
victim[j].location= pte[process][vpn].location = i;
victim[j].reference= pte[process][vpn].reference = 0;
victim[j].vpn = vpn;
break;
}
}
memory[i] = 0;
*evict_p =(char) process + 'A';
*evict_vpn = -1;
*pfn = i;
*des=-1;
pte[process][vpn].present=1;
/*if(i==63){
for(int j=0;j<frames;j++){
printf("victim:%d process:%d location:%d\n",j,victim[j].process,victim[j].location);
}
}*/
return;
}
}
//if no memory available
while(1)
{
for(int i=0; i<frames; i++)
{
if(strcmp(frame_policy,"LOCAL") == 0 && victim[i].process!=process)
continue;
if(strcmp(page_policy, "FIFO") == 0)
{
*evict_p =(char) victim[i].process + 'A';
*evict_vpn = victim[i].vpn;
*pfn = victim[i].location;
//printf("pfn=%d\n", *pfn);
*source = pte[process][vpn].location;
remove_victim(victim,&pte[victim[i].process][victim[i].vpn],disk,frames,i,des);
if(pte[process][vpn].location != -1)
disk[pte[process][vpn].location] = 1;//if used page will free disk
victim[frames-1].process = process;
victim[frames-1].reference=0;
victim[frames-1].vpn = vpn;
victim[frames-1].location= *pfn;
return;
}
else //clock
{
if(pte[process][vpn].reference == 0)
{
pte[process][vpn].reference = 1;
}
else
{
*evict_p =(char) victim[i].process + 'A';
*evict_vpn = victim[i].vpn;
*pfn = victim[i].location;
*source = pte[process][vpn].location;
if(pte[process][vpn].location != -1)
disk[pte[process][vpn].location] = 1;//if used page will free disk
pte[process][vpn].reference = 0;
remove_victim(victim,&pte[victim[i].process][vpn],disk,frames,i,des);
victim[frames-1].process = process;
victim[frames-1].reference=0;
victim[frames-1].vpn = vpn;
victim[frames-1].location= *pfn;
return;
}
}
}
}
}//return pfn
void getconfig(char* TLB_policy, char* page_policy, char *frame_policy, int *num_process, int *virtual_page, int *frames)
{
char* path_config = "./sys_config.txt";
char temp[15];
FILE* config = fopen(path_config,"r");
int useless;//to ignore warning
useless = fscanf(config, "%*[^:]%*c%*c%[^\n]s", TLB_policy);
useless = fscanf(config, "%*[^:]%*c%*c%[^\n]s", page_policy);
useless = fscanf(config, "%*[^:]%*c%*c%[^\n]s", frame_policy);
useless = fscanf(config, "%*[^:]%*c%*c%[^'\n']s", temp);
*num_process = atoi(temp);
useless =fscanf(config, "%*[^:]%*c%*c%[^'\n']s", temp);
*virtual_page = atoi(temp);
useless = fscanf(config, "%*[^:]%*c%*c%s", temp);
*frames = atoi(temp);
fclose(config);
useless = useless+t+m;//useless line
return;
}