简体   繁体   中英

Pipe buffer size in boost process

I am using boost::process to read asynchronously the output of a console application in Windows. I noticed that the reading events is triggered after about 4k of data every-time.

If I set my buffer 'buf' to a small value nothing changes: the event is triggered multiple times ONLY after 4k of data has been transferred.

As per my understanding this could be a safe mechanism used in Windows to avoid dead-lock while reading from the pipe.

Is there any way in boost::process to change the size of the buffer used by the PIPE to transfer the data?

   #include <boost/process.hpp>
   #include <boost/asio.hpp>

   using namespace boost::process;
   boost::asio::io_service ios;

   std::vector<char> buf(200);
   async_pipe ap(ios);

   void read_from_buffer(const boost::system::error_code &ec, std::size_t size)
   {
    if (ec)
    {
        std::cout << "error" << std::endl;
        return;
    }

    std::cout << "--read-- " << size << std::endl;
    for (size_t i = 0; i < size; i++) std::cout << buf[i];
    std::cout << std::endl;


   ap.async_read_some(boost::asio::buffer(buf), read_from_buffer);
   }


   int main()
   {    
    child c("MyApp.exe --args", std_out > ap);  
    ap.async_read_some(boost::asio::buffer(buf), read_from_buffer);
    ios.run();
    int result = c.exit_code(); 
   }

You might have to control the "sending" side (so, MyApp.exe ).

On UNIX there's stdbuf (using setvbuff ), unbuffer and similar. Tools might have some support built-in (eg grep --line-buffered ).

On Windows, I'm not sure. Here's some pointers: Disable buffering on redirected stdout Pipe (Win32 API, C++)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM