fork off n children and keep the parent process as a monitor

This commit is contained in:
Stephen Birarda 2013-09-09 11:59:58 -07:00
parent d1c602df07
commit bb073bdff3

View file

@ -32,16 +32,19 @@ int main(int argc, const char* argv[]) {
}
const char* NUM_FORKS_PARAMETER = "-n";
const char* numForksIncludingParentString = getCmdOption(argc, argv, NUM_FORKS_PARAMETER);
if (numForksIncludingParentString) {
int numForksIncludingParent = atoi(numForksIncludingParentString);
qDebug() << "Starting" << numForksIncludingParent << "assignment clients.";
const char* numForksString = getCmdOption(argc, argv, NUM_FORKS_PARAMETER);
int processID = 0;
int numForks = 0;
if (numForksString) {
numForks = atoi(numForksString);
qDebug() << "Starting" << numForks << "assignment clients.";
// fire off as many children as we need (this is one less than the parent since the parent will run as well)
for (int i = 0; i < numForksIncludingParent - 1; i++) {
for (int i = 0; i < numForks; i++) {
processID = fork();
if (processID == 0) {
@ -51,6 +54,9 @@ int main(int argc, const char* argv[]) {
}
}
if (processID == 0 || numForks == 0) {
// this is one of the child forks or there is a single assignment client, continue assignment-client execution
// create a NodeList as an unassigned client
NodeList* nodeList = NodeList::createInstance(NODE_TYPE_UNASSIGNED);
@ -110,4 +116,8 @@ int main(int argc, const char* argv[]) {
nodeList->clear();
}
}
} else {
// don't bail until all children have finished
wait(NULL);
}
}