multiproc.c 434 B

123456789101112131415161718192021222324252627
  1. #include <stdlib.h>
  2. #include <sys/types.h>
  3. #include <sys/wait.h>
  4. #include <unistd.h>
  5. int main(int argc, char** argv) {
  6. int times = 0, i;
  7. pid_t pid;
  8. if (argc > 1)
  9. times = atoi(argv[1]);
  10. for (i = 0; i < times; i++) {
  11. pid = fork();
  12. if (pid < 0)
  13. exit(1);
  14. if (pid > 0) {
  15. waitpid(pid, NULL, 0);
  16. exit(0);
  17. }
  18. }
  19. sleep(1);
  20. return 0;
  21. }