Processes
(ply-system.h)
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.
String getEnvironmentVariable(StringView name)Returns the value of the environment variable specified by
name. Returns an empty string if the variable is unset.
Subprocess
The Subprocess class represents a child process. You can spawn processes, redirect their I/O and wait for them to complete. Not supported on iOS.
Owned<Subprocess> process = Subprocess::execShellCommand("ls -la", "/home/user",
Subprocess::Output::openMerged());
String output = readAllRemainingData(process->getStdOutReader());
s32 exitCode = process->join();
static Owned<Subprocess> Subprocess::exec(StringView exePath, ArrayView<const StringView> args, StringView initialDir, const Subprocess::Output& output, const Subprocess::Input& input, const Subprocess::Options& options)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.Pass any of the following values to
output:Output::ignore()Discards the output. Output::inherit()Inherits the parent process's stdout/stderr. Output::openSeparate()Opens separate pipes to read from stdout/stderr. Output::openMerged()Opens a single pipe to read from both stdout and stderr. Output::openStdOutOnly()Opens a pipe to read from stdout and ignores stderr. Pass any of the following values to
input:Input::ignore()Sends no input. Input::inherit()Inherits the parent process's stdin. Input::open()Opens a pipe to write to stdin. Subprocess::Optionshas the following members:terminateProcessTreeIf trueand the subprocess spawns its own subprocesses, callingterminate()terminates the entire subprocess tree. Default isfalse.Note that on POSIX platforms, enabling
terminateProcessTreeplaces the subprocess tree in a separate process group. As such, terminal-generated signals received by the parent process (such as SIGINT generated by Ctrl-C) will no longer be automatically received by the subprocess tree.static Owned<Subprocess> Subprocess::execShellCommand(StringView shellCommand, StringView initialDir, const Subprocess::Output& output, const Subprocess::Input& input, const Subprocess::Options& options)Spawns a new process by passing
shellCommandto the platform's default command interpreter. Theoutput,inputandoptionsparameters behave the same as inexec().Pipe* Subprocess::getStdInWriter() constReturns a pipe that writes to the subprocess's stdin, or
nullptrif stdin wasn't opened.Pipe* Subprocess::getStdOutReader() constReturns a pipe that reads the subprocess's stdout, or
nullptrif stdout wasn't opened.Pipe* Subprocess::getStdErrReader() constReturns a pipe that reads the subprocess's stderr, or
nullptrif stderr wasn't opened.s32 Subprocess::join()Waits for the subprocess to exit and returns its exit code. Does not wait for any descendant processes spawned by the subprocess.
JoinResult Subprocess::joinWithTimeout(s32 timeoutMillis)Waits for the subprocess to exit with a timeout. A negative timeout value waits indefinitely. Does not wait for any descendant processes that may have been spawned by the subprocess.
JoinResulthas the following members:bool joinedtrueif the subprocess exited;falseif the timeout was reached.s32 exitCodeThe exit code, if the subprocess exited. bool Subprocess::terminate()Forcibly terminates the subprocess. If the subprocess hasn't been joined, and
Options::terminateProcessTreewas enabled when the process was created, then its subprocess tree is also terminated. Ifterminate()is called while another thread is waiting on a join function, the waiting thread is released.The terminated subprocess closes its standard I/O pipes when it exits. If
terminate()is called while another thread is waiting inside aread()from one of those pipes, the waiting thread is released.