xv6-65oo2/user/forktest.c

57 lines
752 B
C
Raw Permalink Normal View History

2007-08-24 20:20:23 +00:00
// Test that fork fails gracefully.
// Tiny executable so that the limit can be filling the proc table.
#include "kernel/types.h"
#include "kernel/stat.h"
#include "user/user.h"
2007-08-24 20:20:23 +00:00
#define N 1000
2007-08-24 20:20:23 +00:00
void
2019-08-27 17:13:03 +00:00
print(const char *s)
2007-08-24 20:20:23 +00:00
{
2019-08-27 17:13:03 +00:00
write(1, s, strlen(s));
2007-08-24 20:20:23 +00:00
}
void
forktest(void)
{
int n, pid;
2019-08-27 17:13:03 +00:00
print("fork test\n");
2007-08-24 20:20:23 +00:00
for(n=0; n<N; n++){
2007-08-24 20:20:23 +00:00
pid = fork();
if(pid < 0)
break;
if(pid == 0)
exit(0);
2007-08-24 20:20:23 +00:00
}
if(n == N){
2019-08-27 17:13:03 +00:00
print("fork claimed to work N times!\n");
2019-09-11 14:04:40 +00:00
exit(1);
2007-08-24 20:20:23 +00:00
}
2007-08-24 20:20:23 +00:00
for(; n > 0; n--){
if(wait(0) < 0){
2019-08-27 17:13:03 +00:00
print("wait stopped early\n");
2019-09-11 14:04:40 +00:00
exit(1);
2007-08-24 20:20:23 +00:00
}
}
if(wait(0) != -1){
2019-08-27 17:13:03 +00:00
print("wait got too many\n");
2019-09-11 14:04:40 +00:00
exit(1);
2007-08-24 20:20:23 +00:00
}
2019-08-27 17:13:03 +00:00
print("fork test OK\n");
2007-08-24 20:20:23 +00:00
}
int
main(void)
{
forktest();
exit(0);
2007-08-24 20:20:23 +00:00
}