C++ How to use pipe() & fork() with stdin and stdout to another program

Hi,
Program A: uses pipe()

I am able to read the stdout of PROGAM B (stdout got through system() command) into PROGRAM A using:

  • child
    -> dup2(fd[1], STDOUT_FILENO);
    -> execl("/path/PROGRAM B", "PROGRAM B", NULL);

  • parent
    -> char line[100];
    -> read(fd[0], line, 100);

Question:
---------
How to write to stdin of PROGRAM B from PROGRAM A?

  • should I use a different pipe?
  • how to I read stdin in PROGRAM B? using cin?

Thanks in advance,
Vivek

Found the solution.

Between two binaries, one pipe can serve only one way communication. YOU need two pipes for to and fro communication. You need one pipe to write data to STDIN of another program (B) and need another pipe to read the STDOUT from that another program (B).

Two best links on this:

Network Programming (CSE 533) | Unix Pipes
http://www.ecst.csuchico.edu/~hilzer/csci372/pdf/Chapter7.pdf

How to write to STDIN of PROGRAM B from PROGAM A:

In PROGRAM A:

int StartPipe(iosockinet &s, char PROGRAM_B_INPUT[])
{
    int fd1[2];
    int fd2[2];
    pid_t pid;
    char line[MAXLINE];

    if ( (pipe(fd1) < 0) || (pipe(fd2) < 0) )
    {
        cerr << "PIPE ERROR" << endl;
        return -2;
    }
    if ( (pid = fork()) < 0 )
    {
        cerr << "FORK ERROR" << endl;
        return -3;
    }
    else  if (pid == 0)     // CHILD PROCESS
    {
        close(fd1[1]);
        close(fd2[0]);

        if (fd1[0] != STDIN_FILENO)
        {
            if (dup2(fd1[0], STDIN_FILENO) != STDIN_FILENO)
            {
                cerr << "dup2 error to stdin" << endl;
            }
            close(fd1[0]);
        }

        if (fd2[1] != STDOUT_FILENO)
        {
            if (dup2(fd2[1], STDOUT_FILENO) != STDOUT_FILENO)
            {
                cerr << "dup2 error to stdout" << endl;
            }
            close(fd2[1]);
        }

        if ( execl("path/PROGRAM B", "PROGRAM B", (char *)0) < 0 )
        {
            cerr << "system error" << endl;
            return -4;
        }

        return 0;
    }
    else        // PARENT PROCESS
    {
        int rv;
        close(fd1[0]);
        close(fd2[1]);

        if ( write(fd1[1], PROGRAM_B_INPUT, strlen(PROGRAM_B_INPUT) ) != strlen(PROGRAM_B_INPUT) )
        {
            cerr << "READ ERROR FROM PIPE" << endl;
        }

        if ( (rv = read(fd2[0], line, MAXLINE)) < 0 )
        {
            cerr << "READ ERROR FROM PIPE" << endl;
        }
        else if (rv == 0)
        {
            cerr << "Child Closed Pipe" << endl;
            return 0;
        }

        cout << "OUTPUT of PROGRAM B is: " << line;

        return 0;
    }
    return 0;
}

  • should I use a different pipe?
  • Yes
  • how to I read stdin in PROGRAM B? using cin?

In PROGRAM B:

char line[1000];
read(STDIN_FILENO, line, MAXLEN);

I've got something along these lines where after the fork the child redirects stdin and stdout to 2 pipes to communicate with the parent. The child then calls execv to run another program sending output to a pipe and getting input from another pipe.

My issue is how can i tell when the child is expecting data on its input pipe(now stdin)?