Skip to content

Commit

Permalink
test that fork fails gracefully
Browse files Browse the repository at this point in the history
  • Loading branch information
rsc committed Aug 24, 2007
1 parent 5af5f6a commit e0e7d07
Show file tree
Hide file tree
Showing 3 changed files with 103 additions and 4 deletions.
14 changes: 10 additions & 4 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,9 @@ vectors.S : vectors.pl

ULIB = ulib.o usys.o printf.o umalloc.o

usertests : usertests.o $(ULIB)
$(LD) -N -e main -Ttext 0 -o usertests usertests.o $(ULIB)
$(OBJDUMP) -S usertests > usertests.asm
_usertests : usertests.o $(ULIB)
$(LD) -N -e main -Ttext 0 -o _usertests usertests.o $(ULIB)
$(OBJDUMP) -S _usertests > usertests.asm

_echo : echo.o $(ULIB)
$(LD) -N -e main -Ttext 0 -o _echo echo.o $(ULIB)
Expand Down Expand Up @@ -117,10 +117,16 @@ _zombie: zombie.o $(ULIB)
$(LD) -N -e main -Ttext 0 -o _zombie zombie.o $(ULIB)
$(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
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)
./mkfs fs.img README $(UPROGS)

Expand Down
54 changes: 54 additions & 0 deletions forktest.c
Original file line number Diff line number Diff line change
@@ -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();
}
39 changes: 39 additions & 0 deletions usertests.c
Original file line number Diff line number Diff line change
Expand Up @@ -1189,6 +1189,44 @@ iref(void)
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
main(int argc, char *argv[])
{
Expand Down Expand Up @@ -1223,6 +1261,7 @@ main(int argc, char *argv[])
sharedfd();
dirfile();
iref();
forktest();

exectest();

Expand Down

0 comments on commit e0e7d07

Please sign in to comment.