a Code for the Combination of Indirect and Direct Constraints on High Energy Physics Models Logo
WaitingOnSubprocessExecutor.hpp
Go to the documentation of this file.
1/*
2 * WaitingOnSubprocessExecutor.hpp
3 *
4 * Created on: Jan 8, 2012
5 * Author: Ben O'Leary (benjamin.oleary@gmail.com)
6 *
7 * This file is part of BOLlib, released under the
8 * GNU General Public License. Please see the accompanying
9 * README.BOLlib.txt file for a full list of files, brief documentation
10 * on how to use these classes, and further details on the license.
11 */
12
13#ifndef WAITINGONSUBPROCESSEXECUTOR_HPP_
14#define WAITINGONSUBPROCESSEXECUTOR_HPP_
15
16#include <csignal>
17#include <ctime>
18#include <sys/types.h>
19#include <sys/wait.h>
20#include <unistd.h>
21#include "StringParser.hpp"
22#include "VectorlikeArray.hpp"
23
24namespace BOL
25{
27 {
28 public:
30 std::string const& executableStringIncludingArguments,
31 bool const isVerbose,
32 int const patienceMilliseconds = 10000 );
34
35 void
36 setExecutableName( std::string const& executableName );
37 void
39 /* the setArguments functions assign memory to make argumentListAsArray
40 * point to an array of pointers to chars which are stored in the strings
41 * of executableNameAndArguments & thus can be passed to execv to properly
42 * execute the subprocess with its arguments.
43 */
44 void
45 setArguments( std::string const& argumentsAsSingleString );
46 bool
48
49
50 protected:
51 static int const sleepingMicroseconds;
52
54 // this is for checking whether the process is the parent or the forked
55 // child subprocess.
56 bool const isVerbose;
57 // this is to allow the suppression of the generated reports.
59 char const** argumentListAsArray;
60 /* this is to hold the arguments for the executable. it is a constant
61 * pointer to a pointer to a char because execv is ancient & requires
62 * arguments in an irritatingly silly format.
63 */
65 /* this is the number of clock ticks for which the parent process will
66 * wait before losing patience & killing the subprocess, if the subprocess
67 * hasn't finished.
68 */
69 clock_t killingTime;
70 // this is the clock() value at which the parent process has run out of
71 // patience.
72
73 void
75 };
76
77
78
79 inline void
81 std::string const& executableName )
82 {
83 executableNameAndArguments.getFront().assign( executableName );
85 }
86
87 inline void
89 {
90 this->patienceTicks = patienceTicks;
91 }
92
93
94 inline void
96 std::string const& argumentsAsSingleString )
97 /* the setArguments functions assign memory to make argumentListAsArray
98 * point to an array of pointers to chars which are stored in the strings
99 * of executableNameAndArguments & thus can be passed to execv to properly
100 * execute the subprocess with its arguments.
101 */
102 {
103 executableNameAndArguments.setSize( 1 );
104 StringParser::parseByChar( argumentsAsSingleString,
107 }
108
109 inline void
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 }
130
131}
132
133#endif /* WAITINGONSUBPROCESSEXECUTOR_HPP_ */
static void parseByChar(std::string const &stringToParse, VectorlikeArray< std::string > &destinationArray, std::string const &divisionCharSet=whitespaceChars)
VectorlikeArray< std::string > executableNameAndArguments
void setExecutableName(std::string const &executableName)
void setArguments(std::string const &argumentsAsSingleString)
void setPatienceTicks(int const patienceTicks)
WaitingOnSubprocessExecutor(std::string const &executableStringIncludingArguments, bool const isVerbose, int const patienceMilliseconds=10000)