NIST Biometric Evaluation Framework
Software components for biometric technology evaluations
Classes | Public Member Functions | Static Public Member Functions | Static Public Attributes | List of all members
BiometricEvaluation::Process::ForkManager Class Reference

Manager implementation that starts Workers by calling fork(2). More...

#include <be_process_forkmanager.h>

Inherits BiometricEvaluation::Process::Manager.

Public Member Functions

 ForkManager ()
 ForkManager constructor. More...
 
std::shared_ptr< WorkerControlleraddWorker (std::shared_ptr< Worker > worker)
 Adds a Worker to be managed by this Manager. More...
 
void startWorkers (bool wait=true, bool communicate=false)
 Begin Worker's work. More...
 
void startWorker (std::shared_ptr< WorkerController > worker, bool wait=true, bool communicate=false)
 Start a worker. More...
 
void stopWorker (std::shared_ptr< WorkerController > workerController)
 Ask Worker to exit. More...
 
void broadcastSignal (int signo)
 Send a POSIX signal to all workers. More...
 
bool responsibleFor (const pid_t pid) const
 Obtain whether or not this ForkManager is responsbile for a particular PID. More...
 
void setNotWorking (const pid_t pid)
 Set Status.isWorking for PID to false. More...
 
void markAllFinished ()
 Call setNotWorking() for all PIDs known to this ForkManager. More...
 
bool getIsWorkingStatus (const pid_t pid) const
 Get Status.isWorking for PID. More...
 
void waitForWorkerExit ()
 Block until all Workers have exited. More...
 
 ~ForkManager ()
 ForkManager destructor. More...
 
void setExitCallback (void(*exitCallback)(std::shared_ptr< ForkWorkerController > worker, int stat_loc))
 Call a function in your program when a child exits. More...
 
void setExitStatus (const pid_t pid, const int32_t waitStatus)
 Set the exit status in the WorkerController for given process ID. More...
 
- Public Member Functions inherited from BiometricEvaluation::Process::Manager
 Manager ()
 Manager constructor. More...
 
virtual std::shared_ptr< WorkerControlleraddWorker (std::shared_ptr< Worker > worker)=0
 Adds a Worker to be managed by this Manager. More...
 
virtual uint32_t getNumCompletedWorkers () const
 Obtain the number of Workers that have exited. More...
 
virtual uint32_t getNumActiveWorkers () const
 Obtain the number of Workers that are still working. More...
 
virtual uint32_t getTotalWorkers () const
 Obtain the number of Workers this class is handling. More...
 
virtual void startWorkers (bool wait=true, bool communicate=false)=0
 Begin Worker's work. More...
 
virtual void startWorker (std::shared_ptr< WorkerController > worker, bool wait=true, bool communicate=false)=0
 Start a Worker. More...
 
virtual void waitForWorkerExit ()=0
 Block until all Workers have exited. More...
 
virtual void reset ()
 Reuse all Workers. More...
 
virtual void stopWorker (std::shared_ptr< WorkerController > worker)=0
 Ask Worker to return as soon as possible. More...
 
virtual bool waitForMessage (std::shared_ptr< WorkerController > &sender, int *nextFD=nullptr, int numSeconds=-1) const
 Wait for a message from a Worker. More...
 
virtual bool getNextMessage (std::shared_ptr< WorkerController > &sender, Memory::uint8Array &message, int numSeconds=-1) const
 Obtain a message from a Worker. More...
 
virtual void broadcastMessage (Memory::uint8Array &message) const
 Send one message to all Workers. More...
 
virtual ~Manager ()
 Manager destructor. More...
 

Static Public Member Functions

static void defaultExitCallback (std::shared_ptr< ForkWorkerController > worker, int status)
 A default exit callback function. More...
 

Static Public Attributes

static std::list< ForkManager * > FORKMANAGERS
 List of all instantiated ForkManagers. More...
 

Additional Inherited Members

virtual void _wait ()=0
 Do not return until all spawned processes exited. More...
 
- Protected Attributes inherited from BiometricEvaluation::Process::Manager
std::vector< std::shared_ptr< WorkerController > > _workers
 Workers that have been added. More...
 
std::vector< std::shared_ptr< WorkerController > > _pendingExit
 Workers that are about to exit (stop requested). More...
 

Detailed Description

Manager implementation that starts Workers by calling fork(2).

Definition at line 32 of file be_process_forkmanager.h.

Constructor & Destructor Documentation

◆ ForkManager()

BiometricEvaluation::Process::ForkManager::ForkManager ( )

ForkManager constructor.

◆ ~ForkManager()

BiometricEvaluation::Process::ForkManager::~ForkManager ( )

ForkManager destructor.

Member Function Documentation

◆ addWorker()

std::shared_ptr< WorkerController > BiometricEvaluation::Process::ForkManager::addWorker ( std::shared_ptr< Worker worker)
virtual

Adds a Worker to be managed by this Manager.

Parameters
workerA Worker instance to run.
Returns
shared_ptr to worker.

Implements BiometricEvaluation::Process::Manager.

