简体   繁体   English

如果超过5秒,如何使用C ++退出进程?

[英]How to exit a process run with C++ if it takes more than 5 seconds?

I'm implementing a checking system in C++. 我正在用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. 这就是为什么我想将执行时间限制为5秒。

I'm using system() function to run executables: 我正在使用system()函数来运行可执行文件:

system("./solution");

.NET has a great WaitForExit() method, what about native C++?. .NET有一个很好的WaitForExit()方法,那么本机C ++呢? I'm also using Qt, so Qt-based solutions are welcome. 我也使用Qt,因此欢迎基于Qt的解决方案。

So is there a way to limit external process' execution time to 5 seconds? 那么有没有办法将外部进程的执行时间限制为5秒?

Thanks 谢谢

Use a QProcess with a QTimer so you can kill it after 5 seconds. QProcessQTimer配合使用,以便在5秒后将其杀死。 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() ; 并实现checkProcess() ;

void checkProcess()
{
    if (proc.state() != QProcess::NotRunning())
        proc.kill();
}

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. 使用单独的线程执行所需的工作,然后从另一个线程,在工作线程一段时间(5秒)后发出pthread_cancle ()调用。 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 有关更多详细信息,请访问: http//www.kernel.org/doc/man-pages/online/pages/man3/pthread_cancel.3.html

void WaitForExit(void*)
{
    Sleep(5000);
    exit(0);
}

And then use it (Windows specific): 然后使用它(Windows特定):

_beginthread(WaitForExit, 0, 0);

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. 检查Boost.Thread以允许您在单独的线程中进行系统调用,并使用timed_join方法来限制运行时间。

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). 最难的部分是确定如何很好地终止线程(注意test_thread仍在运行)。

Windows上的解决方案测试系统应该使用Job对象来限制它对系统和执行时间的访问(而不是实时,BTW)。

If you are working with Posix compliant systems (of which MacOS and Unix generally are), use fork execv and ``waitpid instead of system`.An example can be found here . 如果你正在使用Posix兼容系统(通常是MacOS和Unix),请使用fork execv和``waitpid instead of system`。可以在这里找到一个例子。 The only really tricky bit now is how to get a waitpid with a timeout. 现在唯一真正棘手的一点是如何获得一个超时的waitpid。 Take a look here for ideas. 看看这里的想法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM