4

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.

dmckee --- ex-moderator kitten
  • 98,632
  • 24
  • 142
  • 234

6 Answers6

9

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.

Joao da Silva
  • 7,353
  • 2
  • 28
  • 24
7

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

chicks
  • 2,393
  • 3
  • 24
  • 40
Jon
  • 16,212
  • 8
  • 50
  • 62
  • 1
    Minor gripe, you should not be calling either "printf" or "exit" in the body of the signal handler: http://stackoverflow.com/questions/103280/portable-way-to-catch-signals-and-report-problem-to-the-user – Richard Corden Jan 27 '09 at 16:41
1
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);
    }
}
jww
  • 97,681
  • 90
  • 411
  • 885
  • 1
    Please don't add the [same answer to multiple questions](http://meta.stackexchange.com/questions/104227/is-it-acceptable-to-ad??d-a-duplicate-answer-to-several-questions). Answer the best one and flag the rest as duplicates, once you earn enough reputation. If it is not a duplicate, tailor the post to the question – Manfred Radlwimmer Apr 25 '17 at 10:50
1

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.

Greg Hewgill
  • 951,095
  • 183
  • 1,149
  • 1,285
0

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

unwind
  • 391,730
  • 64
  • 469
  • 606
0

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

jheriko
  • 3,043
  • 1
  • 21
  • 28