Belle II Software  release-08-01-10
ProcHandler Class Reference

A class to manage processes for parallel processing. More...

#include <ProcHandler.h>

Collaboration diagram for ProcHandler:

Public Member Functions

 ProcHandler (unsigned int nWorkerProc, bool markChildrenAsLocal=false)
 Constructor. More...
 
 ~ProcHandler ()
 Destructor.
 
void startInputProcess ()
 Fork and initialize an input process.
 
void startWorkerProcesses ()
 Fork and initialize worker processes.
 
void startOutputProcess ()
 There is no real output process, but marks current process as output.
 
bool waitForAllProcesses ()
 Wait until all forked processes handled by this ProcHandler terminate. More...
 
std::set< int > processList () const
 Return list of PIDs managed by this ProcHandler instance.
 

Static Public Member Functions

static bool parallelProcessingUsed ()
 Returns true if multiple processes have been spawned, false in single-core mode.
 
static bool isInputProcess ()
 Return true if the process is an input process.
 
static bool isWorkerProcess ()
 Return true if the process is a worker process.
 
static bool isOutputProcess ()
 Return true if the process is an output process.
 
static int numEventProcesses ()
 Return number of worker processes (configured value, not current)
 
static std::set< int > globalProcessList ()
 Return list of all PIDs (from all ProcHandler instances).
 
static int EvtProcID ()
 Return ID of the current process. More...
 
static void setProcessID (int processID)
 Set the process ID of this process.
 
static std::string getProcessName ()
 Get a name for this process. More...
 

Private Member Functions

bool startProc (std::set< int > *processList, const std::string &procType, int id)
 Start a new process, adding its PID to processList, and setting s_processID = id. More...
 

Private Attributes

bool m_markChildrenAsLocal
 Anormal termination of child will not stop parent, waitForAllProcesses() returns status.
 
std::set< int > m_processList
 PIDs of processes controlled by this ProcHandler.
 
unsigned int m_numWorkerProcesses
 Number of worker processes controlled by this ProcHandler.
 

Detailed Description

A class to manage processes for parallel processing.

Definition at line 21 of file ProcHandler.h.

Constructor & Destructor Documentation

◆ ProcHandler()

ProcHandler ( unsigned int  nWorkerProc,
bool  markChildrenAsLocal = false 
)
explicit

Constructor.

Children marked as local will not cause the parent process to die in case of anormal termination. Errors can be detected using the return value of waitForAllProcesses(). Note that you probably shouldn't have multiple ProcHandlers with local children running at the same time.

Definition at line 188 of file ProcHandler.cc.

188  :
189  m_markChildrenAsLocal(markChildrenAsLocal),
190  m_numWorkerProcesses(nWorkerProc)
191 {
192  if ((int)nWorkerProc > s_numEventProcesses)
193  s_numEventProcesses = nWorkerProc;
194 
195  if (!pidListEmpty())
196  B2FATAL("Constructing ProcHandler after forking is not allowed!");
197 
198  //s_pidVector size shouldn't be changed once processes are forked (race condition)
199  s_pidVector.reserve(s_pidVector.size() + nWorkerProc + 2);
200  s_pids = s_pidVector.data();
201  setsid();
202 
203 }
unsigned int m_numWorkerProcesses
Number of worker processes controlled by this ProcHandler.
Definition: ProcHandler.h:90
bool m_markChildrenAsLocal
Anormal termination of child will not stop parent, waitForAllProcesses() returns status.
Definition: ProcHandler.h:88

Member Function Documentation

◆ EvtProcID()

int EvtProcID ( )
static

Return ID of the current process.

Return values mean: -1: no parallel processing used <10000 one of the worker processes (between input and output paths). In range 0..Environment::getInstance().getNumberProcesses()-1. >=10000 input path >=20000 output path

Definition at line 248 of file ProcHandler.cc.

◆ getProcessName()

std::string getProcessName ( )
static

Get a name for this process.

(input, event, output...).

Definition at line 252 of file ProcHandler.cc.

◆ startProc()

bool startProc ( std::set< int > *  processList,
const std::string &  procType,
int  id 
)
private

Start a new process, adding its PID to processList, and setting s_processID = id.

Returns true in child process.

Definition at line 154 of file ProcHandler.cc.

◆ waitForAllProcesses()

bool waitForAllProcesses ( )

Wait until all forked processes handled by this ProcHandler terminate.

In case the markChildrenAsLocal option was set when a process was started and this process terminated anormaly (via signal or non-zero exit code), the return value is set to false. Otherwise, true is returned.

Definition at line 266 of file ProcHandler.cc.


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