-
Notifications
You must be signed in to change notification settings - Fork 0
/
usertests.c
168 lines (152 loc) · 2.61 KB
/
usertests.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
#include "types.h"
#include "stat.h"
#include "user.h"
#include "fcntl.h"
char buf[2048];
// simple fork and pipe read/write
void
pipe1(void)
{
int fds[2], pid;
int seq = 0, i, n, cc, total;
if(pipe(fds) != 0){
puts("pipe() failed\n");
exit();
}
pid = fork();
if(pid == 0){
close(fds[0]);
for(n = 0; n < 5; n++){
for(i = 0; i < 1033; i++)
buf[i] = seq++;
if(write(fds[1], buf, 1033) != 1033){
printf(1, "pipe1 oops 1\n");
exit();
}
}
exit();
} else if(pid > 0){
close(fds[1]);
total = 0;
cc = 1;
while((n = read(fds[0], buf, cc)) > 0){
for(i = 0; i < n; i++){
if((buf[i] & 0xff) != (seq++ & 0xff)){
printf(1, "pipe1 oops 2\n");
return;
}
}
total += n;
cc = cc * 2;
if(cc > sizeof(buf))
cc = sizeof(buf);
}
if(total != 5 * 1033)
printf(1, "pipe1 oops 3 total %d\n", total);
close(fds[0]);
wait();
} else {
puts("fork() failed\n");
exit();
}
puts("pipe1 ok\n");
}
// meant to be run w/ at most two CPUs
void
preempt(void)
{
int pid1, pid2, pid3;
int pfds[2];
pid1 = fork();
if(pid1 == 0)
for(;;)
;
pid2 = fork();
if(pid2 == 0)
for(;;)
;
pipe(pfds);
pid3 = fork();
if(pid3 == 0){
close(pfds[0]);
if(write(pfds[1], "x", 1) != 1)
printf(1, "preempt write error");
close(pfds[1]);
for(;;)
;
}
close(pfds[1]);
if(read(pfds[0], buf, sizeof(buf)) != 1){
printf(1, "preempt read error");
return;
}
close(pfds[0]);
kill(pid1);
kill(pid2);
kill(pid3);
wait();
wait();
wait();
puts("preempt ok\n");
}
// try to find any races between exit and wait
void
exitwait(void)
{
int i, pid;
for(i = 0; i < 100; i++){
pid = fork();
if(pid < 0){
printf(1, "fork failed\n");
return;
}
if(pid){
if(wait() != pid){
printf(1, "wait wrong pid\n");
return;
}
} else {
exit();
}
}
puts("exitwait ok\n");
}
void
mem(void)
{
void *m1, *m2;
int pid;
if((pid = fork()) == 0){
m1 = 0;
while ((m2 = malloc(10001)) != 0) {
*(char **) m2 = m1;
m1 = m2;
}
while (m1) {
m2 = *(char **)m1;
free(m1);
m1 = m2;
}
m1 = malloc(1024*20);
if (m1 == 0) {
puts("couldn't allocate mem?!!\n");
exit();
}
free(m1);
printf(1, "mem ok\n");
exit();
} else {
wait();
}
}
int
main(int argc, char *argv[])
{
puts("usertests starting\n");
mem();
pipe1();
preempt();
exitwait();
puts("usertests finished\n");
exit();
}