ONPOSIX  2.0
 All Classes Namespaces Files Functions Variables Enumerator Friends Macros Pages
Process Class Reference

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.

Detailed Description

Class for launching a new process.

Class to launch a process through fork().

Example of usage to run a function:

void function ()
{
//...
}
int main ()
{
Process p(function);
}

Example of usage to run a program (i.e., through for()+execvp())

int main ()
{
std::vector<std::string> args;
args.push_back("-l");
args.push_back("*.cpp");
Process p("ls", args);
}

Definition at line 66 of file Process.hpp.

Constructor & Destructor Documentation

Process ( void(*)(void)  function)

Constructor to run a specific function.

This constructor creates a new process that will run the function given as argument.

Parameters
function,:pointer to the function that must be run

Definition at line 70 of file Process.cpp.

:
is_child_(false),
running_(false),
{
(*function)();
}
}

Here is the call graph for this function:

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().

Parameters
program,:name of the program to be run args: list of arguments

Definition at line 89 of file Process.cpp.

:
is_child_(false),
running_(false),
{
char* c_args [20];
c_args[0] = const_cast<char*> (program.c_str());
for (unsigned int i = 0; i < args.size(); ++i){
c_args[i+1] = const_cast<char*> (args[i].c_str());
}
c_args[args.size()+1] = (char*) NULL;
execvp(program.c_str(), c_args);
}
}

Here is the call graph for this function:

Member Function Documentation

bool checkNormalTermination ( )
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.

Returns
true in case of normal termination; false otherwise

Definition at line 135 of file Process.cpp.

{
if (is_child_)
return false;
if (WIFEXITED(status_))
return true;
else
return false;
}
bool checkSignalTermination ( )
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.

Returns
true in case the child has terminated due to a signal; false otherwise

Definition at line 153 of file Process.cpp.

{
if (is_child_)
return false;
if (WIFSIGNALED(status_))
return true;
else
return false;
}
void createProcess ( )
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.

Exceptions
std::runtime_errorin case the process cannot be created.

Definition at line 43 of file Process.cpp.

{
pid_ = fork();
if (pid_ == -1) {
running_ = false;
ERROR("Cannot start process");
throw std::runtime_error ("Async error");
}
if (pid_ == 0) {
// Child
pid_ = getpid();
is_child_ = true;
} else {
// Parent
is_child_ = false;
}
running_ = true;
}

Here is the caller graph for this function:

pid_t getPid ( ) const
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.

{
return pid_;
}
bool getSchedParam ( int *  policy,
int *  priority 
)

Get current scheduling policy and priority.

Parameters
policy,:policy (SCHED_FIFO, SCHED_RR or SCHED_OTHER)
priority,:scheduling priority; it has a meaning only for SCHED_FIFO and SCHED_RR
Returns
true in case of success; false in case of error

Definition at line 249 of file Process.cpp.

{
struct sched_param p;
int ret = sched_getparam(pid_, &p);
*priority = p.sched_priority;
*policy = sched_getscheduler(pid_);
if ((*policy < 0) || (ret < 0))
return false;
else
return true;
}
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

Parameters
sigthe signal to be sent
Returns
true on success; false if some error occurred

Definition at line 175 of file Process.cpp.

{
if (kill(pid_, sig) != 0){
ERROR("Can't send signal " << sig);
return false;
}
return true;
}
bool setSchedParam ( int  policy,
int  priority 
)

Set scheduling policy and priority.

Parameters
policy,:policy (SCHED_FIFO, SCHED_RR or SCHED_OTHER)
priority,:scheduling priority
Returns
true in case of success; false in case of error

Definition at line 229 of file Process.cpp.

{
struct sched_param p;
p.sched_priority = priority;
if (sched_setscheduler(pid_, policy, &p) == 0)
return true;
else
return false;
}
bool setSignalHandler ( int  sig,
void(*)(int)  handler 
)
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.

Parameters
sigthe signal to be sent
Returns
true on success; false if some error occurred

Definition at line 201 of file Process.cpp.

{
bool ret = true;
sigset_t oldset, set;
struct sigaction sa;
/* mask all signals until the handlers are installed */
sigfillset(&set);
sigprocmask(SIG_SETMASK, &set, &oldset);
bzero( &sa, sizeof(sa) );
sa.sa_handler = handler;
if (sigaction(sig, &sa, NULL) < 0) {
ERROR("Can't set signal " << sig);
ret = false;
}
/* remove the mask */
sigprocmask(SIG_SETMASK, &oldset,NULL);
return ret;
}
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.

Returns
false in case the function is called by the child or in case of abnormal termination; true in case of normal termination

Definition at line 116 of file Process.cpp.

{
if (is_child_)
return false;
waitpid(pid_, &status_, 1);
running_ = false;
if (WIFEXITED(status_))
return true;
else
return false;
}

Member Data Documentation

bool is_child_
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.

pid_t pid_
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.

bool running_
private

If the process is running.

Definition at line 90 of file Process.hpp.

int status_
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.


The documentation for this class was generated from the following files: