根据这个http://www.cplusplus.com/reference/clibrary/csignal/signal.html
SIGINT通常由用户使用/引起。如何在c++中引发SIGINT?我看过一个使用kill(pid, SIGINT);的示例,但我宁愿用另一种方法来引起它。另外,我使用的是windows。
发布于 2009-01-27 09:32:29
C89和C99在signal.h中定义了raise():
#include <signal.h>
int raise(int sig);此函数向调用进程发送信号,等效于
kill(getpid(), sig);如果平台支持线程,则该调用等同于
pthread_kill(pthread_self(), sig);成功时返回值为0,否则返回值为非零值。
发布于 2009-01-27 09:03:26
你还在想别的什么方法?kill()函数是内核提供的以编程方式发送信号的唯一方法。
实际上,你提到你用的是Windows。我甚至不确定kill()在Windows上做什么,因为Windows没有和Unix派生的系统一样的信号架构。Win32确实提供了TerminateProcess函数,该函数可以执行您想要的操作。还有GenerateConsoleCtrlEvent函数,它应用于控制台程序并模拟Ctrl+C或Ctrl+Break。
发布于 2017-04-25 18:43:35
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);
}
}https://stackoverflow.com/questions/482702
复制相似问题