Windows 上是否有 posix SIGTERM 替代方案?-(对控制台应用程序的温和杀死)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2007516/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Is there a posix SIGTERM alternative on Windows? - (A gentle kill for console application)
提问by Piotr Czapla
I have a console daemon that is run by a GUI application. When the GUI application is terminated I'd like to stop the daemon as well.
我有一个由 GUI 应用程序运行的控制台守护程序。当 GUI 应用程序终止时,我也想停止守护进程。
How can I do it in a gentle way on windows?
我怎样才能在窗户上以温和的方式做到这一点?
On Linux, I would just use SIGTERM is there a similar mechanism on windows for console applications?
在 Linux 上,我只会使用 SIGTERM 在 Windows 上是否有类似的机制用于控制台应用程序?
To provide a bit more detail, the daemon app is written in python and the gui is written in C# & windows forms.
为了提供更多细节,守护程序应用程序是用 python 编写的,gui 是用 C# 和 windows 形式编写的。
采纳答案by Seva Alekseyev
Define "gentle" :)
定义“温柔”:)
I'm assuming there is already a communication mechanism in place between the daemon and the GUI. Just introduce a "quit" command and send it.
我假设守护进程和 GUI 之间已经存在通信机制。只需引入“退出”命令并发送即可。
If you want to kill the daemon even if it's busy doing something (or is frozen), use TerminateProcess().
如果您想杀死守护进程,即使它正忙于做某事(或被冻结),请使用 TerminateProcess()。
To have the best of both, you can send "quit", then wait on the process handle for some time (WaitForSingleObject()). If the daemon process does not die in, say, 5 sec, then terminate it.
为了两者兼得,您可以发送“退出”,然后等待进程句柄一段时间(WaitForSingleObject())。如果守护进程没有在 5 秒内终止,则终止它。
If the main thread of the daemon is prone to long periods of busy activity, have the daemon start a background thread that does nothing but waits for a named event. To signal that thread, open the event by name from GUI, then raise it. It's up to the daemon what to do upon event detection, but at least it will be a controlled shutdown.
如果守护进程的主线程容易出现长时间的繁忙活动,让守护进程启动一个后台线程,它只等待命名事件。要向该线程发出信号,请从 GUI 按名称打开事件,然后引发它。检测到事件时要做什么取决于守护进程,但至少它将是受控关闭。
回答by Kevin Montrose
Windows doesn't have signals in the way you're thinking.
Windows 没有您想象中的信号。
There's some infrastructure for changing how the (faked) SIGTERM and SIGBREAK are handled by console apps, mostly SetConsoleCtrlHandler
and GenerateConsoleCtrlEvent
but both are only of use in the console application itself; not from outside.
有一些基础设施可以改变控制台应用程序处理(伪造的)SIGTERM 和 SIGBREAK 的方式,大多数情况下SetConsoleCtrlHandler
,GenerateConsoleCtrlEvent
但两者都只在控制台应用程序本身中使用;不是来自外面。
It's worth noting that all a windows console app does when it receives a SIGTERM is call ExitProcess
, nothing special. I'm not 100% on what the python equivalent is called, but whatever standard "exit" call should be equivalent.
值得注意的是,Windows 控制台应用程序在收到 SIGTERM 时所做的一切都是 call ExitProcess
,没什么特别的。我不是 100% 对 python 等价物的调用是什么,但任何标准的“退出”调用都应该是等价的。
I'd suggest writing some code to signal the console app, causing it to call ExitProcess
itself. If that's not an option, use TerminateProcess
(equivalent Process.Kill) to close the console process from the outside; attempting to "fake" an ExitProcess
is dangerous for reasons noted in the MSDN article.
我建议编写一些代码来向控制台应用程序发出信号,使其ExitProcess
自行调用。如果这不是一个选项,请使用TerminateProcess
(等效Process.Kill)从外部关闭控制台进程;ExitProcess
由于 MSDN 文章中提到的原因,试图“伪造” an是危险的。