X-Git-Url: https://git.ucc.asn.au/?p=progcomp2012.git;a=blobdiff_plain;f=judge%2Fmanager%2Fprogram.cpp;h=031d4113febc78d7f41cc9e10f0e7481ceb013f3;hp=588f7146c323f19f47369a5ab52845c40ebbc12f;hb=341297b4dce9528d54fe9dceeff0d6dc33f70abe;hpb=1a03b2543b67f0551e62babec4cd119f1e0e4640 diff --git a/judge/manager/program.cpp b/judge/manager/program.cpp index 588f714..031d411 100644 --- a/judge/manager/program.cpp +++ b/judge/manager/program.cpp @@ -20,7 +20,7 @@ using namespace std; * The child process closes unused sides of the pipe, and then calls exec to replace itself with the AI program * The parent process closes unused sides of the pipe, and sets up member variables - associates streams with the pipe fd's for convenience. */ -Program::Program(const char * executablePath) : input(NULL), output(NULL), pid(0) +Program::Program(const char * executablePath) : input(NULL), output(NULL), pid(0), paused(false) { //See if file exists and is executable... if (access(executablePath, X_OK) != 0) @@ -100,8 +100,42 @@ Program::~Program() } +/** + * Forces the program to pause by sending SIGSTOP + * Program can be resumed by calling Continue() (which sends SIGCONT) + * @returns true if the program could be paused, false if it couldn't (probably because it wasn't running) + */ +bool Program::Pause() +{ + if (pid > 0 && kill(pid,SIGSTOP) == 0) + { + paused = true; + return true; + } + return false; +} +/** + * Causes a paused program to continue + * @returns true if the program could be continued, false if it couldn't (probably because it wasn't running) + */ +bool Program::Continue() +{ + if (pid > 0 && kill(pid,SIGCONT) == 0) + { + paused = false; + return true; + } + return false; +} +/** + * @returns true iff the program is paused + */ +bool Program::Paused() const +{ + return paused; +} /** @@ -140,12 +174,13 @@ bool Program::SendMessage(const char * print, ...) */ bool Program::GetMessage(string & buffer, double timeout) { - if (!Running()) + if (!Running() || timeout == 0) return false; assert(&buffer != NULL); GetterThread getterThread(input, buffer); assert(&(getterThread.buffer) != NULL); + TimerThread timerThread(timeout*1000000); getterThread.Start(); @@ -164,7 +199,8 @@ bool Program::GetMessage(string & buffer, double timeout) } getterThread.Stop(); - timerThread.Stop(); + if (timeout > 0) + timerThread.Stop();