![]() |
|
|
|||||||
| 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 |
![]() |
|
|
Submit Tools | LinkBack | Thread Tools | Search this Thread | Display Modes |
|
|||
|
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;
}
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());
}
Thanks, vivek |
| Forum Sponsor | ||
|
|
|
|||
|
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 |
|
|||
|
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. |
|||
| Google UNIX.COM |