ROOT  6.06/09
Reference Guide
Public Member Functions | Private Attributes | List of all members
TMPClient Class Reference

Base class for multiprocess applications' clients.

It provides a simple interface to fork a ROOT session into server/worker sessions and exchange messages with them. Multiprocessing applications can build on TMPClient and TMPWorker: the class providing multiprocess functionalities to users should inherit (possibly privately) from TMPClient, and the workers executing tasks should inherit from TMPWorker.

Definition at line 23 of file TMPClient.h.

Public Member Functions

 TMPClient (unsigned nWorkers=0)
 Class constructor. More...
 
 ~TMPClient ()
 Class destructor. More...
 
 TMPClient (const TMPClient &)=delete
 
TMPClientoperator= (const TMPClient &)=delete
 
bool Fork (TMPWorker &server)
 This method forks the ROOT session into fNWorkers children processes. More...
 
unsigned Broadcast (unsigned code, unsigned nMessages=0)
 Send a message with the specified code to at most nMessages workers. More...
 
template<class T >
unsigned Broadcast (unsigned code, const std::vector< T > &objs)
 Send a message with a different object to each server. More...
 
template<class T >
unsigned Broadcast (unsigned code, std::initializer_list< T > &objs)
 Send a message with a different object to each server. More...
 
template<class T >
unsigned Broadcast (unsigned code, T obj, unsigned nMessages=0)
 Send a message containing code and obj to each worker, up to a maximum number of nMessages workers. More...
 
TMonitorGetMonitor ()
 
bool GetIsParent () const
 
void SetNWorkers (unsigned n)
 Set the number of workers that will be spawned by the next call to Fork() More...
 
unsigned GetNWorkers () const
 
void DeActivate (TSocket *s)
 DeActivate a certain socket. More...
 
void Remove (TSocket *s)
 Remove a certain socket from the monitor. More...
 
void ReapWorkers ()
 Wait on worker processes and remove their pids from fWorkerPids. More...
 
void HandleMPCode (MPCodeBufPair &msg, TSocket *sender)
 Handle messages containing an EMPCode. More...
 

Private Attributes

bool fIsParent
 This is true if this is the parent/client process, false if this is a child/worker process. More...
 
std::vector< pid_t > fWorkerPids
 A vector containing the PIDs of children processes/workers. More...
 
TMonitor fMon
 This object manages the sockets and detect socket events via TMonitor::Select. More...
 
unsigned fNWorkers
 The number of workers that should be spawned upon forking. More...
 

#include <TMPClient.h>

+ Inheritance diagram for TMPClient:
+ Collaboration diagram for TMPClient:

Constructor & Destructor Documentation

TMPClient::TMPClient ( unsigned  nWorkers = 0)
explicit

Class constructor.

Parameters
nWorkersthe number of children processes that will be created by Fork, i.e. the number of workers that will be available after this call. The default value (0) means that a number of workers equal to the number of cores of the machine is going to be spawned. If that information is not available, 2 workers are created instead.

Definition at line 40 of file TMPClient.cxx.

TMPClient::~TMPClient ( )

Class destructor.

This method is in charge of shutting down any remaining worker, closing off connections and reap the terminated children processes.

Definition at line 59 of file TMPClient.cxx.

TMPClient::TMPClient ( const TMPClient )
delete

Member Function Documentation

unsigned TMPClient::Broadcast ( unsigned  code,
unsigned  nMessages = 0 
)

Send a message with the specified code to at most nMessages workers.

Sockets can either be in an "active" or "non-active" state. This method activates all the sockets through which the client is connected to the workers, and deactivates them when a message is sent to the corresponding worker. This way the sockets pertaining to workers who have been left idle will be the only ones in the active list (TSocket::GetMonitor()->GetListOfActives()) after execution.

Parameters
codethe code to send (e.g. EMPCode)
nMessagesthe maximum number of messages to send. If nMessages == 0 || nMessage > fNWorkers, send a message to every worker.
Returns
the number of messages successfully sent

Definition at line 192 of file TMPClient.cxx.

Referenced by Broadcast(), and ~TMPClient().

template<class T >
unsigned TMPClient::Broadcast ( unsigned  code,
const std::vector< T > &  args 
)

Send a message with a different object to each server.

Sockets can either be in an "active" or "non-active" state. This method activates all the sockets through which the client is connected to the workers, and deactivates them when a message is sent to the corresponding worker. This way the sockets pertaining to workers who have been left idle will be the only ones in the active list (TSocket::GetMonitor()->GetListOfActives()) after execution.

Parameters
codethe code of the message to send (e.g. EMPCode)
argsa vector containing the different messages to be sent. If the size of the vector is smaller than the number of workers, a message will be sent only to the first args.size() workers. If the size of the args vector is bigger than the number of workers, only the first fNWorkers arguments will be sent.
Returns
the number of messages successfully sent

Definition at line 73 of file TMPClient.h.

template<class T >
unsigned TMPClient::Broadcast ( unsigned  code,
std::initializer_list< T > &  args 
)

