为什么SIGTERM在Windows上不起作用

Why doesn't SIGTERM Works On Windows

本文关键字:不起作用 Windows SIGTERM 为什么      更新时间:2023-10-16

这是我的代码:

void signalHandler(int sigNum) {
    OutputDebugStringA("i'm terminatedn");
    exit(sigNum);
}
int APIENTRY _tWinMain(_In_ HINSTANCE hInstance,
    _In_opt_ HINSTANCE hPrevInstance,
    _In_ LPTSTR    lpCmdLine,
    _In_ int       nCmdShow)
{
    signal(SIGTERM, signalHandler);
    while (1)
    {
        Sleep(100);
    }
    return 0;
}

当我通过Windows taskmgr终止它时,调试字符串没有显示。我的代码有问题吗?

Windows不像

Unix系统那样使用信号。

例如,它不会生成 SIGTERM。

TaskManager使用TerminateProcess API 调用来终止选定的进程。终止的进程不会收到任何通知。所以你不能用SIGTERM信号来处理它。