Hi, folks
I wish to make the conveyor, but i can't receive the answer
before I shall close a pipe.
For example:
int
main ()
{
int rpipe [2];
int wpipe [2];
::pipe (rpipe);
::pipe (wpipe);
pid_t _pid;
enum { RD, WR };
if (0 == (_pid=::fork()))
{
::close (rpipe [WR]);
::dup2 (rpipe [RD], STDIN_FILENO); // read rpipe through stdin
::close (rpipe [RD]);
::close (wpipe [RD]);
::dup2 (wpipe [WR], STDOUT_FILENO); // write stdout to wpipe
::close (wpipe [WR]);
::execl ("/usr/bin/tr", "tr", "A-Z", "a-z", 0);
cout << ::strerror (errno) << endl;
return errno;
} else {
::close (rpipe [RD]);
::close (wpipe [WR]);
std::string line;
while ( std::getline (cin, line)) {
::write ( rpipe [WR], line.c_str(), line.size () );
::write ( rpipe [WR], "\n" , sizeof("\n") );
///::close ( rpipe [WR]);
/// With close() I read; without close() I blocked
/// But close() destroy my cool conveyor!
/// How to me to avoid it?
char buf [1024];
int n = ::read ( wpipe [RD], buf, 1024 );
buf [n] = '\0';
cout << buf << endl;
}
}
}
--
www.andr.mobi