5

I'm implementing a checking system in C++. It runs executables with different tests. If the solution is not correct, it can take forever for it to finish with certain hard tests. That's why I want to limit the execution time to 5 seconds.

I'm using system() function to run executables:

system("./solution"); 

.NET has a great WaitForExit() method, what about native C++?. I'm also using Qt, so Qt-based solutions are welcome.

So is there a way to limit external process' execution time to 5 seconds?

Thanks

6 Answers 6

5

Use a QProcess with a QTimer so you can kill it after 5 seconds. Something like;

QProcess proc; QTimer timer; connect(&timer, SIGNAL(timeout()), this, SLOT(checkProcess()); proc.start("/full/path/to/solution"); timer.start(5*1000); 

and implement checkProcess();

void checkProcess() { if (proc.state() != QProcess::NotRunning()) proc.kill(); } 
Sign up to request clarification or add additional context in comments.

Comments

2

Use a separate thread for doing your required work and then from another thread, issue the pthread_cancle () call after some time (5 sec) to the worker thread. Make sure to register proper handler and thread's cancelability options.

For more details refer to: http://www.kernel.org/doc/man-pages/online/pages/man3/pthread_cancel.3.html

Comments

2

Check out Boost.Thread to allow you to make the system call in a separate thread and use the timed_join method to restrict the running time.

Something like:

void run_tests() { system("./solution"); } int main() { boost::thread test_thread(&run_tests); if (test_thread.timed_join(boost::posix_time::seconds(5))) { // Thread finished within 5 seconds, all fine. } else { // Wasn't complete within 5 seconds, need to stop the thread } } 

The hardest part is to determine how to nicely terminate the thread (note that test_thread is still running).

1 Comment

timed_join is deprecated and will be gone in boost 1.56, better use "test_thread.try_join_for(boost::chrono::seconds(5))"
1
void WaitForExit(void*) { Sleep(5000); exit(0); } 

And then use it (Windows specific):

_beginthread(WaitForExit, 0, 0); 

1 Comment

Sorry, I forgot to mention that I'm on Mac OS X, and I want it to be platform independent.
0

Solution testing system on Windows should use Job objects to restrict it's access to the system and execution time (not the real time, BTW).

2 Comments

I'm on Mac OS :) And I want to make it cross platform.
Anyway, terminating process based on real time measurements is wrong: if some background process awakes and starts to use substantial amount of CPU time while your "solution" is running, you'll terminate it earlier than needed.
0

If you are working with Posix compliant systems (of which MacOS and Unix generally are), use fork execv and ``waitpidinstead ofsystem`.An example can be found here. The only really tricky bit now is how to get a waitpid with a timeout. Take a look here for ideas.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.