a Code for the Combination of Indirect and Direct Constraints on High Energy Physics Models Logo
BOL::WaitingOnSubprocessExecutor Class Reference

#include <WaitingOnSubprocessExecutor.hpp>

Detailed Description

Definition at line 26 of file WaitingOnSubprocessExecutor.hpp.

Public Member Functions

bool forkAndExecvAndWait ()
 
void setArguments (std::string const &argumentsAsSingleString)
 
void setExecutableName (std::string const &executableName)
 
void setPatienceTicks (int const patienceTicks)
 
 WaitingOnSubprocessExecutor (std::string const &executableStringIncludingArguments, bool const isVerbose, int const patienceMilliseconds=10000)
 
 ~WaitingOnSubprocessExecutor ()
 

Protected Member Functions

void setUpArgumentCharArray ()
 

Protected Attributes

char const ** argumentListAsArray
 
VectorlikeArray< std::string > executableNameAndArguments
 
bool const isVerbose
 
clock_t killingTime
 
clock_t patienceTicks
 
int processId
 

Static Protected Attributes

static int const sleepingMicroseconds
 

Constructor & Destructor Documentation

◆ WaitingOnSubprocessExecutor()

BOL::WaitingOnSubprocessExecutor::WaitingOnSubprocessExecutor ( std::string const &  executableStringIncludingArguments,
bool const  isVerbose,
int const  patienceMilliseconds = 10000 
)

Definition at line 19 of file WaitingOnSubprocessExecutor.cpp.

