I have been banging my head with this one for a bit and decided it is time to ask for help...

There are two programs here, which I shall refer to as Parent and Child. Child is a simple program that outputs a line of text, then waits for input from the user. Parent is supposed to launch Child, grab it's output, perform some transformation on it, then send it back to Child as its input.

This seems like a relatively easy task, but I am new to programming in Linux (in C anyway), so I could definitely be overlooking something. I get Segmentation Faults (because, apparently, I am attempting to write to an invalid file descriptor), or I lock up the program because Parent is waiting for Child and Child is waiting for Parent (this is what I have gathered through strace output anyway).

Assume this is Child:

Code:
int main()
{
    char buf[80];

    printf("some text");
    gets(buf);
}
For Parent, I have been using something like this:

Code:
int main(int argc, char** argv)
{
        pid_t pid;
        int rv;
        int writepipe[2]; /* parent -> child */
        int readpipe[2]; /* child -> parent */
        char* filename;
        char buf[80];
        int i;
        int bytes;

        filename = argv[1];

        if(pipe(writepipe) < 0 || pipe(readpipe) < 0)
        {
                printf("error creating pipes. quitting...");
                exit(1);
        }

#define PARENT_READ     readpipe[0]
#define CHILD_WRITE     readpipe[1]
#define CHILD_READ      writepipe[0]
#define PARENT_WRITE    writepipe[1]

        if((pid=fork()) == -1)
        {
                printf("fork error. quitting...");
                exit(1);
        }

        if(pid)
        {
                //i am the parent
                close(CHILD_READ);
                close(CHILD_WRITE);

                do
                {
                        bytes = read(PARENT_READ, buf, sizeof(buf));

                        for(i=0;i<sizeof(buf);i++)
                        {
                                if(buf[i] == '\x0a')
                                        break;
                                fprintf(stderr, "%c", buf[i]);
                                //fprintf(PARENT_WRITE, "%c", buf[i]);
                                buf[i] = buf[i]+1;
                        }
                } while( bytes < 0 );

                //write data to child
                fprintf(PARENT_WRITE, "%s", buf);

                wait(&rv);
        }
        else
        {
                //i am the child
                close(PARENT_READ);
                close(PARENT_WRITE);

                dup2(CHILD_WRITE, 1);
                dup2(CHILD_READ, 0);

                if(execl(filename, filename, NULL) == -1)
                {
                        printf("execl error.  quitting...");
                        exit(1);
                }
        }
        exit(0);
}
Seems pretty straightforward to me, but I am obviously missing something. Any help would be appreciated.

Thanks,

Skyler