Classes | Public Member Functions | Static Public Member Functions | Private Attributes | Friends | List of all members
PLMD::Subprocess Class Reference

Class managing a subprocess. More...

#include <Subprocess.h>

Classes

class  Handler
 Class used to cont/stop a Subprocess in an exception safe manner. More...
 

Public Member Functions

 Subprocess (const std::string &cmd)
 Constructor with a command line. More...
 
 ~Subprocess ()
 Destructor. More...
 
void cont () noexcept
 Send a SIGCONT to the subprocess. More...
 
Handler contStop () noexcept
 Returns a handler to temporarily resume the process. More...
 
void flush ()
 Flush communication to process. More...
 
Subprocessgetline (std::string &)
 Get a line from the subprocess. More...
 
void stop () noexcept
 Send a SIGSTOP to the subprocess. More...
 

Static Public Member Functions

static bool available () noexcept
 Check if subprocess facilities are available. More...
 

Private Attributes

IFile child_to_parent
 PLUMED file object, child to parent. More...
 
int fcp =0
 File descriptor, child to parent. More...
 
int fpc =0
 File descriptor, parent to child. More...
 
FILE * fpcp =NULL
 File pointer, child to parent. More...
 
FILE * fppc =NULL
 File pointer, parent to child. More...
 
OFile parent_to_child
 PLUMED file object, parent to child. More...
 
std::unique_ptr< SubprocessPidpid
 Process ID. More...
 

Friends

template<class T >
Subprocessoperator<< (Subprocess &ep, const T &t)
 Write something to the subprocess. More...
 

Detailed Description

Class managing a subprocess.

The subprocess is launched and one can interact with it through a pipe.

In order not to consume resources, it might be possible to use this syntax:

// at construction:
Subprocess sp;
sp.stop();

// when needed
{
  auto h=sp.contStop();
  sp<<"command\n";
  sp.flush();
  sp.getline(answer);
}
// when h goes out of scope, subprocess is stopped again.
// If an exception is raised in the block, the subprocess is stopped as well.
Warning
Currently stop and cont are giving problems with some MPI implementation, In addition, notice that the stop signal is only sent to the child process and not to the subsequently spawn processes, so it might not work as intended. This feature is left here but is probably no a good idea to use it. It can be enabled with export PLUMED_ENABLE_SIGNALS=1.

Constructor & Destructor Documentation

◆ Subprocess()

PLMD::Subprocess::Subprocess ( const std::string &  cmd)
explicit

Constructor with a command line.

◆ ~Subprocess()

PLMD::Subprocess::~Subprocess ( )

Destructor.

Member Function Documentation

◆ available()

bool PLMD::Subprocess::available ( )
staticnoexcept

Check if subprocess facilities are available.

If it returns false, any call to Subprocess constructor will raise an exception.

◆ cont()

void PLMD::Subprocess::cont ( )
noexcept

Send a SIGCONT to the subprocess.

Better used through contStop() method.

◆ contStop()

Handler PLMD::Subprocess::contStop ( )
inlinenoexcept

Returns a handler to temporarily resume the process.

◆ flush()

void PLMD::Subprocess::flush ( )

Flush communication to process.

◆ getline()

Subprocess & PLMD::Subprocess::getline ( std::string &  line)

Get a line from the subprocess.

◆ stop()

void PLMD::Subprocess::stop ( )
noexcept

Send a SIGSTOP to the subprocess.

Better used through contStop() method.

Friends And Related Function Documentation

◆ operator<<

template<class T >
Subprocess& operator<< ( Subprocess ep,
const T &  t 
)
friend

Write something to the subprocess.

Member Data Documentation

◆ child_to_parent

IFile PLMD::Subprocess::child_to_parent
private

PLUMED file object, child to parent.

Used to simplify formatting

◆ fcp

int PLMD::Subprocess::fcp =0
private

File descriptor, child to parent.

◆ fpc

int PLMD::Subprocess::fpc =0
private

File descriptor, parent to child.

◆ fpcp

FILE* PLMD::Subprocess::fpcp =NULL
private

File pointer, child to parent.

◆ fppc

FILE* PLMD::Subprocess::fppc =NULL
private

File pointer, parent to child.

◆ parent_to_child

OFile PLMD::Subprocess::parent_to_child
private

PLUMED file object, parent to child.

Used to simplify formatting

◆ pid

std::unique_ptr<SubprocessPid> PLMD::Subprocess::pid
private

Process ID.

We store this rather than pid_t to avoid including <unistd.h> in this header file. This remains nullptr in the child process.


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