#include <boost/asio.hpp> 
#define BOOST_PROCESS_WINDOWS_USE_NAMED_PIPE 
#include <boost/process.hpp> 
#include <boost/array.hpp> 
#include <boost/bind.hpp> 
#include <boost/assign/list_of.hpp> 
#include <string> 
#include <vector> 
#include <iostream> 

using namespace boost::process; 
using namespace boost::asio; 

io_service ioservice; 
boost::array<char, 4096> buf; 

#if defined(BOOST_POSIX_API) 
posix::stream_descriptor in(ioservice); 
#elif defined(BOOST_WINDOWS_API) 
windows::stream_handle in(ioservice); 
#endif 

void begin_read(); 
void end_read(const boost::system::error_code &ec, std::size_t bytes_transferred); 

int main() 
{ 
  std::string exec = find_executable_in_path("hostname"); 
  std::vector<std::string> args = boost::assign::list_of("hostname"); 
  context ctx; 
  ctx.environment = self::get_environment(); 
  ctx.stdout_behavior = capture_stream(); 
  child c = launch(exec, args, ctx); 
  pistream &is = c.get_stdout(); 
  in.assign(is.handle().release()); 
  begin_read(); 
  ioservice.run(); 
} 

void begin_read() 
{ 
  in.async_read_some(boost::asio::buffer(buf), 
    boost::bind(&end_read, placeholders::error, placeholders::bytes_transferred)); 
} 

void end_read(const boost::system::error_code &ec, std::size_t bytes_transferred) 
{ 
  if (!ec) 
  { 
    std::cout << std::string(buf.data(), bytes_transferred) << std::flush; 
    begin_read(); 
  } 
} 