2006-08-19 23:41:34 +00:00
|
|
|
#include "types.h"
|
|
|
|
#include "stat.h"
|
2006-07-16 15:36:31 +00:00
|
|
|
#include "user.h"
|
2006-08-12 11:38:57 +00:00
|
|
|
#include "fcntl.h"
|
2006-07-16 15:36:31 +00:00
|
|
|
|
2006-07-01 21:26:01 +00:00
|
|
|
char buf[2048];
|
2006-06-27 14:35:53 +00:00
|
|
|
|
2006-07-11 17:39:45 +00:00
|
|
|
// simple fork and pipe read/write
|
|
|
|
|
2006-06-27 14:35:53 +00:00
|
|
|
void
|
2006-07-17 01:25:22 +00:00
|
|
|
pipe1(void)
|
2006-06-27 14:35:53 +00:00
|
|
|
{
|
|
|
|
int fds[2], pid;
|
2006-07-01 21:26:01 +00:00
|
|
|
int seq = 0, i, n, cc, total;
|
2006-06-27 14:35:53 +00:00
|
|
|
|
2006-08-29 19:06:37 +00:00
|
|
|
if(pipe(fds) != 0){
|
|
|
|
puts("pipe() failed\n");
|
|
|
|
exit();
|
|
|
|
}
|
2006-07-01 21:26:01 +00:00
|
|
|
pid = fork();
|
2006-06-27 14:35:53 +00:00
|
|
|
if(pid == 0){
|
2006-07-01 21:26:01 +00:00
|
|
|
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){
|
2006-08-12 11:38:57 +00:00
|
|
|
printf(1, "pipe1 oops 1\n");
|
2006-07-16 15:36:31 +00:00
|
|
|
exit();
|
2006-07-01 21:26:01 +00:00
|
|
|
}
|
|
|
|
}
|
2006-07-16 15:36:31 +00:00
|
|
|
exit();
|
2006-08-29 19:06:37 +00:00
|
|
|
} else if(pid > 0){
|
2006-07-01 21:26:01 +00:00
|
|
|
close(fds[1]);
|
|
|
|
total = 0;
|
|
|
|
cc = 1;
|
2006-07-17 01:25:22 +00:00
|
|
|
while((n = read(fds[0], buf, cc)) > 0){
|
2006-07-01 21:26:01 +00:00
|
|
|
for(i = 0; i < n; i++){
|
|
|
|
if((buf[i] & 0xff) != (seq++ & 0xff)){
|
2006-08-12 11:38:57 +00:00
|
|
|
printf(1, "pipe1 oops 2\n");
|
2006-07-01 21:26:01 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
total += n;
|
|
|
|
cc = cc * 2;
|
|
|
|
if(cc > sizeof(buf))
|
|
|
|
cc = sizeof(buf);
|
2006-06-27 14:35:53 +00:00
|
|
|
}
|
2006-07-01 21:26:01 +00:00
|
|
|
if(total != 5 * 1033)
|
2006-08-29 19:06:37 +00:00
|
|
|
printf(1, "pipe1 oops 3 total %d\n", total);
|
2006-07-01 21:26:01 +00:00
|
|
|
close(fds[0]);
|
2006-07-15 12:03:57 +00:00
|
|
|
wait();
|
2006-08-29 19:06:37 +00:00
|
|
|
} else {
|
|
|
|
puts("fork() failed\n");
|
|
|
|
exit();
|
2006-06-27 14:35:53 +00:00
|
|
|
}
|
|
|
|
puts("pipe1 ok\n");
|
|
|
|
}
|
|
|
|
|
2006-07-11 17:39:45 +00:00
|
|
|
// meant to be run w/ at most two CPUs
|
|
|
|
void
|
2006-07-17 01:25:22 +00:00
|
|
|
preempt(void)
|
2006-07-11 17:39:45 +00:00
|
|
|
{
|
|
|
|
int pid1, pid2, pid3;
|
|
|
|
int pfds[2];
|
|
|
|
|
|
|
|
pid1 = fork();
|
|
|
|
if(pid1 == 0)
|
2006-07-17 01:25:22 +00:00
|
|
|
for(;;)
|
2006-07-11 17:39:45 +00:00
|
|
|
;
|
2006-09-06 17:27:19 +00:00
|
|
|
|
2006-07-11 17:39:45 +00:00
|
|
|
pid2 = fork();
|
|
|
|
if(pid2 == 0)
|
2006-07-17 01:25:22 +00:00
|
|
|
for(;;)
|
2006-07-11 17:39:45 +00:00
|
|
|
;
|
|
|
|
|
|
|
|
pipe(pfds);
|
|
|
|
pid3 = fork();
|
|
|
|
if(pid3 == 0){
|
|
|
|
close(pfds[0]);
|
|
|
|
if(write(pfds[1], "x", 1) != 1)
|
2006-08-12 11:38:57 +00:00
|
|
|
printf(1, "preempt write error");
|
2006-07-11 17:39:45 +00:00
|
|
|
close(pfds[1]);
|
2006-07-17 01:25:22 +00:00
|
|
|
for(;;)
|
2006-07-11 17:39:45 +00:00
|
|
|
;
|
|
|
|
}
|
|
|
|
|
|
|
|
close(pfds[1]);
|
|
|
|
if(read(pfds[0], buf, sizeof(buf)) != 1){
|
2006-08-12 11:38:57 +00:00
|
|
|
printf(1, "preempt read error");
|
2006-07-11 17:39:45 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
close(pfds[0]);
|
|
|
|
kill(pid1);
|
|
|
|
kill(pid2);
|
|
|
|
kill(pid3);
|
|
|
|
wait();
|
|
|
|
wait();
|
|
|
|
wait();
|
|
|
|
puts("preempt ok\n");
|
|
|
|
}
|
|
|
|
|
2006-07-15 12:03:57 +00:00
|
|
|
// try to find any races between exit and wait
|
|
|
|
void
|
2006-07-17 01:25:22 +00:00
|
|
|
exitwait(void)
|
2006-07-15 12:03:57 +00:00
|
|
|
{
|
|
|
|
int i, pid;
|
|
|
|
|
|
|
|
for(i = 0; i < 100; i++){
|
|
|
|
pid = fork();
|
|
|
|
if(pid < 0){
|
2006-08-12 11:38:57 +00:00
|
|
|
printf(1, "fork failed\n");
|
2006-07-15 12:03:57 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
if(pid){
|
|
|
|
if(wait() != pid){
|
2006-08-12 11:38:57 +00:00
|
|
|
printf(1, "wait wrong pid\n");
|
2006-07-15 12:03:57 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
} else {
|
2006-07-16 15:36:31 +00:00
|
|
|
exit();
|
2006-07-15 12:03:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
puts("exitwait ok\n");
|
|
|
|
}
|
|
|
|
|
2006-08-24 19:24:36 +00:00
|
|
|
void
|
|
|
|
mem(void)
|
|
|
|
{
|
|
|
|
void *m1, *m2;
|
2006-08-29 19:06:37 +00:00
|
|
|
int pid;
|
2006-08-24 19:24:36 +00:00
|
|
|
|
2006-08-29 19:06:37 +00:00
|
|
|
if((pid = fork()) == 0){
|
|
|
|
m1 = 0;
|
2006-09-06 17:27:19 +00:00
|
|
|
while((m2 = malloc(10001)) != 0) {
|
|
|
|
*(char**) m2 = m1;
|
2006-08-29 19:06:37 +00:00
|
|
|
m1 = m2;
|
|
|
|
}
|
2006-09-06 17:27:19 +00:00
|
|
|
while(m1) {
|
|
|
|
m2 = *(char**)m1;
|
2006-08-29 19:06:37 +00:00
|
|
|
free(m1);
|
|
|
|
m1 = m2;
|
|
|
|
}
|
|
|
|
m1 = malloc(1024*20);
|
2006-09-06 17:27:19 +00:00
|
|
|
if(m1 == 0) {
|
2006-08-29 19:06:37 +00:00
|
|
|
puts("couldn't allocate mem?!!\n");
|
|
|
|
exit();
|
|
|
|
}
|
2006-08-24 19:24:36 +00:00
|
|
|
free(m1);
|
2006-08-29 19:06:37 +00:00
|
|
|
printf(1, "mem ok\n");
|
2006-08-24 19:24:36 +00:00
|
|
|
exit();
|
2006-08-29 19:06:37 +00:00
|
|
|
} else {
|
|
|
|
wait();
|
2006-08-24 19:24:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-07-16 15:36:31 +00:00
|
|
|
int
|
2006-07-28 22:33:07 +00:00
|
|
|
main(int argc, char *argv[])
|
2006-06-27 14:35:53 +00:00
|
|
|
{
|
2006-07-11 17:39:45 +00:00
|
|
|
puts("usertests starting\n");
|
2006-07-15 12:03:57 +00:00
|
|
|
|
2006-08-24 19:24:36 +00:00
|
|
|
mem();
|
2006-08-29 17:01:40 +00:00
|
|
|
pipe1();
|
|
|
|
preempt();
|
|
|
|
exitwait();
|
2006-06-27 14:35:53 +00:00
|
|
|
|
2006-08-12 11:38:57 +00:00
|
|
|
puts("usertests finished\n");
|
|
|
|
exit();
|
2006-06-27 14:35:53 +00:00
|
|
|
}
|