Processes
PID | getCurrentProcessId() |
String | getCurrentExecutablePath() |
PID getCurrentProcessId()Returns the operating system's process ID for the current process. See also
getCurrentThreadId.String getCurrentExecutablePath()Returns the path to the executable file for the current process.
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 exePath, ArrayView<const StringView> args, StringView initialDir, const Output& output, const Input& input) |
static Owned<Subprocess> | execArgStr(StringView exePath, StringView argStr, StringView initialDir, 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 exePath, ArrayView<const StringView> args, StringView initialDir, const Output& output, const Input& input)Spawns a new process.
exePathis the path to the executable.argsis an array of command-line arguments.initialDiris the working directory for the new process.outputspecifies how to handle stdout/stderr.inputspecifies how to handle stdin.static Owned<Subprocess> execArgStr(StringView exePath, StringView argStr, StringView initialDir, 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 outputStream;
Owned<Subprocess> proc = Subprocess::exec(
"/bin/ls", {"-la"},
"/home/user",
Subprocess::Output::pipe(outputStream)
);
String output = outputStream.readRemaining();
s32 exitCode = proc->join();