Classes | Static Public Member Functions | List of all members
Fork Class Reference

Convenience wrapper for forking processes. More...

#include <Fork.hpp>

Classes

struct  TerminationReport
 A termination report, returned by wait methods. More...
 

Static Public Member Functions

static TerminationReport checkForTermination (int pid)
 Check the process for termination without blocking. More...
 
static std::vector< TerminationReportcheckForTermination (const std::vector< int > &pids)
 Check without blocking the supplied processes for termination. More...
 
static bool forkSupported ()
 Determine whether forking is support on this platform. More...
 
static int performFork (const std::function< int()> &function, bool exitChild)
 Perform a fork operator and run the supplied function for the child. More...
 
static int performFork (const std::function< int()> &function)
 Perform a fork operator and run the supplied function for the child. More...
 
static TerminationReport terminateProcess (int pid)
 Terminate the child process if it is running. More...
 
static TerminationReport waitOnProcess (int pid)
 Wait on a process until the process terminates. More...
 

Detailed Description

Convenience wrapper for forking processes.

Member Function Documentation

◆ checkForTermination() [1/2]

static TerminationReport checkForTermination ( int  pid)
inlinestatic

Check the process for termination without blocking.

If the pid is invalid, an empty termination report is returned.

If the process with the specified id has not terminated, an empty termination report is returned.

Parameters
pidthe process id
Returns
a termination report
Exceptions
WaitExceptionif the waitid call failed

◆ checkForTermination() [2/2]

static std::vector<TerminationReport> checkForTermination ( const std::vector< int > &  pids)
inlinestatic

Check without blocking the supplied processes for termination.

Parameters
pidsthe process ids
Returns
a vector of termination reports for the pids that terminated.
Exceptions
WaitExceptionif the waitid call failed

◆ forkSupported()

static bool forkSupported ( )
inlinestatic

Determine whether forking is support on this platform.

◆ performFork() [1/2]

static int performFork ( const std::function< int()> &  function,
bool  exitChild 
)
inlinestatic

Perform a fork operator and run the supplied function for the child.

The child process will exit when the function returns if exitChild is set to true. Otherwise, the child will return and the caller will need to handle child termination.

Parameters
functionthe function to run in the child process
exitChildif set to true, _Exit(int) is called with the the exit state of the function
Returns
the child pid for the parent process, the exit state of the function for the child process if it returns
Exceptions
ForkExceptionif the fork call failed

◆ performFork() [2/2]

static int performFork ( const std::function< int()> &  function)
inlinestatic

Perform a fork operator and run the supplied function for the child.

The child will return when the function has completed and the caller will need to handle child termination.

This version does not require a function that returns an integer.

Parameters
functionthe function to run in the child process
Returns
the child pid for the parent process and zero for the child process
Exceptions
ForkExceptionif the fork call failed

◆ terminateProcess()

static TerminationReport terminateProcess ( int  pid)
inlinestatic

Terminate the child process if it is running.

If the process has not terminated, terminate it and return an empty termination report. If the pid is invalid, an empty termination report is returned.

Parameters
pidthe process id
Returns
a termination report
Exceptions
WaitExceptionif the waitid call failed

◆ waitOnProcess()

static TerminationReport waitOnProcess ( int  pid)
inlinestatic

Wait on a process until the process terminates.

If the pid is invalid, an empty termination report is returned.

Parameters
pidthe process id
Returns
a termination report
Exceptions
WaitExceptionif the waitid call failed

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