<ply-base.h>namespace plyProcesses
<ply-base.h>namespace plyPlywood provides facilities for spawning and managing child processes, including redirecting their input and output streams.
Subprocess
The Subprocess class represents a child process. You can spawn processes, redirect their I/O, and wait for them to complete.
s32 | join() |
static Owned<Subprocess> | exec(StringView exe_path, ArrayView<const StringView> args, StringView initial_dir, const Output& output, const Input& input) |
static Owned<Subprocess> | exec_arg_str(StringView exe_path, StringView arg_str, StringView initial_dir, const Output& output, const Input& input) |
s32 join()Waits for the subprocess to finish and returns its exit code. Must be called before the
Subprocessobject is destroyed.static Owned<Subprocess> exec(StringView exe_path, ArrayView<const StringView> args, StringView initial_dir, const Output& output, const Input& input)Spawns a new process.
exe_pathis the path to the executable.argsis an array of command-line arguments.initial_diris the working directory for the new process.outputspecifies how to handle stdout/stderr.inputspecifies how to handle stdin.static Owned<Subprocess> exec_arg_str(StringView exe_path, StringView arg_str, StringView initial_dir, const Output& output, const Input& input)Like
exec, but takes the arguments as a single string that will be parsed into individual arguments.
The Output and Input parameters control I/O redirection:
Output::ignore()— Discards the outputOutput::inherit()— Uses the parent process's stdout/stderrOutput::pipe(Stream&)— Redirects to a pipe you can read fromInput::open()— Uses the parent process's stdinInput::pipe(Stream&)— Redirects from a pipe you can write to
// Run a command and capture its output
Stream output_stream;
Owned<Subprocess> proc = Subprocess::exec(
"/bin/ls", {"-la"},
"/home/user",
Subprocess::Output::pipe(output_stream)
);
String output = output_stream.read_remaining();
s32 exit_code = proc->join();