Advertisement
Guest User

sm13-1

a guest
Feb 16th, 2019
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.57 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <unistd.h>
  3. #include <sys/types.h>
  4. #include <sys/wait.h>
  5. #include <stdlib.h>
  6.  
  7. int
  8. main(int argc, char** argv) {
  9.     int n = strtol(argv[1], NULL, 10);
  10.     int counter = 0;
  11.     pid_t pid;
  12.     do {
  13.         ++counter;
  14.         printf("%d", counter);
  15.         if (counter != n) {
  16.             putchar(' ');
  17.         }
  18.         else {
  19.             putchar('\n');
  20.         }
  21.         fflush(stdout);
  22.         pid = fork();
  23.     } while (pid == 0 && counter < n);
  24.  
  25.     if (pid > 0) {
  26.         int status;
  27.         waitpid(pid, &status, 0);
  28.     }
  29. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement