multiproc.c 590 B

123456789101112131415161718192021222324252627282930
  1. /* -*- mode:c; c-file-style:"k&r"; c-basic-offset: 4; tab-width:4; indent-tabs-mode:nil; mode:auto-fill; fill-column:78; -*- */
  2. /* vim: set ts=4 sw=4 et tw=78 fo=cqt wm=0: */
  3. #include <stdlib.h>
  4. #include <unistd.h>
  5. #include <sys/wait.h>
  6. int main(int argc, char ** argv)
  7. {
  8. int times = 0, i;
  9. pid_t pid;
  10. if (argc > 1)
  11. times = atoi(argv[1]);
  12. for (i = 0 ; i < times ; i++) {
  13. pid = fork();
  14. if (pid < 0)
  15. exit(1);
  16. if (pid > 0) {
  17. waitpid(pid, NULL, 0);
  18. exit(0);
  19. }
  20. }
  21. sleep(1);
  22. return 0;
  23. }