C++ 使用信号情报

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/482702/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 15:35:05  来源:igfitidea点击:

Using SIGINT

c++windowssignals

提问by Jon

According to this http://www.cplusplus.com/reference/clibrary/csignal/signal.html

根据这个http://www.cplusplus.com/reference/clibrary/csignal/signal.html

SIGINTis generally used/cause by the user. How do i cause a SIGINTin c++? i seen an example using kill(pid, SIGINT);but i rather cause it another way. Also i am using windows.

SIGINT通常由用户使用/引起。我如何SIGINT在 C++ 中导致 a ?我看到了一个例子,kill(pid, SIGINT);但我宁愿以另一种方式导致它。我也在使用窗户。

回答by Joao da Silva

C89 and C99 define raise() in signal.h:

C89 和 C99 在 signal.h 中定义了 raise():

#include <signal.h>

int raise(int sig);

This function sends a signal to the calling process, and is equivalent to

该函数向调用进程发送信号,相当于

kill(getpid(), sig);

If the platform supports threads, then the call is equivalent to

如果平台支持线程,那么调用就相当于

pthread_kill(pthread_self(), sig);

The return value is 0 on success, nonzero otherwise.

成功时返回值为 0,否则为非零。

回答by Jon

You cause a SIGINTby pressing Ctrl+C.

您可以SIGINT通过按引起 a Ctrl+C

Example code:

示例代码:

#include <stdio.h>
#include <stdlib.h>
#include <signal.h>

void siginthandler(int param)
{
  printf("User pressed Ctrl+C\n");
  exit(1);
}

int main()
{
  signal(SIGINT, siginthandler);
  while(1);
  return 0;
}

When run:

运行时:

$ ./a.out 
^CUser pressed Ctrl+C
$ 

(Note that this is pure C code, should work in C++ though)

(请注意,这是纯 C 代码,不过应该可以在 C++ 中工作)

Edit: The only way I know of to send SIGINTapart from interactively pressing Ctrl+Cis using kill(pid, SIGINT)as you said...

编辑:我所知道的SIGINT除了交互式按压之外的唯一发送方式Ctrl+C是使用kill(pid, SIGINT)你所说的......

回答by Greg Hewgill

What other way are you thinking of? The kill()function is the only way the kernel offers to programmatically send a signal.

你还想什么办法?该kill()函数是内核提供的以编程方式发送信号的唯一方式。

Actually, you mentioned you were using Windows. I'm not even sure what kill()does on Windows, since Windows doesn't have the same signal architecture that Unix-derived systems do. Win32 does offer the TerminateProcess function, which may do what you want. There is also the GenerateConsoleCtrlEventfunction, which applies to console programs and simulates a Ctrl+C or Ctrl+Break.

实际上,您提到您使用的是Windows。我什至不确定kill()Windows 上有什么,因为 Windows 没有与 Unix 派生系统相同的信号架构。Win32 确实提供了 TerminateProcess 函数,它可以执行您想要的操作。还有GenerateConsoleCtrlEvent函数,它适用于控制台程序并模拟 Ctrl+C 或 Ctrl+Break。

回答by unwind

"Signals" in this regard are a Unix/POSIX concept. Windows has no direct equivalent.

在这方面的“信号”是一个 Unix/POSIX 概念。Windows 没有直接的等价物。

回答by Тарик Ялауи

void SendSIGINT( HANDLE hProcess )
{
    DWORD pid = GetProcessId(hProcess);
    FreeConsole();
    if (AttachConsole(pid))
    {
        // Disable Ctrl-C handling for our program
        SetConsoleCtrlHandler(NULL, true);

        GenerateConsoleCtrlEvent(CTRL_C_EVENT, 0); // SIGINT

        //Re-enable Ctrl-C handling or any subsequently started
        //programs will inherit the disabled state.
        SetConsoleCtrlHandler(NULL, false);

        WaitForSingleObject(hProcess, 10000);
    }
}

回答by jheriko

I assume this is a Win32 app...

我假设这是一个 Win32 应用程序...

For a "controlled" or "safe" exit, if the app uses a message loop you can use the PostQuitMessage API from inside of it, or PostMessage outside of it. Otherwise you will need to get the thread/process ID and use the TerminateThread or TerminateProcess API, depending on if you want to kill just a thread or the entire process and all threads it has spawned. It is explained nicely by Microsoft (as with all API calls) on MSDN:

对于“受控”或“安全”退出,如果应用程序使用消息循环,您可以从其内部使用 PostQuitMessage API,或在其外部使用 PostMessage。否则,您将需要获取线程/进程 ID 并使用 TerminateThread 或 TerminateProcess API,具体取决于您是想杀死一个线程还是整个进程及其产生的所有线程。Microsoft(与所有 API 调用一样)在 MSDN 上对此进行了很好的解释:

http://msdn.microsoft.com/en-us/library/aa450927.aspx

http://msdn.microsoft.com/en-us/library/aa450927.aspx