The_Law

Untitled

Nov 12th, 2018
408
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.04 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <unistd.h>
  3. #include <sys/wait.h>
  4. #include <string.h>
  5.  
  6. enum
  7. {
  8.     NUM_CNT = 3,
  9. };
  10.  
  11. void
  12. outpt(int id, int num)
  13. {
  14.     char *buf;
  15.     sprintf(buf, "%d %d\n", id, num * num);
  16.     write(STDOUT_FILENO, buf, strlen(buf) * sizeof(buf[0]));
  17.     fflush(stdout);
  18. }
  19.  
  20. int
  21. main(int argc, char *argv[])
  22. {
  23.     int arr[NUM_CNT];
  24.     for (int i = 0; i < NUM_CNT; ++i) {
  25.         scanf("%d", &arr[i]);
  26.     }
  27.     int pid;
  28.     if ((pid = fork()) < 0) {
  29.         return 1;
  30.     } else if (!pid) {
  31.         outpt(1, arr[0]);
  32.         return 0;
  33.     } else {
  34.         if ((pid = fork()) < 0) {
  35.             return 1;
  36.         } else if (!pid) {
  37.             outpt(2, arr[1]);
  38.             return 0;
  39.         } else {
  40.             if ((pid = fork()) < 0) {
  41.                 return 1;
  42.             } else if (!pid) {
  43.                 outpt(3, arr[2]);
  44.                 return 0;
  45.             } else {
  46.                 wait(NULL);
  47.                 wait(NULL);
  48.                 wait(NULL);
  49.             }
  50.         }
  51.     }
  52.     return 0;
  53. }
Advertisement
Add Comment
Please, Sign In to add comment