Advertisement
Guest User

Untitled

a guest
Dec 15th, 2018
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.80 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <sys/types.h>
  3. #include <unistd.h>
  4. #include <sys/wait.h>
  5. #include <stdlib.h>
  6. #include <stdint.h>
  7.  
  8. char st[500];
  9.  
  10. void getword() {
  11.     int s = scanf("%s", st);
  12.     if (s == 1) {
  13.         pid_t pid = fork();
  14.         if (pid == 0) {
  15.             getword();
  16.         }
  17.         else {
  18.             int status;
  19.             waitpid(pid, &status, 0);
  20.             int exit_code = WEXITSTATUS(status);
  21.             exit(exit_code + 1);
  22.         }
  23.     }
  24.     else {
  25.         exit(0);
  26.     }
  27. }
  28.  
  29. int main(int argc, char* argv[]) {
  30.     int result;
  31.    
  32.     pid_t pid = fork();
  33.     if (pid == 0) {
  34.         getword();
  35.     }
  36.     else {
  37.         int status;
  38.         waitpid(pid, &status, 0);
  39.         result = (uint8_t) WEXITSTATUS(status);
  40.     }
  41.  
  42.     printf("%d\n", result);
  43. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement