...one of the most highly
regarded and expertly designed C++ library projects in the
world.
— Herb Sutter and Andrei
Alexandrescu, C++
Coding Standards
boost::process::std_in
// In header: <boost/process/io.hpp> unspecified std_in;
This property allows to set the input stream for the child process.
The file I/O simple redirects the stream to a file, for which the possible types are
boost::filesystem::path
std::basic_string<char_type>
const char_type*
FILE*
with char_type
being either char
or wchar_t
.
FILE* is explicitly added, so the process can easily redirect the output stream of the child to another output stream of the process. That is:
system("ls", std_in < stdin);
Warning | |
---|---|
If the launching and the child process use the input, this leads to undefined behaviour. |
A syntax like system("ls", std_out > std::cerr)
is not possible, due to the C++ implementation not providing access to the handle.
The valid expressions for this property are
std_in < file; std_in = file;
As explained in the corresponding section, the boost.process library provides a async_pipe class which can be used to communicate with child processes.
Note | |
---|---|
Technically the async_pipe works synchronous here, since no asio implementation is used by the library here. The async-operation will then however not end if the process is finished, since the pipe remains open. You can use the async_close function with on_exit to fix that. |
Valid expressions with pipes are these:
std_in < pipe; std_in = pipe;
Where the valid types for pipe
are the following:
Note that the pipe may also be used between several processes, like this:
pipe p; child c1("nm", "a.out", std_out>p); child c2("c++filt", std_in<p);
Asynchronous Pipe I/O classifies communication which has automatically handling of the asynchronous operations by the process library. This means, that a pipe will be constructed, the async_read/-write will be automatically started, and that the end of the child process will also close the pipe.
Valid types for pipe I/O are the following:
Valid expressions with pipes are these:
std_in < buffer; std_in = buffer; std_out > buffer; std_out = buffer; std_err > buffer; std_err = buffer; (std_out & std_err) > buffer; (std_out & std_err) = buffer;
Note | |
---|---|
It is also possible to get a future for std_in, by chaining another std::future<void> fut; boost::asio::io_context ios; std::string data; child c("prog", std_in < buffer(data) > fut, ios); fut.get();
|
Warning | |
---|---|
This feature requires |
The input stream can be closed, so it cannot be read from. This will lead to an error when attempted.
This can be achieved by the following syntax.
std_in < close; std_in = close; std_in.close();