process-cpp  3.0.0
A simple convenience library for handling processes in C++11.
/root/process-cpp/process-cpp-3.0.1/README.md
Go to the documentation of this file.
1 process-cpp {#mainpage}
2 ===========
3 
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:
7 
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.
12 
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.
16 
17 A simple echo
18 -------------
19 
20 ~~~~~~~~~~~~~{.cpp}
21 // Fork and run a simple echo:
22 posix::ChildProcess child = posix::fork(
23  []()
24  {
25  std::string line;
26  while(true)
27  {
28  std::cin >> line;
29  std::cout << line << std::endl;
30  }
31  return EXIT_FAILURE;
32  },
33  posix::StandardStreamFlags()
34  .set(posix::StandardStream::stdin)
35  .set(posix::StandardStream::stdout));
36 
37 // Check that the resulting process has a valid pid.
38 EXPECT_TRUE(child.pid() > 0);
39 
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);
45 
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,
50  result.status);
51 EXPECT_EQ(posix::Signal::sig_stop,
52  result.detail.if_stopped.signal);
53 
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,
58  result.status);
59 EXPECT_EQ(posix::Signal::sig_kill,
60  result.detail.if_signaled.signal);
61 ~~~~~~~~~~~~~
62 
63 Adjusting OOM Score Values
64 --------------------------
65 
66 ~~~~~~~~~~~~~{.cpp}
67 // Setup the manipulator with a well-known value.
68 posix::linux::proc::process::OomScoreAdj oom_score_adj
69 {
70  posix::linux::proc::process::OomScoreAdj::max_value()
71 };
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(),
78  oom_score_adj.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
84 // OOM score.
85 EXPECT_TRUE(is_approximately_equal(oom_score.value, posix::linux::proc::process::OomScoreAdj::max_value()));
86 ~~~~~~~~~~~~~