Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <sys/wait.h>
- #include <assert.h>
- #include <stdlib.h>
- #include <unistd.h>
- #include <string.h>
- int main(int argc, char *argv[])
- {
- int pfd[2];
- int pfd_2[2];
- int x=0, y=0;
- pid_t cpid;
- struct liczby
- {
- int x;
- int y;
- };
- struct liczby bufor_dz, bufor_o;
- bufor_dz.x=0;
- bufor_dz.y=0;
- bufor_o.x=0;
- bufor_o.y=0;
- int suma=0;
- int buf;
- int suma_dziecko=0;
- if (pipe(pfd) == -1) { perror("pipe"); exit(EXIT_FAILURE); }
- if (pipe(pfd_2) == -1) { perror("pipe_2"); exit(EXIT_FAILURE); }
- cpid = fork();
- if (cpid == -1) { perror("fork"); exit(EXIT_FAILURE); }
- if (cpid == 0)
- {
- close(pfd[0]);
- scanf("%d %d", &bufor_dz.x, &bufor_dz.y);
- write(pfd[1], &bufor_dz, sizeof(struct liczby));
- close(pfd_2[1]);
- read(pfd_2[0], &suma_dziecko, sizeof(struct liczby));
- printf("%d\n", suma_dziecko);
- _exit(EXIT_SUCCESS);
- }
- else
- {
- close(pfd[1]);
- read(pfd[0], &bufor_o, sizeof(struct liczby));
- suma=bufor_o.x+bufor_o.y;
- close(pfd_2[0]);
- write(pfd_2[1], &suma, sizeof(int));
- wait(NULL);
- exit(EXIT_SUCCESS);
- }
- }
Add Comment
Please, Sign In to add comment