1 process-cpp {#mainpage}
4 process-cpp is a simple and straightforward wrapper around process creation and
5 control targeted towards linux. It helps both with handling child processes and with
6 interacting with the current process. Some of its features include:
8 - Thread-safe
get/
set/
unset operation on the current process
's environment.
9 - Throwing and non-throwing overloads of functions when system calls are involved.
10 - Seamless redirection of input, output and error streams of child processes.
11 - Type-safe interaction with the virtual proc filesystem, both for reading & writing.
13 The library's
main purpose is to assist in testing and when a software component
14 needs to carry out process creation/control tasks, e.g., a graphical shell. To
this end,
15 the library is extensively tested and tries to ensure fail-safe operation as much as possible.
33 posix::StandardStreamFlags()
34 .set(posix::StandardStream::stdin)
35 .set(posix::StandardStream::stdout));
38 EXPECT_TRUE(child.pid() > 0);
41 const std::string echo_value{
"42"};
42 child.
cin() << echo_value << std::endl;
43 std::string line; child.cout() >> line;
44 EXPECT_EQ(echo_value, line);
47 EXPECT_NO_THROW(child.send_signal(posix::Signal::sig_stop));
48 auto result = child.wait_for(posix::wait::Flag::untraced);
49 EXPECT_EQ(posix::wait::Result::Status::stopped,
51 EXPECT_EQ(posix::Signal::sig_stop,
52 result.detail.if_stopped.signal);
55 EXPECT_NO_THROW(child.send_signal(posix::Signal::sig_kill));
56 result = child.wait_for(posix::wait::Flag::untraced);
57 EXPECT_EQ(posix::wait::Result::Status::signaled,
59 EXPECT_EQ(posix::Signal::sig_kill,
60 result.detail.if_signaled.signal);
63 Adjusting OOM Score Values
64 --------------------------
68 posix::linux::proc::process::OomScoreAdj oom_score_adj
70 posix::linux::proc::process::OomScoreAdj::max_value()
77 EXPECT_EQ(posix::linux::proc::process::OomScoreAdj::max_value(),
80 posix::linux::proc::process::OomScore oom_score;
85 EXPECT_TRUE(is_approximately_equal(oom_score.value, posix::linux::proc::process::OomScoreAdj::max_value()));
std::ostream & cin()
Access this process's stdin.
CORE_POSIX_DLL_PUBLIC std::istream & cin() noexcept(true)
Access this process's stdin.
CORE_POSIX_DLL_PUBLIC ChildProcess fork(const std::function< posix::exit::Status()> &main, const StandardStream &flags)
fork forks a new process and executes the provided main function in the newly forked process...
CORE_POSIX_DLL_PUBLIC bool set(const std::string &key, const std::string &value, std::error_code &se) noexcept(true)
set will adjust the contents of the variable identified by key to the provided value.
int main(int argc, char *argv[])
CORE_POSIX_DLL_PUBLIC Process instance() noexcept(true)
Returns a Process instance corresponding to this process.
CORE_POSIX_DLL_PUBLIC std::ostream & cout() noexcept(true)
Access this process's stdout.
CORE_POSIX_DLL_PUBLIC bool unset(const std::string &key, std::error_code &se) noexcept(true)
unset removes the variable with name key from the environment.