Advertisement
wowonline

Untitled

Dec 11th, 2021
675
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 = 8,
  10.     INTEGER_BASE = 10
  11. };
  12.  
  13. void
  14. support(int cond)
  15. {
  16.     char string[STRING_SIZE];
  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 == 0) {
  36.                 arr[i] = getpid();
  37.             }
  38.         }
  39.     }
  40.  
  41.     if (getpid() == ppid) {
  42.         while (wait(NULL) > 0) {}
  43.         _exit(0);
  44.     } else {
  45.         for (int i = 1; i <= AMOUNT_OF_CHILDREN; ++i) {
  46.             if (getpid() == arr[i-1]) {
  47.                 support(i);
  48.             }
  49.         }
  50.     }
  51.  
  52.     return 0;
  53. }
Advertisement
RAW Paste Data Copied
Advertisement