The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > High Level Programming
Google UNIX.COM
Home Forums Register Rules & FAQ Members List Arcade Search Today's Posts Mark Forums Read


High Level Programming Post questions about C, C++, Java, SQL, and other programming languages here.


Other UNIX.COM Threads You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
C++ How to use pipe() & fork() with stdin and stdout to another program vvaidyan High Level Programming 2 05-16-2008 04:30 PM
How to write to stdin of another program (program A -> [stdin]program B) vvaidyan High Level Programming 1 04-30-2008 10:44 AM
How to write to stdin of another program (program A -> [stdin]program B) vvaidyan UNIX for Dummies Questions & Answers 0 04-30-2008 09:36 AM
Perform action file name written to the pipe fed.linuxgossip Shell Programming and Scripting 1 03-09-2008 03:42 PM
AIX 5.3 - There is no process to read data written to a pipe vigsgb UNIX for Advanced & Expert Users 4 06-21-2006 01:09 AM

Reply
 
Submit Tools LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 05-14-2008
Registered User
 

Join Date: Mar 2008
Posts: 15
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiReddit! Stumble this Post!Spurl this Post!
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
  #2 (permalink)  
Old 05-14-2008
Registered User
 

Join Date: Oct 2007
Location: USA
Posts: 356
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiReddit! Stumble this Post!Spurl this Post!
Re-direct STDIN

Before reading data on the second pipe the parent process needs to redirect its standard input to fd2[0].

Code:
dup2(fd2[0], STDIN_FILENO);
Reply With Quote
  #3 (permalink)  
Old 05-14-2008
Registered User
 

Join Date: Mar 2008
Posts: 15
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiReddit! Stumble this Post!Spurl this Post!
Thanks for the reply shamrock, but it is not solving the problem.

I am able to read the output of PROGRAM B inside PROGRAM A through char line[]. I am receiving output. But the problem is, the output in the previous run (until program exited) is not getting erased. Able to see new run results overwritten on previous results through char line[] of PROGRAM A AFTER read(fd2[0], line, MAXLINE).

Thanks,
Vivek
Reply With Quote
  #4 (permalink)  
Old 05-14-2008
Registered User
 

Join Date: Oct 2007
Location: USA
Posts: 356
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiReddit! Stumble this Post!Spurl this Post!
The process needs to read() data off the pipe in order to clear it otherwise it blocks and stays in the pipe. Invoke read() after exec'ing program B in the child process.
Reply With Quote
  #5 (permalink)  
Old 05-14-2008
Registered User
 

Join Date: Mar 2008
Posts: 15
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiReddit! Stumble this Post!Spurl this Post!
can you please tell me the syntax?

According to the code above, am already reading from the pipe after exec'ing program B. Can you please point in code if I had done anything wrong?

Thanks,
Vivek

Last edited by vvaidyan : 05-14-2008 at 01:01 PM.
Reply With Quote
Google UNIX.COM
Reply



Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT -7. The time now is 02:38 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008 The CEP Blog All Rights Reserved -Ad Management by RedTyger

Search Engine Optimization by vBSEO 3.1.0

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102