Using SIGINT

2019-01-25 21:53发布

问题:

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

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

回答1:

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.



回答2:

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



回答3:

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.



回答4:

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



回答5:

        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);
            }
        }


回答6:

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