Advertisement
Guest User

Untitled

a guest
Nov 17th, 2017
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.15 KB | None | 0 0
  1. #include <time.h>
  2. #include <stdio.h>
  3. #include <unistd.h>
  4. #include <sys/types.h>
  5. #include <sys/wait.h>
  6.  
  7. struct tm *
  8. psbody(int fd)
  9. {
  10.     time_t t;
  11.     read(fd, &t, sizeof(t));
  12.     close(fd);
  13.     struct tm *stm = localtime(&t);
  14.     return stm;
  15. }
  16.  
  17. int
  18. main(void)
  19. {
  20.     int fds[2];
  21.     pipe(fds);
  22.     time_t t = time(NULL);
  23.     write(fds[1], &t, sizeof(t));
  24.     write(fds[1], &t, sizeof(t));
  25.     write(fds[1], &t, sizeof(t));
  26.     pid_t ps;
  27.     if (!(ps = fork())) {
  28.         close(fds[1]);
  29.         pid_t pg;
  30.         if (!(pg = fork())) {
  31.             pid_t pgg;
  32.             if (!(pgg = fork())) {
  33.                 struct tm *stmgg = psbody(fds[0]);
  34.                 printf("S:%02d\n", stmgg->tm_sec);
  35.                 _exit(0);
  36.             }
  37.             waitpid(pgg, NULL, 0);
  38.             struct tm *stmg = psbody(fds[0]);
  39.             printf("M:%02d\n", stmg->tm_min);
  40.             _exit(0);
  41.         }
  42.         waitpid(pg, NULL, 0);
  43.         struct tm *stms = psbody(fds[0]);
  44.         printf("H:%02d\n", stms->tm_hour);
  45.         _exit(0);
  46.     }    
  47.     waitpid(ps, NULL, 0);
  48.     close(fds[0]);
  49.     close(fds[1]);
  50.     return 0;        
  51. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement