IProcess

An interface to create new child processes, kill processes and inspect the existance of a process. Each static function has a class IInstanceLog * const log pointer, which is used for instance related
logs dependent of the log flag LOG_PROCESS.

File information

Filecommon/interface/process.h

Classes IProcess

Classes

IProcess

class IProcess {
public:
    static int Spawn(const char *filename, int argc, char ** argv, class IInstanceLog * const log);				
    static int Kill(int pid, int signalNr, class IInstanceLog * const log);											
    static bool ProcessExist(int pid, class IInstanceLog * const log);
    static int GetOwnPID(class IInstanceLog * const log);
    static int GetParentPID(class IInstanceLog * const log);
    static bool SetProcessGroupID(int pid, int pgid, class IInstanceLog * const log);
    static void GetOwnName(char * buffer, size_t len, class IInstanceLog * const log);
    static int System(class IInstanceLog * const log, const char * command, ...);
    static bool Exec(class IInstanceLog * const log, const char * command, char * stdOut, size_t stdOutLen);
};

Public static functions

int Spawn(const char *filename, int argc, char ** argv, class IInstanceLog * const log)
Spawns a new child process based on the filename and the set of arguments determined by char **argv.

Parameters

const char * filenameThe actual command which will be executed.
int argcThe number of arguments.
char ** argvAn array of char * arguments. Note that the first argument is the process filename itself again.
class IInstanceLog * const logThe IInstanceLog instance which is to be used.

Return value

On success it returns the process id of the spawned process, otherwise 0 is returned.

Remarks

STDOUT and STDERR are redirected to /dev/null with this method. If you want to have the output, you should use the ICommand interface instead.

You may implement the IChildExitedHandler interface to get notified if your spawned process exited.

Example

char* argv[3];
argv[0] = (char*)"/path/to/binary";
argv[1] = (char*)"-arg1";
argv[2] = (char*)"valueOfArg1";

int pid = IProcess::Spawn(argv[0], 3, argv, logInstance);
int Kill(int pid, int signalNr, class IInstanceLog * const log)
Sends the specified kill code to a process.

Parameters

int pidThe process PID.
int signalNrThe signal number, e.g. 9 (SIG_KILL).
class IInstanceLog * const logThe IInstanceLog instance which is to be used.

Return value

If the kill is successful 1 is returned; otherwise 0 is returned (int).
bool ProcessExist(int pid, class IInstanceLog * const log)
Checks if a process still exists or not.

Parameters

int pidThe process PID.
class IInstanceLog * const logThe IInstanceLog instance which is to be used.

Return value

true if the process still exists, otherwise false.
int GetOwnPID(class IInstanceLog * const log)
Returns the own process ID.

Parameters

class IInstanceLog * const logThe IInstanceLog instance which is to be used.

Return value

The process ID (int).
int GetParentPID(class IInstanceLog * const log)
Returns the parent process ID from the own process.

Parameters

class IInstanceLog * const logThe IInstanceLog instance which is to be used.

Return value

The parent process ID (int).
bool SetProcessGroupID(int pid, int pgid, class IInstanceLog * const log)
Sets the process group ID of the specified process.

Parameters

int pidThe process PID.
int pgidThe group ID.
class IInstanceLog * const logThe IInstanceLog instance which is to be used.

Return value

Returns true on success, otherwise false.
void GetOwnName(char * buffer, size_t len, class IInstanceLog * const log)
Writes the own process name to the given buffer.

Parameters

char * bufferThe buffer in which the name will be written.
size_t lenThe size of the buffer.
class IInstanceLog * const logThe IInstanceLog instance which is to be used.
int System(class IInstanceLog * const log, const char * command, ...)
Makes use of the standard system function and runs the specified command.

Parameters

class IInstanceLog * const logThe IInstanceLog instance which is to be used.
const char * commandThe command to run (e.g. "date -s \"1970-01-02 01:01:00\"").
...Parameters which are used inside command (e.g. %s etc.). The internally used buffer length is 1024 bytes!

Return value

The exit code of the system call (int).
bool Exec(class IInstanceLog * const log, const char * command, char * stdOut, size_t stdOutLen)
Makes use of the standard system function and runs the specified command.

Parameters

class IInstanceLog * const logThe IInstanceLog instance which is to be used.
const char * commandThe command to run (e.g. "date -s \"1970-01-02 01:01:00\"").
char * stdOutBuffer to save std out. The buffer will be NULL terminated.
size_t stdOutLenSize of the buffer.

Return value

Bool.