The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > High Level Programming
Google UNIX.COM



View Single Post in UNIX Forums - Click on the Thread or Permalink to View Entire Thread -->
  #1 (permalink)  
Old 05-14-2008
vvaidyan vvaidyan is offline
Registered User
 

Join Date: Mar 2008
Posts: 15
How to clear the content of a pipe (STDIN) after it is written to another program?

PROGRAM A <-> PROGRAM B
PROGRAM A sends data as STDIN ro PROGRAM B and when PROGRAM B is executed from PROGRAM A, it sends output back to PROGRAM A. This is implemented using 2 pipes (fd1 & fd2).

The above process happens in a loop and during the second run, the previous data that had been written as STDIN to PROGRAM B, it does not get cleared. The input data on STDIN to PROGRAM B just gets overwritten and the extra characters just stay there.

How to clear the STDIN to PROGRAM B? Existing code as follows:

PROGRAM A
Code:
int StartPipe(iosockinet &s, char input_to_program_b[])
{
    int fd1[2];
    int fd2[2];
    pid_t pid;
    char line[MAXLINE];


    if (signal(SIGPIPE, sig_pipe) == SIG_ERR)
    {
        cerr << "signal error" << endl;
        return -1;
    }

    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], input_to_program_b, strlen(input_to_program_b) ) != strlen(input_to_program_b) )
        {
            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;
        }

        // PRINTING OUT THE RESULT ON SOCKET
        s << line << endl;

        return 0;
    }
    return 0;
}
The PROGRAM A code shown above is in a while condition loop, which can run more than once not exiting the program.

PROGRAM B
Code:
# include <stdio.h>
# include <stdlib.h>
# include <iostream>
# include <string>
# include <sstream>

# define MAXLEN 10000

using namespace std;

int
main(int argc, char * argv[])
{

        char line[MAXLEN] = {0};

        string output;
        string echo_output;
        int line_length;

        read(STDIN_FILENO, line, MAXLEN);

        output = line;
        echo_output = "echo " + output;
        system(echo_output.c_str());

}
Just to check if correct data is got as input to PROGRAM B, we are just reading STDIN and echoing back on STDOUT. Previous data (without PROGRAM A exiting) stays on STDIN of PROGRAM B, which needs to be cleared before writing new input data to STDIN of PROGRAM B.

Thanks,
vivek
Reply With Quote
Forum Sponsor