◆ startWorkers()

void BiometricEvaluation::Process::ForkManager::startWorkers ( bool  wait = true,
bool  communicate = false 
)
virtual

Begin Worker's work.

Parameters
[in]waitWhether or not to wait for all Workers to return before returning.
[in]communicateWhether or not to enable communication among the Workers and Managers.
Exceptions
Error::ObjectExistsAt least one Worker is already working.
Error::StrategyErrorProblem forking.

Implements BiometricEvaluation::Process::Manager.

◆ startWorker()

void BiometricEvaluation::Process::ForkManager::startWorker ( std::shared_ptr< WorkerController worker,
bool  wait = true,
bool  communicate = false 
)
virtual

Start a worker.

Parameters
workerPointer to a WorkerController that is being managed by this Manager instance.
waitWhether or not to wait for this Worker to exit before returning control to the caller.
[in]communicateWhether or not to enable communication among the Workers and Managers.
Exceptions
Error::ObjectExistsworker is already working.
Error::StrategyErrorworker is not managed by this Manager instance.

Implements BiometricEvaluation::Process::Manager.

◆ stopWorker()

void BiometricEvaluation::Process::ForkManager::stopWorker ( std::shared_ptr< WorkerController workerController)
virtual

Ask Worker to exit.

Sends SIGUSR1 to the Worker, which ForkManager will handle automatically.

Parameters
workerControllerPointer to the ForkWorkerController that should be stopped.
Exceptions
Error::ObjectDoesNotExistworker is not working.
Error::StrategyErrorProblem sending the signal.
Attention
Do not call stopWorker() when communication is enabled unless you will be finished with communication for all Workers at that point. This creates a race condition for reads()/writes() when the Worker exits.

Implements BiometricEvaluation::Process::Manager.

◆ broadcastSignal()

void BiometricEvaluation::Process::ForkManager::broadcastSignal ( int  signo)

Send a POSIX signal to all workers.

Parameters
[in]signoThe signal to send.

◆ responsibleFor()

bool BiometricEvaluation::Process::ForkManager::responsibleFor ( const pid_t  pid) const

Obtain whether or not this ForkManager is responsbile for a particular PID.

Parameters
[in]pidPID in question
Returns
true if this ForkManager spawned pid, false otherwise.

◆ setNotWorking()

void BiometricEvaluation::Process::ForkManager::setNotWorking ( const pid_t  pid)

Set Status.isWorking for PID to false.

Parameters
[in]pidPID whose inWorking flag should be set to false
Exceptions
Error::ObjectDoesNotExistPID not under this manager's control.

◆ markAllFinished()

void BiometricEvaluation::Process::ForkManager::markAllFinished ( )

Call setNotWorking() for all PIDs known to this ForkManager.

◆ getIsWorkingStatus()

bool BiometricEvaluation::Process::ForkManager::getIsWorkingStatus ( const pid_t  pid) const

Get Status.isWorking for PID.

Parameters
[in]pidPID whose inWorking flag should be queried
Exceptions
Error::ObjectDoesNotExistPID not under this manager's control.

◆ waitForWorkerExit()

void BiometricEvaluation::Process::ForkManager::waitForWorkerExit ( )
virtual

Block until all Workers have exited.

Use this method if wait=false was set during a call to startWorker(s) but now wait=true is desired.

Implements BiometricEvaluation::Process::Manager.

◆ setExitCallback()

void BiometricEvaluation::Process::ForkManager::setExitCallback ( void(*)(std::shared_ptr< ForkWorkerController > worker, int stat_loc)  exitCallback)

Call a function in your program when a child exits.

Parameters
exitCallbackFunction pointer to a method that takes a shared_ptr to a ForkWorkerController and the integer status information.
Note
The exit callback will not have any effect if the Manager is not set to wait for Workers.

◆ defaultExitCallback()

static void BiometricEvaluation::Process::ForkManager::defaultExitCallback ( std::shared_ptr< ForkWorkerController worker,
int  status 
)
static

A default exit callback function.

Writes to stdout in the form: PID #: Exited .

Parameters
workerThe ForkWorkerController object that exited.
statusThe status of the Worker that exited (from wait(2)).

◆ setExitStatus()

void BiometricEvaluation::Process::ForkManager::setExitStatus ( const pid_t  pid,
const int32_t  waitStatus 
)

Set the exit status in the WorkerController for given process ID.

Parameters
[in]pidPID whose exit status should be set.
[in]statusStatus, as returned from wait(2).
Exceptions
Error::ObjectDoesNotExistPID not under this manager's control.
Note
Exit status is only set if process exited cleanly.

Member Data Documentation

◆ FORKMANAGERS

std::list<ForkManager*> BiometricEvaluation::Process::ForkManager::FORKMANAGERS
static

List of all instantiated ForkManagers.

This is not a list of managed pointers to ForkManagers. If it was, the smart pointer's destructor would attempt to delete the object being pointed to at program termination, which is ultimately sometime after the destructor of the ForkManager itself was called.

Definition at line 46 of file be_process_forkmanager.h.


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