当用户杀死进程时发出信号?

发布于 2024-07-12 09:15:08 字数 452 浏览 9 评论 0原文

我超载了本网站上列出的 6 个信号 http://www.cplusplus.com /reference/clibrary/csignal/signal.html

然后我运行我的应用程序(双击而不是通过 IDE 运行)并尝试 1) 结束任务 2) 右上角的 X 和 3) 终止进程。 我预计前两个会引起某种信号(我使用的是 XP),但可惜我什么也没得到。 当信号发生时,我是否不允许打开文件进行写入? 我猜我是(SIGSEGV 允许我)。

当 Firefox 崩溃或我杀死它时,它会记住我所在的页面。 它是在我每次单击页面时记录地址还是在信号/崩溃时记录地址?

我的主要问题是我可以使用什么信号来捕获终止进程

I overloaded the 6 signals listed on this site http://www.cplusplus.com/reference/clibrary/csignal/signal.html

Then i ran my app (double click not ran through IDE) and tried 1) end task 2) X on topright and 3) kill process. I expected the first two to cause some kind of signal (i am on XP) but alas i got nothing. Am i not allowed to open files to write into when a signal occurs? i am guessing i am (SIGSEGV allowed me).

When firefox crashes or when i kill it, it remembers what pages i was. Does it log the address everytime i click a page or does it do that on a signal/crash?

my main question is what signal can i use to catch kill process

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

唔猫 2024-07-19 09:15:08

Win32 不提供拦截使用 TerminateProcess 终止程序的选项(当您从任务管理器中“结束任务”或单击 [X] 时会发生这种情况)。

您可以捕获 SIGSEGV 信号,因为 C 运行时库在 Windows 上运行时提供了此信号的模拟。 当您的程序导致 Windows 访问冲突(异常 0xC0000005)时,运行时库可以选择捕获该问题并为您模拟 Unix 风格的 SIGSEGV。 然而,这并不是处理此类异常的最佳方法。 如果您正在编写 Win32 程序,通常不应尝试使用 Unix 风格的服务。

Win32 does not provide an option to intercept your program being killed with TerminateProcess (which is what will happen when you "End Task" from Task Manager or click on the [X]).

You can catch the SIGSEGV signal because the C runtime library provides an emulation of this signal when running on Windows. When your program causes a Windows access violation (exception 0xC0000005), the runtime library has the option to catch that and simulate a Unix style SIGSEGV for you. This is, however, not the best way to handle such an exception. If you are writing a Win32 program, you shouldn't generally try to use Unix style services.

请止步禁区 2024-07-19 09:15:08

如果您覆盖调用 SetUnhandledExceptionFilter 的默认异常处理程序(这是一个 win32 函数,因此不依赖于 C 库模拟),您可以捕获运行时错误,例如访问冲突。 这是在程序崩溃时提供“小型转储”的方法。

但是,当您正常关闭应用程序或从任务管理器关闭应用程序时,不会调用此异常处理程序。 在最后一种情况下,Windows 正在调用 TerminateProcess,这不是干净的关闭,而是强制您的程序终止。

我不知道 Firefox 使用的是哪个实现,但是保存当前打开的选项卡可能会运行一个计时器,并且每次运行它都会将历史记录保存到文件和某种脏标记中。

检测程序何时关闭的其他更复杂的解决方案(由防病毒程序和类似程序实现)是运行两个不相关的程序,每个程序检查另一个程序是否仍在运行,如果一个程序检测到另一个程序已关闭,则再次运行它。

You can catch runtime error like an access violation if you override the default exception handler calling SetUnhandledExceptionFilter (this is a win32 function and as such doesn't rely on C library emulation). This is the method can used to provide "minidumps" when a program crashes.

But this exception handler will not be called when you normally close your application, or when your application is closed from Task manager. In the last case windows is calling TerminateProcess, is not a clean shutdown but it is forcing your program to terminate.

I'm not aware of which is the implementation used by Firefox, but to save the current tabs open is likely to have a timer running, and each time it is run it save the history to a file and some kind of dirty mark.

Other more complex solutions to detect when a program is closed (implemented by antivirus and similar programs) is to have two unrelated programs running, each checking that the other is still running, and if one detect the other was closed the run it again.

情深已缘浅 2024-07-19 09:15:08

Windows 应用程序是控制台应用程序或 GUI 应用程序。 控制台应用程序倾向于获取 WM_CLOSE,控制台应用程序倾向于获取 CTRL_CLOSE_EVENT。 信号也不是; 如果您的应用程序通过 TerminateProcess() 结束,则两者都不会发送。

如果您想存储您所在的位置,请使用内存映射文件并在每次操作时更新它。 当进程退出时,内存中的脏页会被操作系统写回文件,也可能在其他时刻。 该解决方案允许操作系统为您管理磁盘 I/O,并且它可以更好地做到这一点。

Windows apps are either console apps or GUI apps. Console apps tend to get WM_CLOSE, console apps CTRL_CLOSE_EVENT. Neither are signals; neither would be sent if your app is ended via TerminateProcess().

If you want to store where you were, use a memory-mapped file and update that on every action. When your process exits, the dirty page in memory is written back to file by the OS, possibly at other moments too. This solution allows the OS to manage disk I/O for you, and it's in a better position to do so.

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