使用Boost的process::child创建自定义环境子进程示例请求
child with Custom Environment (Full Example) I totally get what you mean—Boost.Process's system() function is handy but lacks flexibility for things like capturing output or fine-grained process control. Here's a complete, working example using process::child that sets a custom environment, captures the child's stdout, waits for it to finish, and checks the exit code.
First, let's look at the full code, then break down the key parts:
#include <boost/process.hpp> #include <boost/process/environment.hpp> #include <iostream> #include <string> #include <vector> namespace bp = boost::process; int main() { // Step 1: Create a custom environment bp::environment env = bp::this_process::environment(); // Start with current env env["CUSTOM_VAR"] = "HelloFromParent"; // Add/override variables env["EXISTING_VAR"] = "ModifiedValue"; // Example: change an existing variable // Step 2: Set up pipes to capture child's stdout bp::ipstream child_stdout; // Input stream to read from child's stdout // Step 3: Launch the child process with custom env and pipe bp::child proc( "/path/to/your/executable", // Replace with your actual executable path bp::std_out > child_stdout, // Redirect stdout to our pipe env // Pass the custom environment ); // Step 4: Read output from the child std::string line; std::cout << "Child process output:\n"; while (std::getline(child_stdout, line)) { std::cout << line << "\n"; } // Step 5: Wait for the process to complete and get exit code proc.wait(); int exit_code = proc.exit_code(); std::cout << "\nChild process exited with code: " << exit_code << "\n"; return 0; }
Key Breakdown:
- Custom Environment Setup: We start by copying the parent's current environment with
bp::this_process::environment(), then modify or add variables as needed. This is better than building an environment from scratch unless you want a completely clean slate. - Pipe for Output Capture: Using
bp::ipstreamandbp::std_out > child_stdoutlets us capture the child's standard output directly into our program. You can do similar for stderr withbp::std_err. - Launching the Child: The
bp::childconstructor takes the executable path, redirection options, and the environment. For command-line arguments, you can pass them as additional parameters (e.g.,"/bin/echo", "Hello", "World"). - Waiting and Exit Code:
proc.wait()blocks until the child finishes, andproc.exit_code()gives you the exit status to handle success/failure.
Platform Notes:
- On Windows, executable paths might use backslashes, but Boost.Process handles both forward and backslashes. Alternatively, use
bp::search_path("executable_name")to find the executable in PATH. - If you want a completely empty environment, initialize
envasbp::environment()instead of copying the parent's.
Example with Arguments:
If you need to pass command-line arguments to the child, just add them to the child constructor:
bp::child proc( "/bin/echo", "My custom var is:", bp::environment::get("CUSTOM_VAR"), // Pass env var as arg bp::std_out > child_stdout, env );
This should cover most common use cases where system() falls short—capturing output, waiting for the process, and using a custom environment. Let me know if you need help adapting this to your specific scenario!
内容的提问来源于stack exchange,提问作者zack evein

