-
Notifications
You must be signed in to change notification settings - Fork 0
/
tests.c
81 lines (73 loc) · 1.25 KB
/
tests.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
#include "types.h"
#include "stat.h"
#include "user.h"
#define PAGE_SIZE 4096
#define NUM_PAGES 1001
#define NUM_ACCESSES 100
void foo(void)
{
char c = 'a';
char *addr = &c;
int i;
for (i = 0; i < 10; i++)
{
addr += PAGE_SIZE;
*addr = c + i + 1;
}
}
void forkTest()
{
printf(1, "fork test\n");
int pid = fork();
if (pid == 0)
{
// child process
foo();
exit();
}
else if (pid > 0)
{
// parent process
wait();
printf(1, "fork test ok\n");
}
else
{
printf(1, "fork error\n");
}
}
void sbrkTest()
{
printf(1, "sbrk test\n");
char *pages[NUM_PAGES];
int i, j;
// Allocate and initialize pages with zeroes
for (i = 0; i < NUM_PAGES; i++)
{
pages[i] = sbrk(PAGE_SIZE);
if (pages[i] == (char *)-1)
{
printf(2, "Error: sbrk test error %d.\n", i);
exit();
}
memset(pages[i], 0, PAGE_SIZE);
}
// Access pages randomly to simulate demand paging
for (i = 0; i < NUM_ACCESSES; i++)
{
j = i % NUM_PAGES;
pages[j][0] = 'a'; // Access first byte of page to trigger page fault
}
sbrk(-(NUM_PAGES * PAGE_SIZE));
printf(1, "sbrk test ok\n");
}
int main(int argc, char *argv[])
{
lrur();
swapr();
sbrkTest();
lrur();
swapr();
forkTest();
exit();
}