Using ordinary pipes only, I want to create a full-duplex communication between two programs named A and B. The A program (A.c) will open a file, read its text and write the text to the write end of a pipe. The B program (B.c) written in a different C file will read the pipe contents written by A and count the number of characters. Then the B program will write a number of characters to a second pipe, and the A program will read the number of characters written by B and display it.
Here is Some Code: I have tried:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <sys/wait.h>
int main(int argc, char *argv[]) {
int fd[2];
char buf[] = "HELLO WORLD!", red[100];
if (pipe(fd))
{
perror("pipe");
return -1;
}
switch(fork()){
case -1:
perror("fork");
return -1;
case 0:
// child
close(fd[1]); // close write end
dup2(fd[0], STDIN_FILENO); // redirect stdin to read end
close(fd[0]);
execl("./child", NULL);
exit(0);
default:
close(fd[0]); // close read end
write(fd[1], buf, sizeof(buf)); // write to write end
close(fd[1]); // close write end
wait(NULL);
}
printf("\nEND~\n");
return 0;
}