ONPOSIX
2.0
|
Class for launching a new process. More...
#include <Process.hpp>
Public Member Functions | |
pid_t | getPid () const |
Get PID of the process related to this instance. | |
Process (void(*function)(void)) | |
Constructor to run a specific function. | |
Process (const std::string &program, const std::vector< std::string > &args) | |
Constructor to run a specific program. | |
bool | waitForTermination () |
Function to wait the termination of the process. | |
bool | checkNormalTermination () |
Function to check if the child has terminated correctly. | |
bool | checkSignalTermination () |
Function to check if the child has terminated for a signal. | |
bool | sendSignal (int sig) |
Function to send a signal to the process. | |
bool | setSchedParam (int policy, int priority) |
Set scheduling policy and priority. | |
bool | getSchedParam (int *policy, int *priority) |
Get current scheduling policy and priority. |
Static Public Member Functions | |
static bool | setSignalHandler (int sig, void(*handler)(int)) |
Set a handler for a specific signal. |
Private Member Functions | |
void | createProcess () |
Create a process. |
Private Attributes | |
pid_t | pid_ |
Pid of the new process. | |
bool | is_child_ |
If the class instance is related to the current process. | |
bool | running_ |
If the process is running. | |
int | status_ |
Exit status of the child process when terminated. |
Class for launching a new process.
Class to launch a process through fork().
Example of usage to run a function:
Example of usage to run a program (i.e., through for()+execvp())
Definition at line 66 of file Process.hpp.
Process | ( | void(*)(void) | function | ) |
Constructor to run a specific function.
This constructor creates a new process that will run the function given as argument.
function,: | pointer to the function that must be run |
Definition at line 70 of file Process.cpp.
Process | ( | const std::string & | program, |
const std::vector< std::string > & | args | ||
) |
Constructor to run a specific program.
This constructor creates a new process that will run the program given as argument. This contructor invokes fork()+execvp().
program,: | name of the program to be run args: list of arguments |
Definition at line 89 of file Process.cpp.
|
inline |
Function to check if the child has terminated correctly.
This function must be invoked after waitForTermination() and allows to inspect the termination status of the child.
Definition at line 135 of file Process.cpp.
|
inline |
Function to check if the child has terminated for a signal.
This function must be invoked after waitForTermination() and allows to inspect the termination status of the child.
Definition at line 153 of file Process.cpp.
|
private |
Create a process.
This function creates a new process through fork(). This function is not meant to be called explicitly, but it is automatically called by the constructors.
std::runtime_error | in case the process cannot be created. |
Definition at line 43 of file Process.cpp.
|
inline |
Get PID of the process related to this instance.
This value is the pid of the new process for both the parent process and the child process (who gets its own pid)
Definition at line 111 of file Process.hpp.
bool getSchedParam | ( | int * | policy, |
int * | priority | ||
) |
Get current scheduling policy and priority.
policy,: | policy (SCHED_FIFO, SCHED_RR or SCHED_OTHER) |
priority,: | scheduling priority; it has a meaning only for SCHED_FIFO and SCHED_RR |
Definition at line 249 of file Process.cpp.
bool sendSignal | ( | int | sig | ) |
Function to send a signal to the process.
This method allows to send a signal to the process related to this instance of Process. This function wraps the classical kill() function. The list of signals is available on /usr/include/bits/signum.h
sig | the signal to be sent |
Definition at line 175 of file Process.cpp.
bool setSchedParam | ( | int | policy, |
int | priority | ||
) |
Set scheduling policy and priority.
policy,: | policy (SCHED_FIFO, SCHED_RR or SCHED_OTHER) |
priority,: | scheduling priority |
Definition at line 229 of file Process.cpp.
|
static |
Set a handler for a specific signal.
This method allows to manually set a handler for handling a specific signal. The list of signals is available on /usr/include/bits/signum.h Use signals less as possible, mainly for standard situations. During the execution of the handler other signals may arrive. This can lead to inconsistent states. The handler must be short. It must just update the internal state and/or kill the application. Not all library functions can be called inside the handler without having strange behaviors (see man signal). In particular, it's not safe calling functions of the standard library, like printf() or exit(), or other functions defined inside the application itself. The access to global variables is not safe either, unless they have been defined as volatile.
sig | the signal to be sent |
Definition at line 201 of file Process.cpp.
bool waitForTermination | ( | ) |
Function to wait the termination of the process.
To avoid deadlocks, this function can be called only by the parent and not by the child itself.
Definition at line 116 of file Process.cpp.
|
private |
If the class instance is related to the current process.
In a parent-child relationship, this variable is useful to distinguish between the parent and the child. This variable is euql to false for the parent (i.e., the process who created the new process) and equal to true for the child process (i.e., the one who has been created).
Definition at line 85 of file Process.hpp.
|
private |
Pid of the new process.
This value contains the pid of the new process for both the parent process and the child process (who sees its own pid)
Definition at line 74 of file Process.hpp.
|
private |
If the process is running.
Definition at line 90 of file Process.hpp.
|
private |
Exit status of the child process when terminated.
The child process can be terminated by the parent through sendSignal(KILL). The parent can also waith the normal termination of the child through waitForTermination().
Definition at line 99 of file Process.hpp.