From 857dcfbf3a82f2cd89c14a5efa92eca3144a3adb Mon Sep 17 00:00:00 2001 From: Ethereal Date: Sun, 14 Nov 2010 23:48:47 -0700 Subject: [PATCH] Successfully converted to using two threads rather than two processes. Next up is support for spawning off new aesalon monitors on a per-process basis . . . --- include/monitor/program/Conductor.h | 3 +++ include/monitor/program/Launcher.h | 4 ++-- monitor/src/Coordinator.cpp | 20 ++++++----------- monitor/src/program/Conductor.cpp | 10 ++++++--- monitor/src/program/Launcher.cpp | 43 ++++++++++++++----------------------- monitor/src/program/Link.cpp | 9 ++------ 6 files changed, 37 insertions(+), 52 deletions(-) diff --git a/include/monitor/program/Conductor.h b/include/monitor/program/Conductor.h index 00dfbc8..9bc661d 100644 --- a/include/monitor/program/Conductor.h +++ b/include/monitor/program/Conductor.h @@ -29,7 +29,10 @@ public: ~Conductor(); void monitor(); + + void join(); private: + static void *run(void *voidInstance); Link *newLink(uint32_t size); void loadModule(); }; diff --git a/include/monitor/program/Launcher.h b/include/monitor/program/Launcher.h index 6ef4977..b245626 100644 --- a/include/monitor/program/Launcher.h +++ b/include/monitor/program/Launcher.h @@ -23,17 +23,17 @@ class Launcher { private: char **m_argv; int m_controllerFds[2]; + pid_t m_targetPid; public: Launcher(char **argv); ~Launcher(); - pid_t startProcess(); + pid_t forkTarget(); int readFd() const { return m_controllerFds[0]; } void waitForChild(); private: void setupEnvironment(); - pid_t createProcess(); }; } // namespace Program diff --git a/monitor/src/Coordinator.cpp b/monitor/src/Coordinator.cpp index 7c6d543..a3ce543 100644 --- a/monitor/src/Coordinator.cpp +++ b/monitor/src/Coordinator.cpp @@ -55,22 +55,16 @@ void Coordinator::run() { return; } - Program::Launcher *launcher = new Program::Launcher(&m_argv[m_argcOffset]); + Program::Launcher launcher(&m_argv[m_argcOffset]); - pid_t monitored_pid = launcher->forkTarget(); + launcher.forkTarget(); - if(launcher->forkMonitor() == 0) { // the child - std::cout << "In monitor process . . ." << std::endl; - Program::Conductor conductor(launcher->readFd()); - conductor.monitor(); - std::cout << "about to leave the scope . . ." << std::endl; - } - else { - launcher->waitForChild(monitored_pid); - //launcher-> - } + std::cout << "In monitor process . . ." << std::endl; + Program::Conductor conductor(launcher.readFd()); + conductor.monitor(); - delete launcher; + launcher.waitForChild(); + conductor.join(); } void Coordinator::parseConfigs() { diff --git a/monitor/src/program/Conductor.cpp b/monitor/src/program/Conductor.cpp index e574aba..974359c 100644 --- a/monitor/src/program/Conductor.cpp +++ b/monitor/src/program/Conductor.cpp @@ -29,11 +29,9 @@ Conductor::Conductor(int readFd) : m_readFd(readFd) { Conductor::~Conductor() { std::cout << "Conductor destructing . . ." << std::endl; for(std::list::iterator i = m_linkList.begin(); i != m_linkList.end(); ++ i) { - std::cout << " Conductor::~Conductor(): terminating . . ." << std::endl; - (*i)->terminate(); - std::cout << " Conductor::~Conductor(): terminated!" << std::endl; delete (*i); } + std::cout << "Conductor destructed" << std::endl; } @@ -62,6 +60,12 @@ void Conductor::monitor() { std::cout << "Conductor::monitor finished!" << std::endl; } +void Conductor::join() { + for(std::list::iterator i = m_linkList.begin(); i != m_linkList.end(); ++ i) { + (*i)->terminate(); + } +} + Link *Conductor::newLink(uint32_t size) { uint16_t length; read(m_readFd, &length, sizeof(length)); diff --git a/monitor/src/program/Launcher.cpp b/monitor/src/program/Launcher.cpp index 61e1134..912b164 100644 --- a/monitor/src/program/Launcher.cpp +++ b/monitor/src/program/Launcher.cpp @@ -36,24 +36,31 @@ Launcher::~Launcher() { } -pid_t Launcher::startProcess() { +pid_t Launcher::forkTarget() { setupEnvironment(); - pid_t childPid = createProcess(); - - - pid_t monitorPid = fork(); - if(monitorPid == -1) { + m_targetPid = fork(); + if(m_targetPid == -1) { std::cout << "Could not fork . . ." << std::endl; exit(1); } - return monitorPid; + else if(m_targetPid == 0) { + close(m_controllerFds[0]); + execvp(m_argv[0], m_argv); + std::cout << m_argv[0] << ": " << strerror(errno) << std::endl; + exit(0); + } + + /* Close the write end of the pipe in the monitoring process. */ + close(m_controllerFds[1]); + + return m_targetPid; } -void Launcher::waitForChild(pid_t childPid) { +void Launcher::waitForChild() { std::cout << "In original process, waiting on process . . ." << std::endl; siginfo_t sinfo; - waitid(P_PID, childPid, &sinfo, WEXITED); + waitid(P_PID, m_targetPid, &sinfo, WEXITED); if(sinfo.si_code == CLD_DUMPED || sinfo.si_code == CLD_KILLED) Coordinator::instance()->setReturnValue(128 + sinfo.si_status); else Coordinator::instance()->setReturnValue(sinfo.si_status); @@ -61,24 +68,6 @@ void Launcher::waitForChild(pid_t childPid) { std::cout << "Main Process: monitored process has terminated." << std::endl; } -pid_t Launcher::createProcess() { - pid_t childPid = fork(); - if(childPid == -1) { - std::cout << "Could not fork . . ." << std::endl; - exit(1); - } - else if(childPid == 0) { - close(m_controllerFds[0]); - execvp(m_argv[0], m_argv); - std::cout << m_argv[0] << ": " << strerror(errno) << std::endl; - exit(0); - } - - close(m_controllerFds[1]); - - return childPid; -} - void Launcher::setupEnvironment() { pipe(m_controllerFds); diff --git a/monitor/src/program/Link.cpp b/monitor/src/program/Link.cpp index bce5ba5..44d3203 100644 --- a/monitor/src/program/Link.cpp +++ b/monitor/src/program/Link.cpp @@ -21,23 +21,18 @@ Link::Link(std::string name, uint32_t size) : m_sharedMemory(NULL) { } Link::~Link() { - std::cout << "Link destructing . . ." << std::endl; + terminate(); } void Link::listen() { - std::cout << "Link: creating thread . . ." << std::endl; - pthread_create(&m_threadID, NULL, run, NULL); + pthread_create(&m_threadID, NULL, run, this); } void Link::terminate() { - std::cout << "Link::terminate() called . . .\n"; - if(m_sharedMemory != NULL) { - std::cout << "m_sharedMemory is not NULL, sending termination notice . . ." << std::endl; m_sharedMemory->notifyTermination(); } - std::cout << "Link::terminate(): joining thread . . ." << std::endl; pthread_join(m_threadID, NULL); } -- 2.11.4.GIT