xv6-riscv-kernel/user/forktest.c

55 lines
709 B
C
Raw Normal View History

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