X-Git-Url: https://git.ucc.asn.au/?p=progcomp2012.git;a=blobdiff_plain;f=judge%2Fmanager%2Fprogram.cpp;h=2d09a510aa6e9c70ba0c0f0bf4922812de7cae63;hp=588f7146c323f19f47369a5ab52845c40ebbc12f;hb=5f9adddd695f2664a0d690b59a779e40b51ade3d;hpb=1a03b2543b67f0551e62babec4cd119f1e0e4640 diff --git a/judge/manager/program.cpp b/judge/manager/program.cpp index 588f714..2d09a51 100644 --- a/judge/manager/program.cpp +++ b/judge/manager/program.cpp @@ -6,7 +6,8 @@ #include "thread_util.h" #include "program.h" - +#include +#include using namespace std; @@ -20,8 +21,45 @@ 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) { + + + + vector args; + if (executablePath[0] != '"') + args.push_back((char*)executablePath); + else + args.push_back((char*)(executablePath)+1); + char * token = NULL; + do + { + token = strstr(args[args.size()-1], " "); + if (token == NULL) + break; + + *token = '\0'; + do + { + ++token; + if (*token == '"') + *token = '\0'; + } + while (*token != '\0' && iswspace(*token)); + + if (*token != '\0' && !iswspace(*token)) + { + args.push_back(token); + } + else + break; + } + while (token != NULL); + + char ** arguments = new char*[args.size()+2]; + for (unsigned int i=0; i < args.size(); ++i) + arguments[i] = args[i]; + //See if file exists and is executable... if (access(executablePath, X_OK) != 0) { @@ -51,7 +89,7 @@ Program::Program(const char * executablePath) : input(NULL), output(NULL), pid(0 if (access(executablePath, X_OK) == 0) //Check we STILL have permissions to start the file - execl(executablePath, executablePath, (char*)(NULL)); ///Replace process with desired executable + execv(executablePath,arguments); ///Replace process with desired executable fprintf(stderr, "Program::Program - Could not run program \"%s\"!\n", executablePath); exit(EXIT_FAILURE); //We will probably have to terminate the whole program if this happens @@ -100,8 +138,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 +212,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 +237,8 @@ bool Program::GetMessage(string & buffer, double timeout) } getterThread.Stop(); - timerThread.Stop(); + if (timeout > 0) + timerThread.Stop();