Have a look at the following code:
#include
#include
#include
#include
#include
In POSIX.1-2001, pipes are unidirectional. From the man page:
pipe() creates a pair of file descriptors, pointing to a pipe inode, and places them in the array pointed to by filedes. filedes[0] is for reading, filedes[1] is for writing.
By the way, your use of fork is wrong: fork returns pid>0 for the parent and pid==0 for the child. pid<0 means there was an error.