Advertisement
wowonline

Untitled

Dec 11th, 2021
771
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <unistd.h>
  2. #include <stdio.h>
  3. #include <sys/wait.h>
  4. #include <stdlib.h>
  5.  
  6. enum
  7. {
  8.     AMOUNT_OF_CHILDREN = 3,
  9.     STRING_SIZE = 9,
  10.     INTEGER_BASE = 10
  11. };
  12.  
  13. void
  14. support(int cond)
  15. {
  16.     char string[STRING_SIZE] = {0};
  17.     if (read(0, string, sizeof(string)) !=sizeof(string)) {
  18.         _exit(1);
  19.     }
  20.     int num = strtol(string, 0, INTEGER_BASE);
  21.     printf("%d %d\n", cond, num * num);
  22.     fflush(stdout);
  23. }
  24.  
  25. int
  26. main(void)
  27. {
  28.     pid_t ppid, pid;
  29.     pid_t arr[AMOUNT_OF_CHILDREN] = {0};
  30.     ppid = getpid();
  31.    
  32.     for (int i = 0; i < AMOUNT_OF_CHILDREN; ++i) {
  33.         if (getpid() == ppid) {
  34.             pid = fork();
  35.             if (pid == -1) {
  36.                 _exit(1);
  37.             }
  38.             else if (pid == 0) {
  39.                 arr[i] = getpid();
  40.             }
  41.         }
  42.     }
  43.  
  44.     if (getpid() == ppid) {
  45.         while (wait(NULL) > 0) {}
  46.         _exit(0);
  47.     } else {
  48.         for (int i = 1; i <= AMOUNT_OF_CHILDREN; ++i) {
  49.             if (getpid() == arr[i-1]) {
  50.                 support(i);
  51.             }
  52.         }
  53.     }
  54.  
  55.     return 0;
  56. }
Advertisement
RAW Paste Data Copied
Advertisement