看门狗内置于与其控制的程序相同的进程中

发布于 2024-08-15 18:25:09 字数 517 浏览 6 评论 0原文

我在日常构建中运行 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

素罗衫 2024-08-22 18:25:09

我认为最好将看门狗作为一个单独的进程来实现。重用它更容易,检测应用程序是否崩溃并获取其返回代码更容易。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文