test that fork fails gracefully

This commit is contained in:
rsc 2007-08-24 20:20:23 +00:00
parent 5af5f6aa7f
commit e0e7d07e5a
3 changed files with 103 additions and 4 deletions

View file

@ -72,9 +72,9 @@ vectors.S : vectors.pl
ULIB = ulib.o usys.o printf.o umalloc.o ULIB = ulib.o usys.o printf.o umalloc.o
usertests : usertests.o $(ULIB) _usertests : usertests.o $(ULIB)
$(LD) -N -e main -Ttext 0 -o usertests usertests.o $(ULIB) $(LD) -N -e main -Ttext 0 -o _usertests usertests.o $(ULIB)
$(OBJDUMP) -S usertests > usertests.asm $(OBJDUMP) -S _usertests > usertests.asm
_echo : echo.o $(ULIB) _echo : echo.o $(ULIB)
$(LD) -N -e main -Ttext 0 -o _echo echo.o $(ULIB) $(LD) -N -e main -Ttext 0 -o _echo echo.o $(ULIB)
@ -117,10 +117,16 @@ _zombie: zombie.o $(ULIB)
$(LD) -N -e main -Ttext 0 -o _zombie zombie.o $(ULIB) $(LD) -N -e main -Ttext 0 -o _zombie zombie.o $(ULIB)
$(OBJDUMP) -S _zombie > zombie.asm $(OBJDUMP) -S _zombie > zombie.asm
_forktest: forktest.o $(ULIB)
# forktest has less library code linked in - needs to be small
# in order to be able to max out the proc table.
$(LD) -N -e main -Ttext 0 -o _forktest forktest.o ulib.o usys.o
$(OBJDUMP) -S _forktest > forktest.asm
mkfs : mkfs.c fs.h mkfs : mkfs.c fs.h
cc -o mkfs mkfs.c cc -o mkfs mkfs.c
UPROGS=usertests _echo _cat _init _kill _ln _ls _mkdir _rm _sh _zombie UPROGS=_usertests _echo _cat _init _kill _ln _ls _mkdir _rm _sh _zombie _forktest
fs.img : mkfs README $(UPROGS) fs.img : mkfs README $(UPROGS)
./mkfs fs.img README $(UPROGS) ./mkfs fs.img README $(UPROGS)

54
forktest.c Normal file
View file

@ -0,0 +1,54 @@
// Test that fork fails gracefully.
// Tiny executable so that the limit can be filling the proc table.
#include "types.h"
#include "stat.h"
#include "user.h"
void
printf(int fd, char *s, ...)
{
write(fd, s, strlen(s));
}
void
forktest(void)
{
int n, pid;
printf(1, "fork test\n");
for(n=0; n<1000; n++){
pid = fork();
if(pid < 0)
break;
if(pid == 0)
exit();
}
if(n == 1000){
printf(1, "fork claimed to work 1000 times!\n");
exit();
}
for(; n > 0; n--){
if(wait() < 0){
printf(1, "wait stopped early\n");
exit();
}
}
if(wait() != -1){
printf(1, "wait got too many\n");
exit();
}
printf(1, "fork test OK\n");
}
int
main(void)
{
forktest();
exit();
}

View file

@ -1189,6 +1189,44 @@ iref(void)
printf(1, "empty file name OK\n"); printf(1, "empty file name OK\n");
} }
// test that fork fails gracefully
// the forktest binary also does this, but it runs out of proc entries first.
// inside the bigger usertests binary, we run out of memory first.
void
forktest(void)
{
int n, pid;
printf(1, "fork test\n");
for(n=0; n<1000; n++){
pid = fork();
if(pid < 0)
break;
if(pid == 0)
exit();
}
if(n == 1000){
printf(1, "fork claimed to work 1000 times!\n");
exit();
}
for(; n > 0; n--){
if(wait() < 0){
printf(1, "wait stopped early\n");
exit();
}
}
if(wait() != -1){
printf(1, "wait got too many\n");
exit();
}
printf(1, "fork test OK\n");
}
int int
main(int argc, char *argv[]) main(int argc, char *argv[])
{ {
@ -1223,6 +1261,7 @@ main(int argc, char *argv[])
sharedfd(); sharedfd();
dirfile(); dirfile();
iref(); iref();
forktest();
exectest(); exectest();