22 :
23 processId( 0 ),
26 argumentListAsArray( NULL ),
27 patienceTicks( ( ( patienceMilliseconds * CLOCKS_PER_SEC ) / 1000 ) ),
29 {
30 StringParser::parseByChar( executableStringIncludingArguments,
33 }
static void parseByChar(std::string const &stringToParse, VectorlikeArray< std::string > &destinationArray, std::string const &divisionCharSet=whitespaceChars)
VectorlikeArray< std::string > executableNameAndArguments

◆ ~WaitingOnSubprocessExecutor()

BOL::WaitingOnSubprocessExecutor::~WaitingOnSubprocessExecutor ( )

Definition at line 35 of file WaitingOnSubprocessExecutor.cpp.

36 {
37 delete[] argumentListAsArray;
38 }

Member Function Documentation

◆ forkAndExecvAndWait()

bool BOL::WaitingOnSubprocessExecutor::forkAndExecvAndWait ( )

Definition at line 42 of file WaitingOnSubprocessExecutor.cpp.

50 {
51 killingTime = ( clock() + patienceTicks );
52 int processStatus( 0 );
53 // this keeps track of the status of the process.
54 int returnValue( 0 );
55 // this keeps track of what processes return.
56 bool subprocessReturned( true );
57 // assume that the subprocess won't have to be killed.
58 int forkReturnValue( fork() );
59 if( 0 > forkReturnValue )
60 // if fork failed to do what it should do...
61 {
62 std::cout
63 << std::endl
64 << "BOL::error! WaitingOnSubprocessExecutor::forkAndExecvAndWait() was"
65 << " unable to create the subprocess; there was an error and fork()"
66 << " returned " << forkReturnValue << std::endl;
67 // print error message.
68 }
69 else if( 0 == forkReturnValue )
70 // otherwise if this is the subprocess
71 // [since it got the 0 from fork()]...
72 {
73 processId = getpid();
74 if( isVerbose )
75 {
76 std::cout
77 << std::endl
78 << "now executing \"" << argumentListAsArray[ 0 ];
79 for( int argumentWritingCounter( 1 );
80 executableNameAndArguments.getSize() > argumentWritingCounter;
81 ++argumentWritingCounter )
82 {
83 std::cout
84 << " " << argumentListAsArray[ argumentWritingCounter ];
85 }
86 std::cout
87 << "\", with process id " << getpid() << ", real user id " << getuid()
88 << " & effective user id " << geteuid() << std::endl;
89 }
90 returnValue = execv( argumentListAsArray[ 0 ],
91 (char* const*)argumentListAsArray );
92 // run the executable with its arguments.
93 exit( returnValue );
94 }
95 else if( 0 < forkReturnValue )
96 // otherwise this is the parent process, since it got given the process
97 // id of the subprocess.
98 {
99 bool subprocessFinished( false );
100 // the tracker of whether the subprocess has changed state or not.
101 while( clock() < killingTime )
102 {
103 if( waitpid( forkReturnValue,
104 &processStatus,
105 WNOHANG ) == forkReturnValue )
106 /* if the waitpid() function waited for the subprocess (with
107 * process id fork_return) and the subprocess changed state, so
108 * that waitpid() returned the subprocess's process id...
109 */
110 {
111 killingTime = clock() - 1;
112 // note that we don't have to wait any more.
113 subprocessFinished = true;
114 // note that the subprocess changed state.
115 }
116 else
117 {
118 usleep( sleepingMicroseconds );
119 // wait 1000 microseconds = 1 millisecond.
120 }
121 }
122 if( false == subprocessFinished )
123 // if we have waited over (patienceTicks/1000) seconds & the
124 // subprocess has not changed state...
125 {
126 if( isVerbose )
127 {
128 std::cout
129 << std::endl
130 << "killing forked process " << forkReturnValue
131 << " due to it taking over " << patienceTicks << " milliseconds"
132 << std::endl;
133 // print warning about hanging subprocess.
134 }
135 kill( forkReturnValue,
136 9 );
137 // kill the hanging process.
138 waitpid( -1,
139 &processStatus,
140 0 );
141 // wait for all this killing to finish? I'm not entirely sure.
142 subprocessReturned = false;
143 // note that the subprocess had to be killed because it did not
144 // return a value.
145 }
146 returnValue = WEXITSTATUS( processStatus );
147 } // end of fork() statement.
148 return subprocessReturned;
149 }

◆ setArguments()

void BOL::WaitingOnSubprocessExecutor::setArguments ( std::string const &  argumentsAsSingleString)
inline

Definition at line 95 of file WaitingOnSubprocessExecutor.hpp.

102 {
103 executableNameAndArguments.setSize( 1 );
104 StringParser::parseByChar( argumentsAsSingleString,
107 }

◆ setExecutableName()

void BOL::WaitingOnSubprocessExecutor::setExecutableName ( std::string const &  executableName)
inline

Definition at line 80 of file WaitingOnSubprocessExecutor.hpp.

82 {
83 executableNameAndArguments.getFront().assign( executableName );
85 }

◆ setPatienceTicks()

void BOL::WaitingOnSubprocessExecutor::setPatienceTicks ( int const  patienceTicks)
inline

Definition at line 88 of file WaitingOnSubprocessExecutor.hpp.

89 {
91 }

◆ setUpArgumentCharArray()

void BOL::WaitingOnSubprocessExecutor::setUpArgumentCharArray ( )
inlineprotected

Definition at line 110 of file WaitingOnSubprocessExecutor.hpp.

111 {
112 delete[] argumentListAsArray;
114 = new char const*[ ( executableNameAndArguments.getSize() + 1 ) ];
115 // create a char* const array which has room for the executable name,
116 // then all the arguments, then NULL.
117 for( int argumentCharArrayCounter( 0 );
118 executableNameAndArguments.getSize() > argumentCharArrayCounter;
119 ++argumentCharArrayCounter )
120 // go through the list of arguments.
121 {
122 argumentListAsArray[ argumentCharArrayCounter ]
123 = executableNameAndArguments[ argumentCharArrayCounter ].c_str();
124 // write this argument as the (argumentCharArrayCounter)th element.
125 }
127 // we fill the (argumentCharArrayCounter + 1)th element with NULL, since
128 // that is what execv() wants.
129 }

Member Data Documentation

◆ argumentListAsArray

char const** BOL::WaitingOnSubprocessExecutor::argumentListAsArray
protected

Definition at line 59 of file WaitingOnSubprocessExecutor.hpp.

◆ executableNameAndArguments

VectorlikeArray< std::string > BOL::WaitingOnSubprocessExecutor::executableNameAndArguments
protected

Definition at line 58 of file WaitingOnSubprocessExecutor.hpp.

◆ isVerbose

bool const BOL::WaitingOnSubprocessExecutor::isVerbose
protected

Definition at line 56 of file WaitingOnSubprocessExecutor.hpp.

◆ killingTime

clock_t BOL::WaitingOnSubprocessExecutor::killingTime
protected

Definition at line 69 of file WaitingOnSubprocessExecutor.hpp.

◆ patienceTicks

clock_t BOL::WaitingOnSubprocessExecutor::patienceTicks
protected

Definition at line 64 of file WaitingOnSubprocessExecutor.hpp.

◆ processId

int BOL::WaitingOnSubprocessExecutor::processId
protected

Definition at line 53 of file WaitingOnSubprocessExecutor.hpp.

◆ sleepingMicroseconds

int const BOL::WaitingOnSubprocessExecutor::sleepingMicroseconds
staticprotected

Definition at line 51 of file WaitingOnSubprocessExecutor.hpp.


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