forked from mit-pdos/xv6-public
-
Notifications
You must be signed in to change notification settings - Fork 12
/
cowtest.c
94 lines (88 loc) · 3.37 KB
/
cowtest.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
#include "types.h"
#include "stat.h"
#include "user.h"
int a = 0;
int main()
{
printf(1, "================================\n");
printf(1, "Copy on write test started.\n");
// Simple fork test (exit first).
printf(1, "[Parent] Current free pages: %d\n", nfpgs());
printf(1, "[Parent] Current value of a: %d\n", a);
if (fork() == 0)
{
printf(1, "[Child ] Current free pages: %d\n", nfpgs());
printf(1, "[Child ] Current value of a: %d\n", a);
a = 1;
printf(1, "[Child ] Current free pages: %d\n", nfpgs());
printf(1, "[Child ] Current value of a: %d\n", a);
exit();
}
printf(1, "[Parent] Sleeping.\n");
sleep(100);
printf(1, "[Parent] Current free pages: %d\n", nfpgs());
printf(1, "[Parent] Current value of a: %d\n", a);
printf(1, "[Parent] Waiting.\n");
wait();
printf(1, "[Parent] Current free pages: %d\n", nfpgs());
printf(1, "[Parent] Current value of a: %d\n", a);
a = 2;
printf(1, "[Parent] Current free pages: %d\n", nfpgs());
printf(1, "[Parent] Current value of a: %d\n", a);
// Recursive fork test.
a = 0;
printf(1, "[Parent] Current free pages: %d\n", nfpgs());
printf(1, "[Parent] Current value of a: %d\n", a);
if (fork() == 0)
{
printf(1, "[Child1] Current free pages: %d\n", nfpgs());
printf(1, "[Child1] Current value of a: %d\n", a);
if (fork() == 0)
{
printf(1, "[Child2] Current free pages: %d\n", nfpgs());
printf(1, "[Child2] Current value of a: %d\n", a);
a = 3;
printf(1, "[Child2] Current free pages: %d\n", nfpgs());
printf(1, "[Child2] Current value of a: %d\n", a);
exit();
}
printf(1, "[Child1] Current free pages: %d\n", nfpgs());
printf(1, "[Child1] Current value of a: %d\n", a);
wait();
exit();
}
printf(1, "[Parent] Current free pages: %d\n", nfpgs());
printf(1, "[Parent] Current value of a: %d\n", a);
wait();
printf(1, "[Parent] Current free pages: %d\n", nfpgs());
printf(1, "[Parent] Current value of a: %d\n", a);
a = 4;
printf(1, "[Parent] Current free pages: %d\n", nfpgs());
printf(1, "[Parent] Current value of a: %d\n", a);
// Simple fork test (wait first).
printf(1, "[Parent] Current free pages: %d\n", nfpgs());
printf(1, "[Parent] Current value of a: %d\n", a);
if (fork() == 0)
{
printf(1, "[Child ] Sleeping\n");
sleep(100);
printf(1, "[Child ] Current free pages: %d\n", nfpgs());
printf(1, "[Child ] Current value of a: %d\n", a);
a = 1;
printf(1, "[Child ] Current free pages: %d\n", nfpgs());
printf(1, "[Child ] Current value of a: %d\n", a);
exit();
}
printf(1, "[Parent] Current free pages: %d\n", nfpgs());
printf(1, "[Parent] Current value of a: %d\n", a);
printf(1, "[Parent] Waiting.\n");
wait();
printf(1, "[Parent] Current free pages: %d\n", nfpgs());
printf(1, "[Parent] Current value of a: %d\n", a);
a = 2;
printf(1, "[Parent] Current free pages: %d\n", nfpgs());
printf(1, "[Parent] Current value of a: %d\n", a);
printf(1, "Copy on write test finished.\n");
printf(1, "================================\n");
return 0;
}