Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdlib.h>
- #include <stdio.h>
- #include <limits.h>
- #include <unistd.h>
- #include <sys/wait.h>
- #include <string.h>
- #include <ctype.h>
- #include <time.h>
- void
- output(int i, time_t t) {
- struct tm *timeinfo;
- timeinfo = localtime (&t);
- fflush(stdout);
- printf(" %d ", i);
- switch (i) {
- case 3:
- printf("S:%d\n", timeinfo->tm_sec);
- break;
- case 2:
- printf("M:%d\n", timeinfo->tm_min);
- break;
- case 1:
- printf("H:%d\n", timeinfo->tm_hour);
- break;
- }
- return ;
- }
- int
- main(void)
- {
- int pid, st;
- time_t t = time( NULL);
- int fd[2];
- pipe(fd);
- for (int i = 0; i < 3; i++) {
- pid = fork();
- if (pid != 0) {
- if (i == 0) {
- close(fd[0]);//close for reading
- time_t t = time( NULL);
- write(fd[1], &t, sizeof(t));
- write(fd[1], &t, sizeof(t));
- write(fd[1], &t, sizeof(t));
- while (wait(&st) != -1);
- close(fd[1]);
- return 0;
- } else {
- close(fd[1]);//close for writing
- fflush(stdout);
- time_t t;
- read(fd[0], &t, sizeof(t));
- while (wait(&st) != -1);
- output(i, t);
- close(fd[0]);
- return 0;
- }
- } else if (i < 2){
- continue;
- } else {//правнук
- close(fd[1]);//close for writing
- fflush(stdout);
- time_t t;
- read(fd[0], &t, sizeof(t));
- output(i+1, t);
- close(fd[0]);
- return 0;
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement