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.
21 // Fork and run a simple echo:
22 posix::ChildProcess child = posix::fork(
29 std::cout << line << std::endl;
33 posix::StandardStreamFlags()
34 .set(posix::StandardStream::stdin)
35 .set(posix::StandardStream::stdout));
37 // Check that the resulting process has a valid pid.
38 EXPECT_TRUE(child.pid() > 0);
40 // Check on echo functionality.
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);
46 // Stop the process and synchronize with the process changing state.
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);
54 // Kill the stopped process and synchronize to its state change.
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 --------------------------
67 // Setup the manipulator with a well-known value.
68 posix::linux::proc::process::OomScoreAdj oom_score_adj
70 posix::linux::proc::process::OomScoreAdj::max_value()
72 // Apply the manipulator to the current process
73 EXPECT_NO_THROW(posix::this_process::instance() << oom_score_adj);
74 // Read back the manipulators value for the current process
75 EXPECT_NO_THROW(posix::this_process::instance() >> oom_score_adj);
76 // And check that applying the manipulator was successful.
77 EXPECT_EQ(posix::linux::proc::process::OomScoreAdj::max_value(),
79 // Instantiate the observer...
80 posix::linux::proc::process::OomScore oom_score;
81 // ... and fill in its value for the current process.
82 EXPECT_NO_THROW(posix::this_process::instance() >> oom_score);
83 // Check that applying the manipulator before results in adjustments to the
85 EXPECT_TRUE(is_approximately_equal(oom_score.value, posix::linux::proc::process::OomScoreAdj::max_value()));