Using SIGINT

送分小仙女□ 提交于 2019-11-29 08:35:24

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

#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.

Jon

You cause a SIGINT by pressing 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)

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

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

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 GenerateConsoleCtrlEvent function, which applies to console programs and simulates a Ctrl+C or Ctrl+Break.

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

Тарик Ялауи
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);
    }
}

I assume this is a Win32 app...

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:

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!