C++17 class to manage Unix child processes
Features:
Also:
Prerequisites:
This repository contains the child process library (childprocess.hpp, childprocess.cpp) together with a Boost.Test unit test program. To build and run the unit tests:
$ mkdir build
$ cd build
$ cmake ..
$ make
$ make test
Copy childprocess.hpp and childprocess.cpp to locations of your choise and add them to your build settings. Install and link with boost (see CMakeLists.txt for reference).
Reference code only. For complete examples refer to the unit test program (test.cpp).
Run make -j test
, wait for completion, and check exit status.
#include <childprocess.hpp>
auto chld = sdb::ChildProcess("/usr/bin/make", { "-j", "test" });
const auto status = child.join();
if (status!=0) {
// Failed
}
Run sed to convert Hello world
into Good night world
.
#include <childprocess.hpp>
auto chld = sdb::ChildProcess(
"/bin/sed",
{ "s/Hello/Good night/g" },
sdb::ChildProcess::IN | sdb::ChildProcess::OUT
);
auto in = chld.make_stdin([&data](std::ostream& os) {
os << "Hello world\n";
});
std::string input;
auto out = chld.get_stdout([&input](std::istream& is) {
input.assign(std::istreambuf_iterator<char>(is),std::istreambuf_iterator<char>());
});
in.get();
out.get();
const auto status = chld.join();
if (status!=0) {
// Failed
}
// Now input is "Good night world"
Wolfram Rösler • wolfram@roesler-ac.de • https://gitlab.com/wolframroesler • https://twitter.com/wolframroesler • https://www.linkedin.com/in/wolframroesler/