// Program: forkalot.c // // Compile: cc forkalot.c -o forkalot // Run: ./forkalot [100] [1] // // Args: arg1 = # of copies of program to run simultaneously [100] // arg2 = Sleep time before exiting [1] #include #include #include #include #include #define CHILDREN 100 #define SLEEP_FOR 1 int main(int argc, char *argv[]) { int count, this_long; int pid; if (argc > 1) count = atoi(argv[1]); else count = CHILDREN; if (argc > 2) this_long = atoi(argv[2]); else this_long = SLEEP_FOR; /* fork count-1 children */ while (count-- > 1) { pid = fork(); if (pid == 0) { /* child */ break; } else if (pid < 0) { perror("fork"); exit(1); } } /* Sleepy... sleepy... */ sleep(this_long); /* All done... return success! */ return 0; }