Skip to content
Snippets Groups Projects
Commit e0e7d07e authored by rsc's avatar rsc
Browse files

test that fork fails gracefully

parent 5af5f6aa
No related branches found
No related tags found
No related merge requests found
...@@ -72,9 +72,9 @@ vectors.S : vectors.pl ...@@ -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) ...@@ -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)
......
// 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();
}
...@@ -1189,6 +1189,44 @@ iref(void) ...@@ -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[]) ...@@ -1223,6 +1261,7 @@ main(int argc, char *argv[])
sharedfd(); sharedfd();
dirfile(); dirfile();
iref(); iref();
forktest();
exectest(); exectest();
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment