看门狗内置于与其控制的程序相同的进程中
我在日常构建中运行 Visual C++ 控制台测试程序。测试时不时地会调用一些被其他开发人员不当更改的函数,陷入无限循环并挂起,从而阻塞构建。
我需要一个尽可能简单的看门狗解决方案。这就是我的想法。在测试程序入口点中,我启动一个单独的线程,该线程连续循环并检查经过的时间。如果超过某个预定义的时间段,它将调用 TerminateProcess()。伪代码:
DWORD WatchDog( LPVOID)
{
DWORD start = GetTickCount();
while( true ) {
Sleep( ReasonablePeriod );
if( GetTickCount() - start > MaxAllowed ) {
TerminateProcess( GetCurrentProcess(), 0 );
}
}
return 0;
}
这个解决方案比作为单独的主程序实现的看门狗更糟糕吗?
I run a Visual C++ console test program inside the daily build. Every now and then the test would call some function that was changed by other developers improperly, descend into an infinite loop and hang thus blocking the build.
I need a watchdog solution as simple as possible. Here's what I came up with. In the test program entry point I start a separate thread that loops continuosly and checks elapsed time. If some predefined period is exceeded it calls TerminateProcess(). Pseudocode:
DWORD WatchDog( LPVOID)
{
DWORD start = GetTickCount();
while( true ) {
Sleep( ReasonablePeriod );
if( GetTickCount() - start > MaxAllowed ) {
TerminateProcess( GetCurrentProcess(), 0 );
}
}
return 0;
}
Is this solution any worse than a watchdog implemented as a separate master program?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为最好将看门狗作为一个单独的进程来实现。重用它更容易,检测应用程序是否崩溃并获取其返回代码更容易。
I think it's preferable to implement the watchdog as a separate process. It's easier to re-use it, it's easier to detect if your app crashed and to get its return code.