The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > High Level Programming
.
google unix.com



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

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
AIX 5.3 - There is no process to read data written to a pipe vigsgb UNIX for Advanced & Expert Users 6 05-22-2009 04:59 AM
How to write to stdin of another program (program A -> [stdin]program B) vvaidyan UNIX for Dummies Questions & Answers 3 08-02-2008 05:21 PM
Perform action file name written to the pipe fed.linuxgossip Shell Programming and Scripting 2 07-23-2008 10:38 AM
C++ How to use pipe() & fork() with stdin and stdout to another program vvaidyan High Level Programming 2 05-16-2008 07:30 PM
How to write to stdin of another program (program A -> [stdin]program B) vvaidyan High Level Programming 1 04-30-2008 01:44 PM

 
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
Prev Previous Post   Next Post Next
  #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
 

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

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

BB 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 -4. The time now is 11:01 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0