| [ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
pipestream stream classes provide the services of the UNIX
system calls pipe and socketpair and the C library
function popen. ipipestream, opipestream, and
iopipestream are obtained by simply deriving from
isockstream, osockstream and iosockstream
respectively. See section 8. sockstream Classes for details.
In what follows,
ip is an ipipestream object
op is an opipestream object
iop is an iopipestream object
cmd is a char* denoting an executable like "wc"
ty is of type sockbuf::type indicating the type of the
connection
proto is an int denoting a protocol number
ipipestream ip(cmd)
ipipestream object ip such that the output of
the command cmd is available as input through ip.
opipestream op(cmd)
opipestream object op such that the input for
the command cmd can be send through op.
iopipestream iop(cmd)
iopipestream object iop such that the input
and the output to the command cmd can be sent and received
through iop.
iopipestream iop(ty, proto)
iopipestream object iop whose socket is a
socketpair of type ty with protocol number proto.
ty defaults to sockbuf::sock_stream and proto
defaults to 0. Object iop can be used either as a pipe or
as a socketpair.
iop.pid ()
iopipestream::fork ()
fork() is a static function of class iopipestream.
fork() forks the current process and appropriately sets the
cpid field of the iopipestream objects that have not
forked yet.
| 9.1 pipestream as pipe | How to use pipestream as pipe? | |
| 9.2 pipestream as socketpair | How to use pipestream as socketpair? | |
| 9.3 pipestream as popen | How to use pipestream as popen? |
| [ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
pipe is used to communicate between parent and child processes in
the unix domain.
The following example illustrates how to use iopipestream class as
a pipe. The parent sends the string "I am the parent" to the
child and receives the string "I am the child" from child. The child, in
turn, receives the string "I am the parent" from parent and sends the
string "I am the child" to the parent. Note the same iopipestream
object is used for input and output in each process.
#include <pipestream.h>
int main()
{
iopipestream p;
if ( p.fork() ) {
char buf[128];
p << "I am the parent\n" << flush;
cout << "parent: ";
while(p >> buf)
cout << buf << ' ';
cout << endl;
}else {
char buf[128];
p.getline(buf, 127);
cout << "child: " << buf << endl;
p << "I am the child\n" << flush;
}
return 0;
}
|
| [ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
Like pipes, socketpairs also allow communication between parent and child processes. But socketpairs are more flexible than pipes in the sense that they let the users choose the socket type and protocol.
The following example illustrates the use of iopipestream class as
a socketpair whose type is sockbuf::sock_dgram. The parent
sends the string "I am the parent" to the child and receives the string
"I am the child" from the child. The child, in turn, receives and sends
the strings "I am the parent" and "I am the child" respectively from and
to the parent. Note in the following example that the same
iopipestream object is used for both the input and the output in
each process.
#include <pipestream.h>
int main()
{
iopipestream p(sockbuf::sock_dgram);
if ( iopipestream::fork() ) {
char buf[128];
p << "I am the parent\n" << flush;
p.getline(buf, 127);
cout << "parent: " << buf << endl;
}else {
char buf[128];
p.getline(buf, 127);
cout << "child: " << buf << endl;
p << "I am the child\n" << flush;
}
return 0;
}
|
| [ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
popen is used to call an executable and send inputs and
outputs to that executable. For example, the following example
executes "/bin/date", gets its output, and prints it to stdout.
#include <pipestream.h>
int main ()
{
char buf[128];
ipipestream p("/bin/date");
p.getline (buf, 127);
cout << buf << endl;
return 0;
}
|
Here is an example that prints "Hello World!!" on stdout. It uses
opipestream object.
#include <pipestream.h>
int main ()
{
opipestream p("/bin/cat");
p << "Hello World!!\n" << endl;
return 0;
}
|
The following example illustrates the use of iopipestream for
both input and output.
#include <pipestream.h>
int main()
{
char buf[128];
iopipestream p("lpc");
p << "help\nquit\n" << flush;
while ( p.getline(buf, 127) ) cout << buf << endl;
return 0;
}
|
| [ << ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |