sllul/forktest.c

95 lines
1.5 KiB
C
Raw Permalink Normal View History

// 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"
// N should be set HIGHER than the limit!
#define N 10000
void
print(const char *s)
{
write(1, s, strlen(s));
}
void printdec(unsigned int i) {
if (i == 0) {
print("0");
return;
}
char foo[11];
int idx = 10;
while (i > 0) {
idx--;
foo[idx] = '0' + (i%10);
i /= 10;
}
foo[10] = 0;
print(foo+idx);
}
void
forktest(void)
{
int n, pid;
print("fork test\n");
//print("testing printdec(12345)="); printdec(12345); print("\n");
print("attempting to fork up to N="); printdec(N); print(" processes...\n");
for(n=0; n<N; n++){
pid = fork();
if(pid < 0)
break;
if (pid == 0) {
int i, j, ch, pr;
pr = 7-(getpid() % 4);
ch = '0'+pr;
prio(getpid(), pr, 4);
sleep(1);
for (i = 0; i < 100; i++) {
for (j = 0; j < 256; j++) {
ch++;
}
char str[2];
str[0] = (char)ch;
str[1] = 0;
print(str);
}
exit(0);
}
}
print("forked "); printdec(n); print(" processes\n");
if(n == N){
print("fork claimed to work N times! forktest should be updated so N is greater than the kernel's NPROC!\n");
exit(1);
}
for(; n > 0; n--){
if(wait(0) < 0){
print("wait stopped early\n");
exit(1);
}
}
if(wait(0) != -1){
print("wait got too many\n");
exit(1);
}
print("fork test OK\n");
}
int
main(void)
{
forktest();
exit(0);
}