Webmaster  |  Imprint 
C++ Server Pages
Main  |  License  |  Documentation  |  Download 

pipestream.cpp

Example: pipestream.cpp application


/**
 * This demo shows, how to create a child-process, use a pipe to signal
 * from child to parent and another pipe to send a datastream.
 */

#include <cxxtools/pipestream.h>
#include <cxxtools/fork.h>

int main(int argc, char* argv[])
{
  try
  {
    // create pipe, where child signals, that he is initialized
    cxxtools::Pipe pipe;

    cxxtools::Pipestream pstream;

    // fork child-process
    cxxtools::Fork fork;

    if (fork.parent())
    {
      pipe.closeWriteFd();
      pstream.closeWriteFd();

      std::cout << "waiting for child to become ready" << std::endl;

      char ch;
      pipe.out().read(&ch, 1); // wait for child to get ready
      std::cout << "child is ready - he sent '" << ch << '\'' << std::endl;

      // now we copy everything, the child sends through the stream
      std::cout << pstream.rdbuf() << std::flush;

      fork.wait();

      std::cout << "child terminated normally" << std::endl;
    }
    else // child
    {
      pipe.closeReadFd();
      pstream.closeReadFd();

      // we do some long initialization:
      ::sleep(1);

      char ch = 'a';
      pipe.in().write(&ch, 1);

      // make another break
      ::sleep(1);

      pstream << "Hello World!" << std::endl;
    }
  }
  catch (const std::exception& e)
  {
    std::cerr << e.what() << std::endl;
  }
}
Copyright © 2008 The Tntnet Development Team
Tntnet 1.6