DF-0032 / forktest.c
/* forktest: do one fork() as the caller; report pid/errno. Used to prove that * even root cannot fork once nprocs >= maxproc (system-wide fork DoS). */ #include <stdio.h> #include <errno.h> #include <string.h> #include <unistd.h> #include <sys/wait.h> int main(int argc, char **argv){ int n = (argc>1)?atoi(argv[1]):1; int eagain=0, ok=0, other=0; for(int i=0;i<n;i++){ pid_t p=fork(); if(p==0)_exit(0); if(p>0){ok++; waitpid(p,NULL,0);} else if(errno==EAGAIN)eagain++; else other++; if(eagain>0){ printf("forktest: EAGAIN after %d ok (errno=%d %s)\n",ok,errno,strerror(errno)); fflush(stdout); return 0; } } printf("forktest: all %d forks succeeded (no EAGAIN)\n", ok); fflush(stdout); return 0; } |