MIRA
|
Encapsulates a process, that was launched from the current process. More...
#include <platform/Process.h>
Classes | |
class | Environment |
Holds the environment variables that can be passed to a process. More... | |
Public Types | |
enum | ExitStatus { NORMALEXIT, CRASHED, KILLED } |
enum | CreationFlags { noflags = 0x00, interruptChildOnParentDeath = 0x01, shutdownRecursively = 0x02 } |
enum | RedirectionFlags { none = 0x00, in = 0x01, out = 0x02, err = 0x04 } |
typedef boost::iostreams::stream< boost::iostreams::file_descriptor_sink > | ostream |
typedef boost::iostreams::stream< boost::iostreams::file_descriptor_source > | istream |
Public Member Functions | |
Process () | |
Creates an invalid process object. More... | |
~Process () | |
Destructor, which will terminate the process, if it is still running. More... | |
Process (Process &&other) noexcept | |
Process & | operator= (Process &&other) noexcept |
void | swap (Process &other) |
Swaps the ownership of the process context within this and other. More... | |
bool | wait (Duration maxWait=Duration::infinity()) |
Blocks the current thread of the calling process, until the process that is represented by this Process object has terminated or maxWait is reached. More... | |
int | getExitCode () |
Return the exit code of the process. More... | |
ExitStatus | getExitStatus () |
Returns the exit status of the process only valid if not running. More... | |
bool | isRunning () |
Returns if the process that is represented by this Process object is still running. More... | |
void | interrupt () |
Stops (e.g. More... | |
void | terminate () |
Terminates (e.g. More... | |
void | kill () |
Kills (e.g. More... | |
void | setRecursiveShutdown (bool recursive) |
Enable recursive shutdown. More... | |
void | shutdown (Duration timeout=Duration::seconds(5)) |
Performs an orderly shutdown by executing the following steps. More... | |
uint32 | getPID () const |
Returns the process id (pid) of the process. More... | |
ostream & | cin () |
Output stream for writing to the stdin of the child process. More... | |
istream & | cout () |
Input stream for reading from the stdout of the child process. More... | |
istream & | cerr () |
Input stream for reading from the stderr of the child process. More... | |
Static Public Member Functions | |
static Process | createProcess (const std::string &commandLine, CreationFlags flags, RedirectionFlags streamRedirection=none) |
Spawns a new process and executes the given command line. More... | |
static Process | createProcess (const std::string &commandLine, RedirectionFlags streamRedirection=none) |
Same as above but the flags can be omitted. More... | |
static Process | createProcess (const std::string &applicationName, const std::vector< std::string > &args, CreationFlags flags=noflags, RedirectionFlags streamRedirection=none, boost::optional< Environment > env=boost::optional< Environment >()) |
Same as above, but a more advanced interface to create a new process. More... | |
Protected Member Functions | |
bool | wait (int pid, std::vector< int > &children, Duration timeout=Duration::seconds(5)) |
std::vector< int > | getRecursiveChildProcesses (int pid, int maxDepth=-1) |
bool | interrupt (int pid) |
bool | terminate (int pid) |
bool | kill (int pid) |
void | shutdown (int pid, Duration timeout=Duration::seconds(5)) |
Encapsulates a process, that was launched from the current process.
A new process can be spawned using the createProcess() method, which returns an object to the created process. The process object can be used to terminate the process and to check its current status.
Note, that Process objects can not be copied. Use the move semantic and std::move() to pass instances of this class.
typedef boost::iostreams::stream<boost::iostreams::file_descriptor_sink> ostream |
typedef boost::iostreams::stream<boost::iostreams::file_descriptor_source> istream |
enum ExitStatus |
enum CreationFlags |
enum RedirectionFlags |
Process | ( | ) |
Creates an invalid process object.
~Process | ( | ) |
Destructor, which will terminate the process, if it is still running.
void swap | ( | Process & | other | ) |
Swaps the ownership of the process context within this and other.
|
static |
Spawns a new process and executes the given command line.
The command line may contain parameters that are separated by white characters.
On Windows the application is searched in the following paths:
On Linux the application is searched within:
flags | Allows to specify some flags that control the creation and termination of the created process. |
streamRedirection | Allows to specify which streams (stdin, stdout, etc) should be redirected in order to access them from the parent process via the cin(), cout(), cerr() stream method (see below). |
XRuntime | If the process can not be forked |
XFileNotFound | If the given executable can not be found |
XInvalidParameter | If the execution of the given application fails |
|
static |
Same as above but the flags can be omitted.
|
static |
Same as above, but a more advanced interface to create a new process.
Here, the arguments can be specified separately as vector of strings. Moreover, the environment of the new process (containing the environment variables) can be specified.
bool wait | ( | Duration | maxWait = Duration::infinity() | ) |
Blocks the current thread of the calling process, until the process that is represented by this Process object has terminated or maxWait is reached.
int getExitCode | ( | ) |
Return the exit code of the process.
Only valid if not running and exit status is NORMALEXIT.
ExitStatus getExitStatus | ( | ) |
Returns the exit status of the process only valid if not running.
bool isRunning | ( | ) |
Returns if the process that is represented by this Process object is still running.
void interrupt | ( | ) |
Stops (e.g.
using SIGINT) the process that is represented by this Process object.
void terminate | ( | ) |
Terminates (e.g.
using SIGTERM) the process that is represented by this Process object.
void kill | ( | ) |
Kills (e.g.
using SIGKILL) the process that is represented by this Process object.
void setRecursiveShutdown | ( | bool | recursive | ) |
Enable recursive shutdown.
Can be used to change this setting after createProcess().
void shutdown | ( | Duration | timeout = Duration::seconds(5) | ) |
Performs an orderly shutdown by executing the following steps.
This method blocks until the process that is represented by this Process object has terminated.
timeout | Timeout used for waiting for process to terminate in the above steps |
uint32 getPID | ( | ) | const |
Returns the process id (pid) of the process.
Or 0, if no process was started.
ostream& cin | ( | ) |
Output stream for writing to the stdin of the child process.
This stream is available only, if the appropriate redirection flag was set in createProcess().
istream& cout | ( | ) |
Input stream for reading from the stdout of the child process.
This stream is available only, if the appropriate redirection flag was set in createProcess().
istream& cerr | ( | ) |
Input stream for reading from the stderr of the child process.
This stream is available only, if the appropriate redirection flag was set in createProcess().
|
protected |
|
protected |
|
protected |
|
protected |
|
protected |
|
protected |