libassa 3.5.1
|
Fork class is a simple wrapper around C library function fork(). More...
#include <Fork.h>
Public Types | |
enum | state_t { KILL_ON_EXIT , WAIT_ON_EXIT , LEAVE_ALONE } |
Child completion states. More... | |
enum | wait4status_t { IGNORE_STATUS , COLLECT_STATUS } |
Public Member Functions | |
Fork (state_t exit_action_=WAIT_ON_EXIT, wait4status_t catch_status_=COLLECT_STATUS) | |
Fork the current process in two immediately. | |
~Fork () | |
Destructor. | |
bool | isParent () const |
Test whether we are in parent section of the code. | |
bool | isChild () const |
Test whether we are in child section of the code. | |
pid_t | getChildPID () const |
Retrieve child process id. | |
int | get_exit_status () const |
Retrieve exit status of a child process if the constructor's parameter catch_status_ was set to TRUE. | |
Static Public Member Functions | |
static int | fork_exec (const string &cmd_, const string &args_, wait4status_t wait_for_completion_, bool ignore_output_=false) |
Execute an external command. | |
Private Attributes | |
pid_t | m_pid |
Child pid. | |
SigHandler | m_local_sh |
Local signal handler. | |
ChildStatusHandler | m_chstath |
Handler to catch Child's status. | |
SigAction | m_old_disp |
Old signal disposition. | |
Fork class is a simple wrapper around C library function fork().
Main advantage of using Fork over fork() is that child termination process is handles internally by Fork class static destructor.
Child completion states.
Enumerator | |
---|---|
KILL_ON_EXIT | Kill all childer on exit. |
WAIT_ON_EXIT | Wait for all children to exit. |
LEAVE_ALONE | Ignore all running children on exit. |
Enumerator | |
---|---|
IGNORE_STATUS | Don't wait for child to complete. |
COLLECT_STATUS | Wait for child to complete and collect its exit status. |
Definition at line 99 of file Fork.h.
Fork::Fork | ( | Fork::state_t | exit_action_ = WAIT_ON_EXIT , |
Fork::wait4status_t | catch_status_ = COLLECT_STATUS |
||
) |
Fork the current process in two immediately.
exit_action_ | Specify (default=WAIT_ON_EXIT) whether to wait for the child to finish or kill it with SIGTERM on process exit. |
catch_status_ | If true (default=COLLECT_STATUS), pause for the child to exit and collect its exit status. |
Definition at line 159 of file Fork.cpp.
References ASSA::ASSAERR, ASSA::ChildStatusHandler::caught(), COLLECT_STATUS, EL, ASSA::FORK, ASSA::Singleton< ForkList >::get_instance(), ASSA::SigHandler::install(), LEAVE_ALONE, m_chstath, m_local_sh, m_old_disp, m_pid, ASSA::SigHandler::remove(), and trace_with_mask.
|
inline |
Destructor.
Doesn't really do anything. All children will be terminated according to state set when process terminates.
Definition at line 124 of file Fork.h.
References ASSA::FORK, and trace_with_mask.
|
static |
Execute an external command.
Conveniently wraps fork()/execvp()/wait() sequence of calls.
cmd_ | Command to execute. |
args_ | Command arguments as one string. |
wait_for_completion_ | If set to true, blocks until child exits; false otherwise. |
ignore_output_ | Discard child's output to stdout/stderr. |
Close all file descriptors and reduped stdout/stderr to /dev/null
Definition at line 62 of file Fork.cpp.
References ASSA::ASSAERR, DL, EL, ASSA::FORK, LEAVE_ALONE, ASSA::CmdLineOpts::str_to_argv(), and trace_with_mask.
|
inline |
Retrieve exit status of a child process if the constructor's parameter catch_status_ was set to TRUE.
Definition at line 151 of file Fork.h.
References ASSA::ChildStatusHandler::exit_status(), and m_chstath.
|
inline |
Retrieve child process id.
Definition at line 142 of file Fork.h.
References ASSA::FORK, m_pid, and trace_with_mask.
|
inline |
|
inline |
|
private |
Handler to catch Child's status.
Definition at line 184 of file Fork.h.
Referenced by Fork(), and get_exit_status().
|
private |
|
private |
|
private |
Child pid.
Definition at line 178 of file Fork.h.
Referenced by Fork(), getChildPID(), isChild(), and isParent().