#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
int main(int argc, char const *argv[])
{
int fd[2], fork_status1, fork_status2;
char buff[20], str_[32];
if(pipe(fd) == -1) {
fprintf(stderr, "pipe - error!\n");
exit(-1);
} else {
fork_status1 = fork();
if(fork_status1 == -1) {
fprintf(stderr, "fork1 - error!\n");
exit(-2);
} else if(fork_status1 == 0) {
fork_status2 = fork();
if(fork_status2 == -1) {
fprintf(stderr, "fork2 - error!\n");
exit(-2);
} else if(fork_status2 == 0) { // Child process(2)
printf("Write smth(2)...\n");
while(strcmp(str_, "exit")) {
fgets(str_, 30, stdin);
write(fd[1], str_, strlen(str_));
}
exit(0);
} else { // Child process(1)
printf("Write smth(1)...\n");
while(strcmp(str_, "exit")) {
fgets(str_, 30, stdin);
printf("%s", str_); //<-----------------------------------
write(fd[1], str_, strlen(str_));
}
exit(0);
}
} else { // Parent process
while(strcmp(buff, "exit")) {
read(fd[0], buff, strlen(str_));
if(strcmp(buff, "exit")) {
//printf("%s\n", buff);
}
}
close(fd[0]);
close(fd[1]);
exit(0);
}
}
return 0;
}
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
int main(int argc, char const *argv[])
{
int fd[2], fork_status1, fork_status2;
char buff[20], str_[32];
if(pipe(fd) == -1) {
fprintf(stderr, "pipe - error!\n");
exit(-1);
} else {
fork_status1 = fork();
if(fork_status1 == -1) {
fprintf(stderr, "fork1 - error!\n");
exit(-2);
} else if(fork_status1 == 0) {
fork_status2 = fork();
if(fork_status2 == -1) {
fprintf(stderr, "fork2 - error!\n");
exit(-2);
} else if(fork_status2 == 0) { // Child process(2)
printf("Write smth(2)...\n");
while(strcmp(str_, "exit")) {
fgets(str_, 30, stdin);
write(fd[1], str_, strlen(str_));
}
exit(0);
} else { // Child process(1)
printf("Write smth(1)...\n");
while(strcmp(str_, "exit")) {
fgets(str_, 30, stdin);
printf("%s", str_); //<-----------------------------------
write(fd[1], str_, strlen(str_));
}
exit(0);
}
} else { // Parent process
while(strcmp(buff, "exit")) {
read(fd[0], buff, strlen(str_));
if(strcmp(buff, "exit")) {
//printf("%s\n", buff);
}
}
close(fd[0]);
close(fd[1]);
exit(0);
}
}
return 0;
}