Send a message with a different object to each server.

See TMPClient::Broadcast(unsigned code, const std::vector<T> &args) for more informations.

Definition at line 100 of file TMPClient.h.

template<class T >
unsigned TMPClient::Broadcast ( unsigned  code,
obj,
unsigned  nMessages = 0 
)

Send a message containing code and obj to each worker, up to a maximum number of nMessages workers.

See Broadcast(unsigned code, unsigned nMessages) for more informations.

Parameters
codethe code of the message to send (e.g. EMPCode)
objthe object to send
nMessagesthe maximum number of messages to send. If nMessages == 0, send a message to every worker.
Returns
the number of messages successfully sent

Definition at line 120 of file TMPClient.h.

void TMPClient::DeActivate ( TSocket s)

DeActivate a certain socket.

This does not remove it from the monitor: it will be reactivated by the next call to Broadcast() (or possibly other methods that are specified to do so).
A socket should be DeActivated when the corresponding worker is done for now and we want to stop listening to this worker's socket. If the worker is done forever, Remove() should be used instead.

Parameters
sthe socket to be deactivated

Definition at line 225 of file TMPClient.cxx.

bool TMPClient::Fork ( TMPWorker server)

This method forks the ROOT session into fNWorkers children processes.

The ROOT sessions spawned in this way will not have graphical capabilities and will not read from standard input, but will be connected to the original (interactive) session through TSockets. The children processes' PIDs are added to the fWorkerPids vector. The parent session can then communicate with the children using the Broadcast and MPSend methods, and receive messages through MPRecv.

Parameters
serverA pointer to an instance of the class that will take control of the subprocesses after forking. Applications should implement their own class inheriting from TMPWorker. Behaviour can be customized overriding TMPWorker::HandleInput.
Returns
true if Fork succeeded, false otherwise

Definition at line 89 of file TMPClient.cxx.

bool TMPClient::GetIsParent ( ) const
inline

Definition at line 37 of file TMPClient.h.

TMonitor& TMPClient::GetMonitor ( )
inline

Definition at line 36 of file TMPClient.h.

Referenced by TProcPool::Collect().

unsigned TMPClient::GetNWorkers ( ) const
inline

Definition at line 40 of file TMPClient.h.

Referenced by TProcPool::GetNWorkers().

void TMPClient::HandleMPCode ( MPCodeBufPair msg,
TSocket s 
)

Handle messages containing an EMPCode.

This method should be called upon receiving a message with a code >= 1000 (i.e. EMPCode). It handles the most generic types of messages.
Classes inheriting from TMPClient should implement a similar method to handle message codes specific to the application they're part of.

Parameters
msgthe MPCodeBufPair returned by a MPRecv call
sa pointer to the socket from which the message has been received is passed. This way HandleMPCode knows which socket to reply on.

Definition at line 273 of file TMPClient.cxx.

Referenced by TProcPool::Collect().

TMPClient& TMPClient::operator= ( const TMPClient )
delete
void TMPClient::ReapWorkers ( )

Wait on worker processes and remove their pids from fWorkerPids.

A blocking waitpid is called, but this should actually not block execution since ReapWorkers should only be called when all workers have already quit. ReapWorkers is then called not to leave zombie processes hanging around, and to clean-up fWorkerPids.

Definition at line 252 of file TMPClient.cxx.

Referenced by ~TMPClient().

void TMPClient::Remove ( TSocket s)

Remove a certain socket from the monitor.

A socket should be Removed from the monitor when the corresponding worker is done forever. For example HandleMPCode() calls this method on sockets pertaining to workers which sent an MPCode::kShutdownNotice.
If the worker is done for now, DeActivate should be used instead.

Parameters
sthe socket to be removed from the monitor fMon

Definition at line 239 of file TMPClient.cxx.

Referenced by TProcPool::Collect(), and HandleMPCode().

void TMPClient::SetNWorkers ( unsigned  n)
inline

Set the number of workers that will be spawned by the next call to Fork()

Definition at line 39 of file TMPClient.h.

Referenced by TProcPool::SetNWorkers().

Member Data Documentation

bool TMPClient::fIsParent
private

This is true if this is the parent/client process, false if this is a child/worker process.

Definition at line 47 of file TMPClient.h.

Referenced by Fork(), and GetIsParent().

TMonitor TMPClient::fMon
private

This object manages the sockets and detect socket events via TMonitor::Select.

Definition at line 49 of file TMPClient.h.

Referenced by Broadcast(), DeActivate(), Fork(), GetMonitor(), Remove(), and ~TMPClient().

unsigned TMPClient::fNWorkers
private

The number of workers that should be spawned upon forking.

Definition at line 50 of file TMPClient.h.

Referenced by Broadcast(), Fork(), GetNWorkers(), SetNWorkers(), and TMPClient().

std::vector<pid_t> TMPClient::fWorkerPids
private

A vector containing the PIDs of children processes/workers.

Definition at line 48 of file TMPClient.h.

Referenced by Fork(), and ReapWorkers().


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