-
Notifications
You must be signed in to change notification settings - Fork 0
/
pageforktest.c
123 lines (115 loc) · 2.04 KB
/
pageforktest.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
#include "types.h"
#include "stat.h"
#include "user.h"
#define PGSIZE (1<<12)
#define PGADDRBIT 12
int init_page;
void init_mem(uint max_page)
{
uint currsize=(uint)sbrk(0);
int curr_page = currsize >> PGADDRBIT;
init_page = curr_page;
// printf(1,"curr_page : %d\n",curr_page);
if(curr_page>max_page)
{
printf(1," errorr curr_page:%d > max_page:%d\n",curr_page,max_page);
exit();
}
int new_page = max_page - curr_page;
// printf(1,"new_page : %d\n",new_page);
for(int i=0;i<new_page;i++)
{
if(sbrk(PGSIZE) == (void*)-1)
{
printf(1,"sbrk error\n");
return;
}
}
}
void write_mem(uint max_page,char c)
{
for(int i=init_page;i<max_page;i++)
{
for(int j=0;j<PGSIZE;j++)
{
*(char *)(i*PGSIZE+j) = c + j*PGSIZE + i;
}
}
}
int check_mem(uint max_page,char c)
{
for(int i=init_page;i<max_page;i++)
{
for(int j=0;j<PGSIZE;j++)
{
if(*(char *)(i*PGSIZE+j) != ((char)(c + j*PGSIZE + i)))
{
return -1;
}
}
}
return 0;
}
void release_mem(int max_page)
{
for(int i=init_page;i<max_page;i++)
{
sbrk(-PGSIZE);
}
}
int
main(int argc, char * argv[]){
int mx_page = atoi(argv[1]);
init_mem(mx_page);
write_mem(mx_page,'d');
check_mem(mx_page,'d');
release_mem(mx_page);
init_mem(mx_page);
write_mem(mx_page,'a');
sleep(50);
int x = fork();
if(x<0)
{
printf(1,"fork failed\n");
exit();
}
if(x==0)
{
if(check_mem(mx_page,'a') == -1)
{
printf(1,"child check memory failed\n");
exit();
}
release_mem(mx_page);
init_mem(mx_page);
write_mem(mx_page,'b');
if(check_mem(mx_page,'b') == -1)
{
printf(1,"child check memory failed\n");
exit();
}
release_mem(mx_page);
printf(1,"child mem test success\n");
exit();
}
else
{
wait();
if(check_mem(mx_page,'a') == -1)
{
printf(1,"parent check memory failed\n");
exit();
}
release_mem(mx_page);
init_mem(mx_page);
write_mem(mx_page,'c');
if(check_mem(mx_page,'c') == -1)
{
printf(1,"parent check memory failed\n");
exit();
}
release_mem(mx_page);
printf(1,"parent mem test success\n");
exit();
}
}