multiproc.c 411 B

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