Have a look at the following code:
#include<unistd.h>
#include<stdlib.h>
#include<stdio.h>
#include<string.h>
#include<sys/types.h>
main() {
int pipdes[2];
char buff[50];
const char parent[]="Parent Writes. Child Reads\n";
const char child[]="Child Writes. Parent Reads\n";
if(pipe(pipdes)==0) {
pid_t pid=fork();
if(pid<0)
printf("Error\n");
if(pid==0){
read(pipdes[0],buff,50);
printf("Parent: %s",buff);
write(pipdes[1], child, strlen(child));
exit(0);
}
else if(pid>0) {
write(pipdes[1], parent, strlen(parent));
wait(pid);
read(pipdes[0], buff, 50);
printf("Child: %s", buff);
}
}
else
printf("Error in pipe\n");
}
Now, here I have created just one pipe, but both the processes can read and write from. Aren’t pipes supposed to be uni-directional.
Also, when i put the conventional ‘close(pipdes[0])’ for parent and ‘close(pipdes[1])’ for child, the code doesn’t work, though I add the open(pipdes[0]) function later.
My concepts with UNIX and pipes is still raw, so I might come out a bit lame here, but please do assist.
On some systems, pipes can be bidirectional. But they don’t have to be, and any assumption that they will be is non-portable. In particular, they aren’t on Linux.
As it is, your code has a problem — both processes are trying to read from and write to the same pipe. The intended use for pipes is that the child writes and the parent reads, or vice versa. The current way you’re doing things works for you right now, because you’re reading and writing once and
waiting on the child. But when you loop while trying to do things the way you’re doing, you can’twait— and without synchronization, the child will often (but not always!) end up reading what it intended to send to the parent, and vice versa.If you want data flowing in both directions, you could use two pairs of pipes. Let’s call them
parent_pipeandchild_pipe. The parent would read fromparent_pipe[0]and write tochild_pipe[1], and the child would read fromchild_pipe[0]and write toparent_pipe[1].Alternatively, you could use a pair of UNIX sockets created with
socketpair(AF_LOCAL, SOCK_STREAM, 0, sockdes)(wheresockdesis what we renamedpipdesto, since it’s sockets now and not pipes). The child would read from and write tosockdes[0], and the parent would read from and write tosockdes[1]. Or vice versa.