This repository has been archived by the owner on Mar 27, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
myMemTest.c
275 lines (238 loc) · 7.78 KB
/
myMemTest.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
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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
#include "types.h"
#include "stat.h"
#include "user.h"
#include "syscall.h"
#define PGSIZE 4096
#define DEBUG 0
int
main(int argc, char *argv[]){
#if FIFO
int i, j;
char *arr[14];
char input[10];
// Allocate all remaining 12 physical pages
for (i = 0; i < 12; ++i) {
arr[i] = sbrk(PGSIZE);
printf(1, "arr[%d]=0x%x\n", i, arr[i]);
}
printf(1, "Called sbrk(PGSIZE) 12 times - all physical pages taken.\nPress any key...\n");
gets(input, 10);
/*
Allocate page 15.
This allocation would cause page 0 to move to the swap file, but upon returning
to user space, a PGFLT would occur and pages 0,1 will be hot-swapped.
Afterwards, page 1 is in the swap file, the rest are in memory.
*/
arr[12] = sbrk(PGSIZE);
printf(1, "arr[12]=0x%x\n", arr[12]);
printf(1, "Called sbrk(PGSIZE) for the 13th time, a page fault should occur and one page in swap file.\nPress any key...\n");
gets(input, 10);
/*
Allocate page 16.
This would cause page 2 to move to the swap file, but since it contains the
user stack, it would be hot-swapped with page 3.
Afterwards, pages 1 & 3 are in the swap file, the rest are in memory.
*/
arr[13] = sbrk(PGSIZE);
printf(1, "arr[13]=0x%x\n", arr[13]);
printf(1, "Called sbrk(PGSIZE) for the 14th time, a page fault should occur and two pages in swap file.\nPress any key...\n");
gets(input, 10);
/*
Access page 3, causing a PGFLT, since it is in the swap file. It would be
hot-swapped with page 4. Page 4 is accessed next, so another PGFLT is invoked,
and this process repeats a total of 5 times.
*/
for (i = 0; i < 5; i++) {
for (j = 0; j < PGSIZE; j++)
arr[i][j] = 'k';
}
printf(1, "5 page faults should have occurred.\nPress any key...\n");
gets(input, 10);
if (fork() == 0) {
printf(1, "Child code running.\n");
printf(1, "View statistics for pid %d, then press any key...\n", getpid());
gets(input, 10);
/*
The purpose of this write is to create a PGFLT in the child process, and
verify that it is caught and handled properly.
*/
arr[5][0] = 't';
printf(1, "A page fault should have occurred for page 8.\nPress any key to exit the child code.\n");
gets(input, 10);
exit();
}
else {
wait();
/*
Deallocate all the pages.
*/
sbrk(-14 * PGSIZE);
printf(1, "Deallocated all extra pages.\nPress any key to exit the father code.\n");
gets(input, 10);
}
#elif SCFIFO
int i, j;
char *arr[14];
char input[10];
// TODO delete
printf(1, "myMemTest: testing SCFIFO... \n");
// Allocate all remaining 12 physical pages
for (i = 0; i < 12; ++i) {
arr[i] = sbrk(PGSIZE);
printf(1, "arr[%d]=0x%x\n", i, arr[i]);
}
printf(1, "Called sbrk(PGSIZE) 12 times - all physical pages taken.\nPress any key...\n");
gets(input, 10);
/*
Allocate page 15.
For this allocation, SCFIFO will consider moving page 0 to disk, but because it has been accessed, page 1 will be moved instead.
Afterwards, page 1 is in the swap file, the rest are in memory.
*/
arr[12] = sbrk(PGSIZE);
printf(1, "arr[12]=0x%x\n", arr[12]);
printf(1, "Called sbrk(PGSIZE) for the 13th time, no page fault should occur and one page in swap file.\nPress any key...\n");
gets(input, 10);
/*
Allocate page 16.
For this allocation, SCFIFO will consider moving page 2 to disk, but because it has been accessed, page 3 will be moved instead.
Afterwards, pages 1 & 3 are in the swap file, the rest are in memory.
*/
arr[13] = sbrk(PGSIZE);
printf(1, "arr[13]=0x%x\n", arr[13]);
printf(1, "Called sbrk(PGSIZE) for the 14th time, no page fault should occur and two pages in swap file.\nPress any key...\n");
gets(input, 10);
/*
Access page 3, causing a PGFLT, since it is in the swap file. It would be
hot-swapped with page 4. Page 4 is accessed next, so another PGFLT is invoked,
and this process repeats a total of 5 times.
*/
for (i = 0; i < 5; i++) {
for (j = 0; j < PGSIZE; j++)
arr[i][j] = 'k';
}
printf(1, "5 page faults should have occurred.\nPress any key...\n");
gets(input, 10);
/*
If DEBUG flag is defined as != 0 this is just another example showing
that because SCFIFO doesn't page out accessed pages, no needless page faults occurr.
*/
if(DEBUG){
for (i = 0; i < 5; i++) {
printf(1, "Writing to address 0x%x\n", arr[i]);
arr[i][0] = 'k';
}
//printf(1, "No page faults should have occurred.\nPress any key...\n");
gets(input, 10);
}
if (fork() == 0) {
printf(1, "Child code running.\n");
printf(1, "View statistics for pid %d, then press any key...\n", getpid());
gets(input, 10);
/*
The purpose of this write is to create a PGFLT in the child process, and
verify that it is caught and handled properly.
*/
arr[5][0] = 'k';
printf(1, "A Page fault should have occurred in child proccess.\nPress any key to exit the child code.\n");
gets(input, 10);
exit();
}
else {
wait();
/*
Deallocate all the pages.
*/
sbrk(-14 * PGSIZE);
printf(1, "Deallocated all extra pages.\nPress any key to exit the father code.\n");
gets(input, 10);
}
#elif NFU
int i, j;
char *arr[27];
char input[10];
// TODO delete
printf(1, "myMemTest: testing NFU... \n");
// Allocate all remaining 12 physical pages
for (i = 0; i < 12; ++i) {
arr[i] = sbrk(PGSIZE);
printf(1, "arr[%d]=0x%x\n", i, arr[i]);
}
printf(1, "Called sbrk(PGSIZE) 12 times - all physical pages taken.\nPress any key...\n");
gets(input, 10);
/*
Allocate page 15.
For this allocation, NFU will choose to move to disk the page that hasn't been accessed the longest (in this case page 1).
Afterwards, page 1 is in the swap file, the rest are in memory.
*/
arr[12] = sbrk(PGSIZE);
printf(1, "arr[12]=0x%x\n", arr[12]);
printf(1, "Called sbrk(PGSIZE) for the 13th time, no page fault should occur and one page in swap file.\nPress any key...\n");
gets(input, 10);
/*
Allocate page 16.
For this allocation, NFU will choose to move to disk the page that hasn't been accessed the longest (in this case page 3)
Afterwards, pages 1 & 3 are in the swap file, the rest are in memory.
*/
arr[13] = sbrk(PGSIZE);
printf(1, "arr[13]=0x%x\n", arr[13]);
printf(1, "Called sbrk(PGSIZE) for the 14th time, no page fault should occur and two pages in swap file.\nPress any key...\n");
gets(input, 10);
/*
Access page 3, causing a PGFLT, since it is in the swap file. It would be
hot-swapped with page 4. Page 4 is accessed next, so another PGFLT is invoked,
and this process repeats a total of 5 times.
*/
for (i = 0; i < 5; i++) {
printf(1, "Writing to address 0x%x\n", arr[i]);
for (j = 0; j < PGSIZE; j++){
arr[i][j] = 'k';
}
}
printf(1, "5 page faults should have occurred.\nPress any key...\n");
gets(input, 10);
/*
If DEBUG flag is defined as != 0 this is just another example showing
that because NFU doesn't page out accessed pages, no needless page faults occurr.
*/
if(DEBUG){
for (i = 0; i < 5; i++){
printf(1, "Writing to address 0x%x\n", arr[i]);
arr[i][0] = 'k';
}
//printf(1, "No page faults should have occurred.\nPress any key...\n");
gets(input, 10);
}
if (fork() == 0) {
printf(1, "Child code running.\n");
printf(1, "View statistics for pid %d, then press any key...\n", getpid());
gets(input, 10);
/*
The purpose of this write is to create a PGFLT in the child process, and
verify that it is caught and handled properly.
*/
arr[5][0] = 'k';
//arr[5][0] = 't';
printf(1, "Page faults should have occurred in child proccess.\nPress any key to exit the child code.\n");
gets(input, 10);
exit();
}
else {
wait();
/*
Deallocate all the pages.
*/
sbrk(-14 * PGSIZE);
printf(1, "Deallocated all extra pages.\nPress any key to exit the father code.\n");
gets(input, 10);
}
#else
char* arr[50];
int i = 50;
printf(1, "Commencing user test for default paging policy.\nNo page faults should occur.\n");
for (i = 0; i < 50; i++) {
arr[i] = sbrk(PGSIZE);
printf(1, "arr[%d]=0x%x\n", i, arr[i]);
}
#endif
exit();
}