Advertisement
Guest User

Untitled

a guest
Nov 17th, 2019
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.80 KB | None | 0 0
  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. #include <limits.h>
  4. #include <unistd.h>
  5. #include <sys/wait.h>
  6. #include <string.h>
  7. #include <ctype.h>
  8. #include <time.h>
  9.  
  10.  
  11. void
  12. output(int i, time_t t) {
  13.     struct tm *timeinfo;
  14.     timeinfo = localtime (&t);
  15.     fflush(stdout);
  16.     printf("  %d  ", i);
  17.  
  18.     switch (i) {
  19.         case 3:
  20.             printf("S:%d\n", timeinfo->tm_sec);
  21.             break;
  22.         case 2:
  23.             printf("M:%d\n", timeinfo->tm_min);
  24.             break;
  25.         case 1:
  26.             printf("H:%d\n", timeinfo->tm_hour);
  27.             break;
  28.     }
  29.  
  30.     return ;
  31. }
  32.  
  33. int
  34. main(void)
  35. {
  36.     int pid, st;
  37.     time_t t = time( NULL);
  38.  
  39.     int fd[2];
  40.     pipe(fd);
  41.     for (int i = 0; i < 3; i++) {
  42.         pid = fork();
  43.         if (pid != 0) {
  44.             if (i == 0) {
  45.                 close(fd[0]);//close for reading
  46.                 time_t t = time( NULL);
  47.                 write(fd[1], &t, sizeof(t));
  48.                 write(fd[1], &t, sizeof(t));
  49.                 write(fd[1], &t, sizeof(t));
  50.                 while (wait(&st) != -1);
  51.                 close(fd[1]);
  52.                 return 0;
  53.             } else {
  54.                 close(fd[1]);//close for writing
  55.                 fflush(stdout);
  56.                 time_t t;
  57.                 read(fd[0], &t, sizeof(t));
  58.                 while (wait(&st) != -1);
  59.                 output(i, t);
  60.                 close(fd[0]);
  61.                 return 0;
  62.             }
  63.         } else if (i < 2){
  64.             continue;
  65.         } else {//правнук
  66.                 close(fd[1]);//close for writing
  67.                 fflush(stdout);
  68.                 time_t t;
  69.                 read(fd[0], &t, sizeof(t));
  70.                 output(i+1, t);
  71.                 close(fd[0]);
  72.                 return 0;
  73.         }
  74.     }
  75.  
  76.     return 0;
  77. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement