Advertisement
Guest User

Untitled

a guest
Dec 16th, 2018
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.71 KB | None | 0 0
  1. #include <sys/wait.h>
  2. #include <stdio.h>
  3. #include <sys/types.h>
  4. #include <unistd.h>
  5. #include <stdlib.h>
  6. #include <string.h>
  7.  
  8.  
  9. int main(int argc, char *argv[]) {
  10.     char word[4097];
  11.     int counter = 0;
  12.     pid_t pid;
  13.     pid_t root = -1;
  14.     do {
  15.         if (scanf("%s", word) != EOF) {
  16.             counter++;
  17.             pid = fork();
  18.             if (counter == 1 && pid > 0) root = pid;
  19.         } else {
  20.             if (counter == 0) {
  21.                 printf("%d", counter);
  22.                 return 0;
  23.             }
  24.             return counter;
  25.             break;
  26.         }
  27.     } while (counter <= 255 && pid == 0);
  28.  
  29.     if (pid > 0) {
  30.         int status;
  31.         waitpid(pid, &status, 0);
  32.         int exit_status = WEXITSTATUS(status);
  33.         if (pid == root) {
  34.             printf("%d", exit_status); 
  35.             return 0;
  36.         }
  37.         return exit_status;
  38.     }
  39.  
  40. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement