A class representing a process. More...
#include <process.h>
Public Types | |
enum | { NO_EXIT_STATUS = -2 } |
enum | Status { DEAD, RUNNING, FINISHED, STOPPED, SIGNALED, CONTINUING } |
Process status. More... | |
Public Member Functions | |
Process (const std::string &command="", unsigned int min_debug_level=1) | |
Create a new process object. | |
int | start () throw (std::runtime_error, std::logic_error) |
Start up a new process. | |
pid_t | pid () const |
const std::string & | command () const |
int | exit_status () const |
int | signaled () const |
bool | killed () const |
bool | stopped () const |
void | signal (int signal, const std::string &error_message) const throw (std::runtime_error) |
Send a signal to this process. | |
void | stop () const throw (std::runtime_error) |
Send a SIGSTOP signal to this process. | |
void | resume () const throw (std::runtime_error) |
Send a SIGCONT signal to this process. | |
void | kill () const throw (std::runtime_error) |
Send a SIGKILL signal to this process. | |
Status | wait () const |
Wait for process to finish. | |
Status | status () const |
Return current process status. | |
virtual | ~Process () |
Destructor. | |
Protected Member Functions | |
Status | status (int s) const |
Convert a status provided by waitpid to a Status, possibly setting exit_status_ and signal_. | |
Status | wait (bool nohang) const |
Wrapper around waitpid . | |
Protected Attributes | |
pid_t | pid_ |
This is protected so that subclasses can set it. | |
Private Member Functions | |
Process (const Process &) | |
Process & | operator= (const Process &) |
Private Attributes | |
std::string | command_ |
int | exit_status_ |
int | signal_ |
A class representing a process.
It supports signaling and obtaining status. Note that the constructor does not start up the process, for that one should implement a subclass, with a constructor doing the fork()
etc. An example function that can be used to do this is available in Dv::Util::Process:start.
Example:
Dv::Util::Process proc("ping www.bla.com"); proc.start(); std::cout << proc.status() << std::endl; sleep(2); proc.stop(); // stop (not kill) the process by sending STOP signal sleep(2); proc.resume(); // resume the process sleep(2); if (impatient) proc.kill(); // send KILL signal else proc.wait(); // wait for process to actually finish
Definition at line 35 of file process.h.
Process status.
DEAD |
doesn't exist anymore |
RUNNING |
also after resuming |
FINISHED |
finished normally, exit_status available |
STOPPED |
received SIGSTOP signal
|
SIGNALED |
e.g. using SIGKILL, |
CONTINUING |
unused, could be supported under Linux 2.6 |
Dv::Util::Process::Process | ( | const std::string & | command = "" , |
|
unsigned int | min_debug_level = 1 | |||
) |
Create a new process object.
This function does not start up the process; this job is left to subclasses or, alternatively, to Dv::Util::Process::start. It simply stores the command to be executed. Example:
Dv::Util::Process proc("ping www.bla.com"); proc.start();
command | to be executed by the process. If empty, one can use Dv::Util::Process:start to have the process execute a function in the current program. | |
min_debug_level | for when the object is connected to a debug master |
virtual Dv::Util::Process::~Process | ( | ) | [virtual] |
Destructor.
If the process is still RUNNING or STOPPED, it will be killed.
Dv::Util::Process::Process | ( | const Process & | ) | [private] |
int Dv::Util::Process::start | ( | ) | throw (std::runtime_error, std::logic_error) |
Start up a new process.
std::runtime_error | if fork() failed. | |
std::runtime_error | if execl() failed. | |
std::logic_error | if the process is already running |
/bin/sh -c exec command
Dv::Util::Process proc("ping www.bla.com"); proc.start();
Dv::Util::Process p; if (! p.start()) return f(...); // in the child p.wait(); // in the parent
Reimplemented in Dv::Util::iopstream.
pid_t Dv::Util::Process::pid | ( | ) | const [inline] |
const std::string& Dv::Util::Process::command | ( | ) | const [inline] |
int Dv::Util::Process::exit_status | ( | ) | const [inline] |
Definition at line 105 of file process.h.
References exit_status_.
int Dv::Util::Process::signaled | ( | ) | const [inline] |
bool Dv::Util::Process::killed | ( | ) | const |
bool Dv::Util::Process::stopped | ( | ) | const |
void Dv::Util::Process::signal | ( | int | signal, | |
const std::string & | error_message | |||
) | const throw (std::runtime_error) |
Send a signal to this process.
signal | to send | |
error_message | to be used when throwing an exception |
std::runtime_error | if kill(_,_) returns -1, e.g. if the process does not exist anymore because it has already been killed or waited upon. |
void Dv::Util::Process::stop | ( | ) | const throw (std::runtime_error) |
Send a SIGSTOP signal to this process.
std::runtime_error | inherited from Dv::Util::Process::signal |
void Dv::Util::Process::resume | ( | ) | const throw (std::runtime_error) |
Send a SIGCONT signal to this process.
std::runtime_error | inherited from Dv::Util::Process::signal |
void Dv::Util::Process::kill | ( | ) | const throw (std::runtime_error) |
Send a SIGKILL signal to this process.
std::runtime_error | inherited from Dv::Util::Process::signal |
Status Dv::Util::Process::wait | ( | ) | const [inline] |
Status Dv::Util::Process::status | ( | ) | const [inline] |
Status Dv::Util::Process::status | ( | int | s | ) | const [protected] |
Convert a status provided by waitpid
to a Status, possibly setting exit_status_ and signal_.
s | status provided by waitpid |
Status Dv::Util::Process::wait | ( | bool | nohang | ) | const [protected] |
Wrapper around waitpid
.
nohang | if true, waitpid will not wait for the process to finish but just report the latest status change, if any. |
pid_t Dv::Util::Process::pid_ [protected] |
std::string Dv::Util::Process::command_ [private] |
int Dv::Util::Process::exit_status_ [private] |
Definition at line 186 of file process.h.
Referenced by exit_status().
int Dv::Util::Process::signal_ [private] |
Definition at line 187 of file process.h.
Referenced by signaled().
dvutil-1.0.10 | [ 5 December, 2009] |