back-port fork/exit/exit tests to xv6-riscv

This commit is contained in:
Robert Morris 2019-07-05 12:33:26 -04:00
parent f59c1bf1d8
commit 75b0c6fc91
3 changed files with 115 additions and 5 deletions

View file

@ -129,7 +129,7 @@ filetest()
{ {
int parent = getpid(); int parent = getpid();
printf(1, "file test: "); printf(1, "file: ");
buf[0] = 99; buf[0] = 99;

View file

@ -31,7 +31,8 @@ main(void)
printf(1, "init: exec sh failed\n"); printf(1, "init: exec sh failed\n");
exit(); exit();
} }
while((wpid=wait()) >= 0 && wpid != pid) while((wpid=wait()) >= 0 && wpid != pid){
printf(1, "zombie!\n"); //printf(1, "zombie!\n");
}
} }
} }

View file

@ -417,16 +417,18 @@ exitwait(void)
{ {
int i, pid; int i, pid;
printf(1, "exitwait test\n");
for(i = 0; i < 100; i++){ for(i = 0; i < 100; i++){
pid = fork(); pid = fork();
if(pid < 0){ if(pid < 0){
printf(1, "fork failed\n"); printf(1, "fork failed\n");
return; exit();
} }
if(pid){ if(pid){
if(wait() != pid){ if(wait() != pid){
printf(1, "wait wrong pid\n"); printf(1, "wait wrong pid\n");
return; exit();
} }
} else { } else {
exit(); exit();
@ -435,6 +437,109 @@ exitwait(void)
printf(1, "exitwait ok\n"); printf(1, "exitwait ok\n");
} }
// try to find races in the reparenting
// code that handles a parent exiting
// when it still has live children.
void
reparent(void)
{
int master_pid = getpid();
printf(1, "reparent test\n");
for(int i = 0; i < 100; i++){
int pid = fork();
if(pid < 0){
printf(1, "fork failed\n");
exit();
}
if(pid){
if(wait() != pid){
printf(1, "wait wrong pid\n");
exit();
}
} else {
int pid2 = fork();
if(pid2 < 0){
printf(1, "fork failed\n");
kill(master_pid);
exit();
}
if(pid2 == 0){
exit();
} else {
exit();
}
}
}
printf(1, "reparent ok\n");
}
// what if two children exit() at the same time?
void
twochildren(void)
{
printf(1, "twochildren test\n");
for(int i = 0; i < 1000; i++){
int pid1 = fork();
if(pid1 < 0){
printf(1, "fork failed\n");
exit();
}
if(pid1 == 0){
exit();
} else {
int pid2 = fork();
if(pid2 < 0){
printf(1, "fork failed\n");
exit();
}
if(pid2 == 0){
exit();
} else {
wait();
wait();
}
}
}
printf(1, "twochildren ok\n");
}
void
forkforkfork(void)
{
printf(1, "forkforkfork test\n");
unlink("stopforking");
int pid = fork();
if(pid < 0){
printf(1, "fork failed");
exit();
}
if(pid == 0){
while(1){
int fd = open("stopforking", 0);
if(fd >= 0){
exit();
}
if(fork() < 0){
close(open("stopforking", O_CREATE|O_RDWR));
}
}
exit();
}
sleep(2);
close(open("stopforking", O_CREATE|O_RDWR));
wait();
sleep(1);
printf(1, "forkforkfork ok\n");
}
void void
mem(void) mem(void)
{ {
@ -1751,6 +1856,10 @@ main(int argc, char *argv[])
} }
close(open("usertests.ran", O_CREATE)); close(open("usertests.ran", O_CREATE));
reparent();
twochildren();
forkforkfork();
argptest(); argptest();
createdelete(); createdelete();
linkunlink(); linkunlink();