Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <unistd.h>
- #include <ctype.h>
- #include <sys/wait.h>
- void kill_process() {
- printf("\nZamykanie programu...\n");
- exit(0);
- }
- int main(void) {
- int pp1 = 0, pp2 = 0, pipe1[2], pipe2[2], pipe3[2], pipe4[2];
- char buf, end = 8;
- _Bool receiving = 0;
- pipe(pipe1);
- pipe(pipe2);
- pipe(pipe3);
- pipe(pipe4);
- if ((pp1 = fork())) {
- if ((pp2 = fork())) {
- signal(SIGTERM, kill_process);
- // parent
- close(pipe1[1]);
- close(pipe2[0]);
- close(pipe3[1]);
- close(pipe4[0]);
- while (read(pipe1[0], &buf, 1) > 0) {
- if (!receiving)
- printf("1. PM:%i\n", getpid());
- receiving = 1;
- write(pipe2[1], &buf, 1);
- if (buf == end) {
- receiving = 0;
- while (read(pipe3[0], &buf, 1) > 0) {
- if (!receiving)
- printf("3. PM:%i\n", getpid());
- receiving = 1;
- write(pipe4[1], &buf, 1);
- if (buf == end) {
- receiving = 0;
- break;
- }
- }
- }
- }
- close(pipe1[0]);
- wait(NULL);
- exit(EXIT_SUCCESS);
- } else {
- // pp2
- close(pipe2[1]);
- close(pipe3[0]);
- while (read(pipe2[0], &buf, 1) > 0) {
- if (!receiving)
- printf("2. PP2:%i\n", getpid());
- receiving = 1;
- buf = tolower(buf);
- write(pipe3[1], &buf, 1);
- if (buf == end)
- receiving = 0;
- }
- }
- } else {
- // pp1
- close(pipe1[0]);
- close(pipe4[1]);
- char str[100] = "";
- while (1) {
- scanf("%s", str);
- if (strcmp("q", str) == 0) {
- kill(getppid(), SIGTERM);
- break;
- };
- write(pipe1[1], str, strlen(str));
- write(pipe1[1], &end, 1);
- while (read(pipe4[0], &buf, 1) > 0) {
- if (!receiving)
- printf("4. PP1:%i\n", getpid());
- receiving = 1;
- write(STDOUT_FILENO, &buf, 1);
- if (buf == end) {
- receiving = 0;
- write(STDOUT_FILENO, "\n", 1);
- break;
- }
- }
- }
- close(pipe1[1]);
- close(pipe4[0]);
- exit(0);
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement